Home Download Buy Blog Forum Support

open file from selected text

open file from selected text

Postby gjj391 on Fri Mar 18, 2011 3:06 pm

Is there a way to open a file (if file) from selected text... For example if I highlight a class name, is there a way to open that file in a new tab?
gjj391
 
Posts: 1
Joined: Fri Mar 18, 2011 3:03 pm

Re: open file from selected text

Postby mrbanzai on Fri Mar 18, 2011 3:19 pm

"goto anything" pre-populated with the selected text would be awesome. Ditto!
mrbanzai
 
Posts: 1
Joined: Fri Mar 18, 2011 3:18 pm

Re: open file from selected text

Postby theblacklion on Sat Mar 19, 2011 9:39 am

gjj391 wrote:Is there a way to open a file (if file) from selected text... For example if I highlight a class name, is there a way to open that file in a new tab?


If it's for Python or Magento (PHP) code I've something working.
theblacklion
 
Posts: 67
Joined: Sat Feb 05, 2011 10:07 pm
Location: Hannover, Germany

Re: open file from selected text

Postby ToddFiske on Mon Apr 04, 2011 5:07 pm

Here's a plug-in I wrote a little while back that does that.

It's intended to work like Ctrl+Enter in Borland Delphi: open the thing under the cursor as a file.

It will try in the current path first, and if not found, it will look in a list of other paths, and will try each of a list of extensions.

For example, I have a library called "GenUtils.py" that I keep in a \projects folder, on different drives between my home and work computers.

I can click on "import GenUtils" in a piece of code, and this plugin will search for and open the first file matching "GenUtils.py" or "GenUtils.txt" in any folder named in the list.


Copy the code below into your plugin folder as OpenFile.py

Code: Select all
   ...\Sublime Text\Data\Packages\User\OpenFile.py


Add a keystroke combination to your user keymap file

Code: Select all
  <binding key="ctrl+enter" command="openFile" />


(Ctrl+Enter is initially assigned to "Add Line.sublime-macro")


Make sure to edit the "paths" and "exts" lists below to match your preferences.

If you have questions or run into something that doesn't work, please let me know.

Todd

Code: Select all
# OpenFile.py
# open filename at cursor in SublimeText
# Todd Fiske (toddfiske at gmail)

# 2010-12-23 12:17:48  first version
# 2011-01-14 13:24:33  added MakeName generator to handle common extensions
# 2011-04-04 12:59:19  updated for Sublime Text forum

import sublime, sublimeplugin
import os

paths = [".", "h:\\projects", "c:\\projects"]
exts  = [".py", ".txt"]

def MakeName(paths=[], name="stub", exts=[]):
    for p in paths:
        for e in exts:
            thisName = os.path.join(p, name + e)
            yield thisName

class OpenFileCommand(sublimeplugin.TextCommand):
    def run(self, view, args):
        print "OpenFile: cwd = %s" % os.getcwd()

        for region in view.sel():
            if region.empty():
                # no selection, get word at cursor
                savedWordSeps = view.options().get("wordSeparators")
                view.options().set("wordSeparators", "")
                word = view.word(region)
                lineContents = view.substr(word)
                view.options().set("wordSeparators", savedWordSeps)

            else:
                #- else get selection
                lineContents = view.substr(region)

            #- remove any leading comment or space characters
            if len(lineContents) > 0:
                while (lineContents[0] in "# "):
                    lineContents = lineContents[1:]

            #- try to open unmodified input first
            fileName = lineContents
            print "  trying [%s]" % fileName
            found = os.path.exists(fileName)

            if not found:
                for fileName in MakeName(paths, fileName, exts):
                    print "  trying [%s]" % fileName
                    found = os.path.exists(fileName)
                    if found:
                        break

            if found:
                print "  opening %s" % fileName
                sublime.activeWindow().openFile(fileName)
            else:
                print "  no matching file was found"

###
ToddFiske
 
Posts: 38
Joined: Wed Nov 04, 2009 10:43 pm


Return to General Discussion

Who is online

Users browsing this forum: No registered users and 25 guests