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?
...\Sublime Text\Data\Packages\User\OpenFile.py <binding key="ctrl+enter" command="openFile" /># 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"
###
Users browsing this forum: No registered users and 6 guests