mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-07 22:44:27 +08:00
goto function added
This commit is contained in:
@@ -27,7 +27,7 @@ Jedi supports many of the widely used Python features:
|
|||||||
- multiple returns / yields
|
- multiple returns / yields
|
||||||
- tuple assignments / array indexing / dictionary indexing
|
- tuple assignments / array indexing / dictionary indexing
|
||||||
- exceptions / with-statement /
|
- exceptions / with-statement /
|
||||||
- \*args / \*\*args
|
- \*args / \*\*kwargs
|
||||||
- decorators
|
- decorators
|
||||||
- generators (yield statement)
|
- generators (yield statement)
|
||||||
|
|
||||||
|
|||||||
42
functions.py
42
functions.py
@@ -5,7 +5,12 @@ import evaluate
|
|||||||
import modules
|
import modules
|
||||||
import debug
|
import debug
|
||||||
|
|
||||||
__all__ = ['complete', 'get_completion_parts', 'set_debug_function']
|
__all__ = ['complete', 'goto', 'get_completion_parts', 'set_debug_function']
|
||||||
|
|
||||||
|
|
||||||
|
class NotFoundError(Exception):
|
||||||
|
""" A custom error to avoid catching the wrong errors """
|
||||||
|
pass
|
||||||
|
|
||||||
|
|
||||||
class Completion(object):
|
class Completion(object):
|
||||||
@@ -85,7 +90,7 @@ def complete(source, row, column, source_path):
|
|||||||
"""
|
"""
|
||||||
f = modules.ModuleWithCursor(source_path, source=source, row=row)
|
f = modules.ModuleWithCursor(source_path, source=source, row=row)
|
||||||
scope = f.parser.user_scope
|
scope = f.parser.user_scope
|
||||||
path = f.get_row_path(column)
|
path = f.get_path_until_cursor(column)
|
||||||
debug.dbg('completion_start: %s in %s' % (path, scope))
|
debug.dbg('completion_start: %s in %s' % (path, scope))
|
||||||
|
|
||||||
# just parse one statement, take it and evaluate it
|
# just parse one statement, take it and evaluate it
|
||||||
@@ -124,6 +129,39 @@ def complete(source, row, column, source_path):
|
|||||||
return [Completion(c, needs_dot, len(like)) for c in set(completions)]
|
return [Completion(c, needs_dot, len(like)) for c in set(completions)]
|
||||||
|
|
||||||
|
|
||||||
|
def prepare_goto(source, row, column, source_path, is_like_search):
|
||||||
|
f = modules.ModuleWithCursor(source_path, source=source, row=row)
|
||||||
|
scope = f.parser.user_scope
|
||||||
|
|
||||||
|
if is_like_search:
|
||||||
|
path = f.get_path_until_cursor(column)
|
||||||
|
path, dot, like = get_completion_parts(path)
|
||||||
|
else:
|
||||||
|
path = f.get_path_under_cursor(column)
|
||||||
|
|
||||||
|
debug.dbg('start: %s in %s' % (path, scope))
|
||||||
|
|
||||||
|
# just parse one statement, take it and evaluate it
|
||||||
|
r = parsing.PyFuzzyParser(path, source_path)
|
||||||
|
try:
|
||||||
|
stmt = r.top.statements[0]
|
||||||
|
except IndexError:
|
||||||
|
raise NotFoundError()
|
||||||
|
else:
|
||||||
|
stmt.line_nr = row
|
||||||
|
stmt.indent = column
|
||||||
|
stmt.parent = scope
|
||||||
|
scopes = evaluate.follow_statement(stmt, scope=scope)
|
||||||
|
return scope, scopes
|
||||||
|
|
||||||
|
|
||||||
|
def goto(source, row, column, source_path):
|
||||||
|
dummy, scopes = prepare_goto(source, row, column, source_path, False)
|
||||||
|
|
||||||
|
_clear_caches()
|
||||||
|
return scopes
|
||||||
|
|
||||||
|
|
||||||
def set_debug_function(func_cb):
|
def set_debug_function(func_cb):
|
||||||
"""
|
"""
|
||||||
You can define a callback debug function to get all the debug messages.
|
You can define a callback debug function to get all the debug messages.
|
||||||
|
|||||||
12
modules.py
12
modules.py
@@ -1,3 +1,4 @@
|
|||||||
|
import re
|
||||||
import tokenize
|
import tokenize
|
||||||
import imp
|
import imp
|
||||||
import os
|
import os
|
||||||
@@ -61,7 +62,7 @@ class ModuleWithCursor(Module):
|
|||||||
|
|
||||||
self._parser = parsing.PyFuzzyParser(source, path, row)
|
self._parser = parsing.PyFuzzyParser(source, path, row)
|
||||||
|
|
||||||
def get_row_path(self, column):
|
def get_path_until_cursor(self, column):
|
||||||
""" Get the path under the cursor. """
|
""" Get the path under the cursor. """
|
||||||
self._is_first = True
|
self._is_first = True
|
||||||
|
|
||||||
@@ -122,6 +123,15 @@ class ModuleWithCursor(Module):
|
|||||||
|
|
||||||
return string[::-1]
|
return string[::-1]
|
||||||
|
|
||||||
|
def get_path_under_cursor(self, column):
|
||||||
|
"""
|
||||||
|
Return the path under the cursor. If there is a rest of the path left,
|
||||||
|
it will be added to the stuff before it.
|
||||||
|
"""
|
||||||
|
line = self.get_line(self.row)
|
||||||
|
after = re.search("[\w\d]*", line[column:]).group(0)
|
||||||
|
return self.get_path_until_cursor(column) + after
|
||||||
|
|
||||||
def get_line(self, line):
|
def get_line(self, line):
|
||||||
if not self._line_cache:
|
if not self._line_cache:
|
||||||
self._line_cache = self.source.split('\n')
|
self._line_cache = self.source.split('\n')
|
||||||
|
|||||||
@@ -177,4 +177,4 @@ def nexti(iterator, default=list):
|
|||||||
|
|
||||||
#exe[0].
|
#exe[0].
|
||||||
#exe[4]['d']
|
#exe[4]['d']
|
||||||
next(gen_exe).; gen_exe.
|
next(gen_exe).
|
||||||
|
|||||||
Reference in New Issue
Block a user