Created
July 4, 2018 18:34
-
-
Save TheClams/16ac624bce30de695781ba6ab878879e to your computer and use it in GitHub Desktop.
[SublimeText3] Modified Goto definition to select current file if inside the list
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
import sublime | |
import sublime_plugin | |
import os | |
def lookup_symbol(window, symbol): | |
if len(symbol.strip()) < 3: | |
return [] | |
index_locations = window.lookup_symbol_in_index(symbol) | |
open_file_locations = window.lookup_symbol_in_open_files(symbol) | |
def file_in_location_list(fname, locations): | |
for l in locations: | |
if l[0] == fname: | |
return True | |
return False | |
# Combine the two lists, overriding results in the index with results | |
# from open files, while trying to preserve the order of the files in | |
# the index. | |
locations = [] | |
ofl_ignore = [] | |
for l in index_locations: | |
if file_in_location_list(l[0], open_file_locations): | |
if not file_in_location_list(l[0], ofl_ignore): | |
for ofl in open_file_locations: | |
if l[0] == ofl[0]: | |
locations.append(ofl) | |
ofl_ignore.append(ofl) | |
else: | |
locations.append(l) | |
for ofl in open_file_locations: | |
if not file_in_location_list(ofl[0], ofl_ignore): | |
locations.append(ofl) | |
return locations | |
def symbol_at_point(view, pt): | |
symbol = view.substr(view.expand_by_class(pt, sublime.CLASS_WORD_START | sublime.CLASS_WORD_END, "[]{}()<>:.")) | |
locations = lookup_symbol(view.window(), symbol) | |
if len(locations) == 0: | |
symbol = view.substr(view.word(pt)) | |
locations = lookup_symbol(view.window(), symbol) | |
return symbol, locations | |
def open_location(window, l): | |
fname, display_fname, rowcol = l | |
row, col = rowcol | |
window.open_file( | |
fname + ":" + str(row) + ":" + str(col), | |
sublime.ENCODED_POSITION | sublime.FORCE_GROUP) | |
def format_location(l): | |
fname, display_fname, rowcol = l | |
row, col = rowcol | |
return display_fname + ":" + str(row) | |
return new_locations | |
def navigate_to_symbol(view, symbol, locations): | |
def select_entry(window, locations, idx, orig_view, orig_sel): | |
if idx >= 0: | |
open_location(window, locations[idx]) | |
else: | |
if orig_view: | |
orig_view.sel().clear() | |
orig_view.sel().add_all(orig_sel) | |
window.focus_view(orig_view) | |
orig_view.show(orig_sel[0]) | |
def highlight_entry(window, locations, idx): | |
fname, display_fname, rowcol = locations[idx] | |
row, col = rowcol | |
window.open_file( | |
fname + ":" + str(row) + ":" + str(col), | |
group=window.active_group(), | |
flags=sublime.TRANSIENT | sublime.ENCODED_POSITION | sublime.FORCE_GROUP) | |
orig_sel = None | |
if view: | |
orig_sel = [r for r in view.sel()] | |
if len(locations) == 0: | |
sublime.status_message("Unable to find " + symbol) | |
elif len(locations) == 1: | |
open_location(view.window(), locations[0]) | |
else: | |
# Check if current file is in the list | |
view_fname = os.path.splitext(view.file_name())[0] | |
print(view_fname) | |
files = [os.path.splitext(x[0])[0] for x in locations] | |
idx = 0 | |
if view_fname in files: | |
idx = files.index(view_fname) | |
window = view.window() | |
window.show_quick_panel( | |
items=[format_location(l) for l in locations], | |
on_select=lambda x: select_entry(window, locations, x, view, orig_sel), | |
on_highlight=lambda x: highlight_entry(window, locations, x), | |
selected_index=idx, | |
flags=sublime.KEEP_OPEN_ON_FOCUS_LOST) | |
class MyGotoDefinition(sublime_plugin.WindowCommand): | |
def run(self, symbol=None): | |
v = self.window.active_view() | |
if not symbol and not v: | |
return | |
if not symbol: | |
pt = v.sel()[0] | |
symbol, locations = symbol_at_point(v, pt) | |
else: | |
locations = lookup_symbol(self.window, symbol) | |
navigate_to_symbol(v, symbol, locations) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
To use simply save file in your Package User directory and bind a key to "my_goto_definition":
{ "keys": ["alt+f12"], "command": "my_goto_definition" }