Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor all the things #16

Closed
wants to merge 14 commits into from
95 changes: 35 additions & 60 deletions python_go_to.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import sublime_plugin

from sublime_jedi import get_script, JediEnvMixin
from jedi.api import NotFoundError


def check_if_string(view):
Expand Down Expand Up @@ -45,58 +46,33 @@ def run(self, edit):
self.view.run_command('string_go_to')
return

# install user env
self.install_env()

script = get_script(self.view, self.view.sel()[0].begin())

# If we have a possible python declaration
# use jedi to find possible declarations.
found = self.attempt_get_definition(script)
if not found:
found = self.attempt_go_to(script)

# restore sublime env, to keep functionality
self.restore_env()

def attempt_go_to(self, script):
""" Uses `jedi.api.Script` goto function
for possible definition.

:param script: `jedi.api.Script` object

:return: bool
"""
found = script.goto()
if len(found) > 0:
if len(found) == 1:
x = found[0]
self._jump_to_in_window(x.module_path, x.start_pos[0], x.start_pos[1])

else:
self._window_quick_panel_open_window(found)
return True

return False

def attempt_get_definition(self, script):
""" Uses `jedi.api.Script` get_definition function
for possible definition.

:param script: `jedi.api.Script` object

:return: bool
"""
found = script.get_definition()
if len(found) == 1:
x = found[0]
self._jump_to_in_window(x.module_path, x.start_pos[0], x.start_pos[1])
return True
elif len(found) > 1:
self._window_quick_panel_open_window(found)
return True

return False
with self.env:
script = get_script(self.view, self.view.sel()[0].begin())

# If we have a possible python declaration
# use jedi to find possible declarations.
# found = self.attempt_get_definition(script)
# if not found:
# found = self.attempt_go_to(script)
for method in ['get_definition', 'goto']:
try:
defns = getattr(script, method)()
except NotFoundError:
return
else:
self.handle_definitions(defns)
break

def handle_definitions(self, defns):
# filter out builtin
self.defns = [i for i in defns if not i.in_builtin_module()]
if not self.defns:
return False
if len(self.defns) == 1:
defn = self.defns[0]
self._jump_to_in_window(defn.module_path, defn.start_pos[0], defn.start_pos[1])
else:
self._window_quick_panel_open_window(self.defns)

def _jump_to_in_window(self, filename, line_number=None, column_number=None):
""" Opens a new window and jumps to declaration if possible
Expand All @@ -109,10 +85,11 @@ def _jump_to_in_window(self, filename, line_number=None, column_number=None):

# If the file was selected from a drop down list
if isinstance(filename, int):
filename = self.options[filename]
if filename == -1: # cancelled
return
line_number, column_number = self.options_map[filename]

active_window.open_file('%s:%s:%s' % (filename, line_number or 0, column_number or 0), sublime.ENCODED_POSITION)
active_window.open_file('%s:%s:%s' % (filename, line_number or 0,
column_number or 0), sublime.ENCODED_POSITION)

def _window_quick_panel_open_window(self, options):
""" Shows the active `sublime.Window` quickpanel (dropdown) for
Expand All @@ -123,11 +100,9 @@ def _window_quick_panel_open_window(self, options):

active_window = self.view.window()

# Get the filenames
self.options = [o.module_path for o in options]

# Map the filenames to line and column numbers
self.options_map = dict((o.module_path, (o.start_pos[0], o.start_pos[1])) for o in options)
self.options_map = dict((o.module_path, (o.start_pos[0], o.start_pos[1]))
for o in self.defns)

# Show the user a selection of filenames
active_window.show_quick_panel(self.options, self._jump_to_in_window)
active_window.show_quick_panel(self.defns, self._jump_to_in_window)
Loading