mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-11 08:11:50 +08:00
refactored Definition class. therefore the 'goto' function now uses it, too
This commit is contained in:
55
functions.py
55
functions.py
@@ -1,5 +1,4 @@
|
|||||||
import re
|
import re
|
||||||
import sys
|
|
||||||
|
|
||||||
import parsing
|
import parsing
|
||||||
import evaluate
|
import evaluate
|
||||||
@@ -64,47 +63,59 @@ class Completion(object):
|
|||||||
|
|
||||||
|
|
||||||
class Definition(object):
|
class Definition(object):
|
||||||
def __init__(self, scope):
|
def __init__(self, definition):
|
||||||
""" The definition of a function """
|
""" The definition of a function """
|
||||||
self.scope = scope
|
self.definition = definition
|
||||||
|
|
||||||
def get_name(self):
|
def get_name(self):
|
||||||
try:
|
try:
|
||||||
return self.scope.name
|
# is a func / class
|
||||||
|
return self.definition.name
|
||||||
except AttributeError:
|
except AttributeError:
|
||||||
return self.scope.type
|
try:
|
||||||
|
# is an array
|
||||||
|
return self.definition.type
|
||||||
|
except:
|
||||||
|
# is a statement
|
||||||
|
return self.definition.get_code()
|
||||||
|
|
||||||
def get_module(self):
|
@property
|
||||||
par = self.scope
|
def module_name(self):
|
||||||
|
path = self.module_path
|
||||||
|
try:
|
||||||
|
return path[path.rindex('/') + 1:]
|
||||||
|
except ValueError:
|
||||||
|
return path
|
||||||
|
|
||||||
|
@property
|
||||||
|
def module_path(self):
|
||||||
|
par = self.definition
|
||||||
while True:
|
while True:
|
||||||
if par.parent is not None:
|
if par.parent is not None:
|
||||||
par = par.parent
|
par = par.parent
|
||||||
else:
|
else:
|
||||||
break
|
break
|
||||||
|
|
||||||
path = str(par.path)
|
return str(par.path)
|
||||||
try:
|
|
||||||
return path[path.rindex('/') + 1:]
|
|
||||||
except ValueError:
|
|
||||||
return path
|
|
||||||
|
|
||||||
def get_line(self):
|
@property
|
||||||
return self.scope.start_pos[0]
|
def line_nr(self):
|
||||||
|
return self.definition.start_pos[0]
|
||||||
|
|
||||||
def get_indent(self):
|
@property
|
||||||
return self.scope.indent
|
def column(self):
|
||||||
|
return self.definition.start_pos[1]
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
module = self.get_module()
|
if self.module_path[0] == '/':
|
||||||
if module[0] == '/':
|
position = '@%s' % (self.line_nr)
|
||||||
position = '@%s' % (self.get_line())
|
|
||||||
else:
|
else:
|
||||||
# no path - is a builtin
|
# no path - is a builtin
|
||||||
position = ''
|
position = ''
|
||||||
return "%s:%s%s" % (module, self.get_name(), position)
|
return "%s:%s%s" % (self.module_name, self.get_name(), position)
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
return "<%s %s>" % (self.__class__.__name__, self)
|
return "<%s %s>" % (self.__class__.__name__, self.definition)
|
||||||
|
|
||||||
|
|
||||||
def get_completion_parts(path):
|
def get_completion_parts(path):
|
||||||
@@ -238,7 +249,7 @@ def goto(source, line, column, source_path):
|
|||||||
#print evaluate.statement_path
|
#print evaluate.statement_path
|
||||||
#print scopes, definitions
|
#print scopes, definitions
|
||||||
_clear_caches()
|
_clear_caches()
|
||||||
return definitions
|
return [Definition(d) for d in definitions]
|
||||||
|
|
||||||
|
|
||||||
def set_debug_function(func_cb):
|
def set_debug_function(func_cb):
|
||||||
|
|||||||
@@ -110,6 +110,7 @@ def run_goto_test(correct, source, line_nr, line, path):
|
|||||||
else:
|
else:
|
||||||
lst = []
|
lst = []
|
||||||
for r in result:
|
for r in result:
|
||||||
|
r = r.definition
|
||||||
if isinstance(r, evaluate.InstanceElement):
|
if isinstance(r, evaluate.InstanceElement):
|
||||||
r = r.var
|
r = r.var
|
||||||
if isinstance(r, (evaluate.Class, evaluate.Instance)):
|
if isinstance(r, (evaluate.Class, evaluate.Instance)):
|
||||||
|
|||||||
Reference in New Issue
Block a user