Created
May 20, 2014 22:46
-
-
Save pietvanzoen/899d90cc200328fbf95c to your computer and use it in GitHub Desktop.
Sublime Text Plugin - goto file/line from "Find Results" page by hitting ENTER. Based on this stack overflow answer: http://stackoverflow.com/a/16779397
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Add this file to your Sublime Packages/User folder | |
import sublime | |
import sublime_plugin | |
import re | |
import os | |
class FindInFilesGotoCommand(sublime_plugin.TextCommand): | |
def run(self, edit): | |
view = self.view | |
if view.name() == "Find Results": | |
line_no = self.get_line_no() | |
file_name = self.get_file() | |
if line_no is not None and file_name is not None: | |
file_loc = "%s:%s" % (file_name, line_no) | |
view.window().open_file(file_loc, sublime.ENCODED_POSITION) | |
elif file_name is not None: | |
view.window().open_file(file_name) | |
def get_line_no(self): | |
view = self.view | |
if len(view.sel()) == 1: | |
line_text = view.substr(view.line(view.sel()[0])) | |
match = re.match(r"\s*(\d+).+", line_text) | |
if match: | |
return match.group(1) | |
return None | |
def get_file(self): | |
view = self.view | |
if len(view.sel()) == 1: | |
line = view.line(view.sel()[0]) | |
while line.begin() > 0: | |
line_text = view.substr(line) | |
match = re.match(r"(.+):$", line_text) | |
if match: | |
if os.path.exists(match.group(1)): | |
return match.group(1) | |
line = view.line(line.begin() - 1) | |
return None |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Add this to your user keybindings settings: | |
{ | |
"keys": ["enter"], | |
"command": "find_in_files_goto", | |
"context": [{ | |
"key": "selector", | |
"operator": "equal", | |
"operand": "text.find-in-files" | |
}] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment