├── .gitignore ├── Default.sublime-mousemap ├── Default.sublime-keymap ├── repositories.json ├── README.md └── go_to_file.py /.gitignore: -------------------------------------------------------------------------------- 1 | *.pyc 2 | -------------------------------------------------------------------------------- /Default.sublime-mousemap: -------------------------------------------------------------------------------- 1 | [ 2 | // { 3 | // "button": "button1", "modifiers": ["alt+shift"], 4 | // "command": "go_to_file" 5 | // } 6 | ] -------------------------------------------------------------------------------- /Default.sublime-keymap: -------------------------------------------------------------------------------- 1 | [ 2 | {"keys": ["alt+d"], "command": "go_to_file"}, 3 | {"keys": ["alt+i"], "command": "file_info"}, 4 | {"keys": ["alt+k"], "command": "file_info_short"} 5 | ] -------------------------------------------------------------------------------- /repositories.json: -------------------------------------------------------------------------------- 1 | { 2 | "schema_version": "1.0.0", 3 | "repositories": [ 4 | "https://github.com/gs/sublime-text-go-to-file" 5 | ], 6 | "package_name_map": { 7 | "sublime-text-go-to-file": "GoToFile" 8 | } 9 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | Note: 2 | 3 | This plugin has some differences that the original: See here: https://github.com/stvkoch/sublime-text-go-to-file/compare/gs:master...master 4 | 5 | 6 | Sublime Text Go To File 7 | ======================= 8 | 9 | Overview 10 | -------- 11 | Plugin searches for a file under the cursor and opens it in new tab. 12 | (The idea comes from Vim `gf` functionality as I missed it in Sublime Text and also the script found on https://gist.github.com/jbjornson/1186126 written by @jbjornson helped a lot!) 13 | 14 | Additional functionality - printing currently opened file path in status bar and copy it to clipboard. 15 | 16 | Usage 17 | ----- 18 | Add shortcut to your keybindings: 19 | 20 | { 21 | "keys": ["alt+d"], "command": "go_to_file", 22 | "keys": ["alt+i"], "command": "file_info", 23 | "keys": ["alt+k"], "command": "file_info_short" 24 | } 25 | 26 | You can highlight the text using `cmd+d` and press `alt+d`. 27 | 28 | Press `alt-i` to get info about currently opened file. 29 | Press `alt-k` to get short info about currently opened file. 30 | 31 | 32 | Maintainers: 33 | ------------ 34 | * Grzegorz Smajdor (https://github.com/gs) 35 | * Maciej Gajek (https://github.com/maltize) 36 | 37 | Installation 38 | ------------ 39 | 40 | Go to your Sublime Text 3 `Packages` directory 41 | 42 | - OS X: `~/Library/Application\ Support/Sublime\ Text\ 3/Packages` 43 | - Windows: `%APPDATA%/Sublime Text 3/Packages/` 44 | - Linux: `~/.config/sublime-text-3/Packages/` 45 | 46 | and clone the repository using the command below: 47 | 48 | 49 | This version: 50 | ``` shell 51 | git clone https://github.com/stvkoch/sublime-text-go-to-file.git GoToFile 52 | ``` 53 | 54 | 55 | Original version without open file from class name: 56 | ``` shell 57 | git clone https://github.com/gs/sublime-text-go-to-file.git GoToFile 58 | ``` 59 | 60 | Note 61 | ---- 62 | Please open an issue at https://github.com/gs/sublime-text-go-to-file if you discover a problem or would like to see a feature/change implemented. 63 | 64 | 65 | Big Thanks to... 66 | ---------------- 67 | 68 | @stvkoch - who improved the plugin a LOT! 69 | 70 | 71 | Try: 72 | ---- 73 | - select namespace and click alt+d 74 | - click on word of class name and click alt+d 75 | - click on path of template file and click alt+d 76 | -------------------------------------------------------------------------------- /go_to_file.py: -------------------------------------------------------------------------------- 1 | import sublime, sublime_plugin 2 | import os, string 3 | import re 4 | 5 | class GoToFile(sublime_plugin.TextCommand): 6 | def run(self, edit): 7 | for region in self.view.sel(): 8 | # Collect the texts that may possibly be filenames 9 | quoted_text = self.get_quoted_selection(region).split(os.sep)[-1] 10 | selected_text = self.get_selection(region) 11 | text_on_cursor = None 12 | if region.begin() == region.end(): 13 | word = self.view.word(region) 14 | if not word.empty(): 15 | text_on_cursor = self.view.substr(word) 16 | # view.run_command("expand_selection", {"to": "word"}) 17 | # selected_text = self.get_selection(region) 18 | whole_line = self.get_line(region) 19 | candidates = [selected_text, self.extract_candidate_from_line(), quoted_text, text_on_cursor, whole_line] 20 | self.try_open(candidates) 21 | 22 | def try_open(self, candidates): 23 | for text in candidates: 24 | if text is None or len(text) == 0: 25 | continue 26 | 27 | self.potential_files = self.get_filename(text) 28 | if len(self.potential_files) > 0: 29 | break 30 | 31 | if len(self.potential_files) > 1: 32 | self.view.window().show_quick_panel(self.potential_files, self.open_file) 33 | elif len(self.potential_files) == 1: 34 | print("Opening file '%s'" % (self.potential_files[0])) 35 | self.view.window().open_file(self.potential_files[0],sublime.ENCODED_POSITION) 36 | else: 37 | sublime.error_message("No file found") 38 | 39 | 40 | def open_file(self, selected_index): 41 | if selected_index != -1: 42 | file = self.potential_files[selected_index] 43 | print("Opening file '%s'" % (file)) 44 | self.view.window().open_file(file,sublime.ENCODED_POSITION) 45 | 46 | 47 | def get_selection(self, region): 48 | return self.view.substr(region).strip() 49 | 50 | 51 | def get_line(self, region): 52 | return self.view.substr(self.view.line(region)).strip() 53 | 54 | 55 | def get_quoted_selection(self, region): 56 | text = self.view.substr(self.view.line(region)) 57 | position = self.view.rowcol(region.begin())[1] 58 | quoted_text = self.expand_within_quotes(text, position, '"') 59 | if not quoted_text: 60 | quoted_text = self.expand_within_quotes(text, position, '\'') 61 | return quoted_text 62 | 63 | 64 | def expand_within_quotes(self, text, position, quote_character): 65 | open_quote = text.rfind(quote_character, 0, position) 66 | close_quote = text.find(quote_character, position) 67 | return text[open_quote+1:close_quote] if (open_quote > 0 and close_quote > 0) else '' 68 | 69 | 70 | def get_filename(self, text): 71 | results = [] 72 | text = text.replace('\\', os.sep).replace(os.sep+os.sep, os.sep).replace('import ', '').replace('use ', '').replace(';', '').strip() 73 | print("get filename " + text) 74 | directories = self.view.window().folders() 75 | for directory in directories: 76 | for dirname, _, files in self.walk(directory): 77 | for file in files: 78 | fileName = dirname + os.sep + file 79 | if re.search(text, fileName): 80 | results += [fileName] 81 | return results 82 | 83 | 84 | def walk(self, directory): 85 | for dir, dirnames, files in os.walk(directory): 86 | dirnames[:] = [dirname for dirname in dirnames] 87 | yield dir, dirnames, files 88 | 89 | 90 | def extract_candidate_from_line(self): 91 | view = sublime.active_window().active_view() 92 | for sel in view.sel(): 93 | patternStr = view.substr(view.word(sel)).strip() 94 | lineStr = view.substr(view.line(sel)).strip() 95 | result = re.search( '(([^(\s|=|\+|\.)|,]*)'+patternStr+'[^(\s|:|;|,|\.|\(]*)', lineStr ) 96 | if result != None: 97 | return result.group() 98 | 99 | class FileInfo(sublime_plugin.WindowCommand): 100 | def run(self): 101 | path = self.current_file() 102 | sublime.set_clipboard(path) 103 | sublime.status_message(path) 104 | 105 | def current_file(self): 106 | return self.window.active_view().file_name() 107 | 108 | class FileInfoShort(FileInfo): 109 | def run(self): 110 | path = self.extract_path(self.current_file()) 111 | sublime.set_clipboard(path) 112 | sublime.status_message(path) 113 | 114 | def extract_path(self, file_path): 115 | directories = self.window.folders() 116 | if directories: 117 | project_path = directories[0] 118 | return file_path.replace(project_path + '/', '') 119 | else: 120 | return file_path 121 | --------------------------------------------------------------------------------