mirror of
https://github.com/davidhalter/jedi.git
synced 2025-12-08 14:54:47 +08:00
First imports are working with goto.
This commit is contained in:
@@ -318,6 +318,17 @@ class Evaluator(object):
|
|||||||
return types
|
return types
|
||||||
|
|
||||||
def goto(self, stmt, call_path):
|
def goto(self, stmt, call_path):
|
||||||
|
if isinstance(stmt, pr.Import):
|
||||||
|
# Nowhere to goto for aliases
|
||||||
|
if stmt.alias_name_part == call_path[0]:
|
||||||
|
return [call_path[0]]
|
||||||
|
|
||||||
|
names = stmt.get_all_import_name_parts()
|
||||||
|
# Filter names that are after our Name
|
||||||
|
removed_names = len(names) - names.index(call_path[0]) - 1
|
||||||
|
i = imports.ImportWrapper(self, stmt, kill_count=removed_names)
|
||||||
|
return i.follow(is_goto=True)
|
||||||
|
|
||||||
# Return the name defined in the call_path, if it's part of the
|
# Return the name defined in the call_path, if it's part of the
|
||||||
# statement name definitions. Only return, if it's one name and one
|
# statement name definitions. Only return, if it's one name and one
|
||||||
# name only. Otherwise it's a mixture between a definition and a
|
# name only. Otherwise it's a mixture between a definition and a
|
||||||
|
|||||||
@@ -840,6 +840,28 @@ class Import(Simple):
|
|||||||
n.append(self.alias)
|
n.append(self.alias)
|
||||||
return n
|
return n
|
||||||
|
|
||||||
|
def get_all_import_name_parts(self):
|
||||||
|
"""
|
||||||
|
TODO refactor and use this method, because NamePart will not exist in
|
||||||
|
the future.
|
||||||
|
"""
|
||||||
|
n = []
|
||||||
|
if self.from_ns:
|
||||||
|
n += self.from_ns.names
|
||||||
|
if self.namespace:
|
||||||
|
n += self.namespace.names
|
||||||
|
if self.alias:
|
||||||
|
n += self.alias.names
|
||||||
|
return n
|
||||||
|
|
||||||
|
@property
|
||||||
|
def alias_name_part(self):
|
||||||
|
"""
|
||||||
|
TODO refactor and dont use this method, because NamePart will not exist in
|
||||||
|
the future.
|
||||||
|
"""
|
||||||
|
return self.alias.names[0] if self.alias else None
|
||||||
|
|
||||||
def is_nested(self):
|
def is_nested(self):
|
||||||
"""
|
"""
|
||||||
This checks for the special case of nested imports, without aliases and
|
This checks for the special case of nested imports, without aliases and
|
||||||
|
|||||||
@@ -255,15 +255,15 @@ class TestGotoAssignments(TestCase):
|
|||||||
assert n.goto_assignments()[0].name == 'upper'
|
assert n.goto_assignments()[0].name == 'upper'
|
||||||
|
|
||||||
def test_import(self):
|
def test_import(self):
|
||||||
nms = names('from json import decode', references=True)
|
nms = names('from json import load', references=True)
|
||||||
assert nms[0].name == 'json'
|
assert nms[0].name == 'json'
|
||||||
assert nms[0].type == 'import'
|
assert nms[0].type == 'import'
|
||||||
n = nms[0].goto_assignments()[0]
|
n = nms[0].goto_assignments()[0]
|
||||||
assert n.name == 'json'
|
assert n.name == 'json'
|
||||||
assert n.type == 'module'
|
assert n.type == 'module'
|
||||||
|
|
||||||
assert nms[1].name == 'decode'
|
assert nms[1].name == 'load'
|
||||||
assert nms[1].type == 'import'
|
assert nms[1].type == 'import'
|
||||||
n = nms[1].goto_assignments()[0]
|
n = nms[1].goto_assignments()[0]
|
||||||
assert n.name == 'decode'
|
assert n.name == 'load'
|
||||||
assert n.type == 'function'
|
assert n.type == 'function'
|
||||||
|
|||||||
Reference in New Issue
Block a user