From 83d2af5138980af50f4f3c304855ab1f798bc71d Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 19 Sep 2014 01:21:17 +0200 Subject: [PATCH] First imports are working with goto. --- jedi/evaluate/__init__.py | 11 +++++++++++ jedi/parser/representation.py | 22 ++++++++++++++++++++++ test/test_api/test_api_classes.py | 6 +++--- 3 files changed, 36 insertions(+), 3 deletions(-) diff --git a/jedi/evaluate/__init__.py b/jedi/evaluate/__init__.py index 64843c32..0dddcbeb 100644 --- a/jedi/evaluate/__init__.py +++ b/jedi/evaluate/__init__.py @@ -318,6 +318,17 @@ class Evaluator(object): return types 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 # statement name definitions. Only return, if it's one name and one # name only. Otherwise it's a mixture between a definition and a diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index 85a10a8d..45d0bed2 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -840,6 +840,28 @@ class Import(Simple): n.append(self.alias) 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): """ This checks for the special case of nested imports, without aliases and diff --git a/test/test_api/test_api_classes.py b/test/test_api/test_api_classes.py index 16ac3efc..94799cff 100644 --- a/test/test_api/test_api_classes.py +++ b/test/test_api/test_api_classes.py @@ -255,15 +255,15 @@ class TestGotoAssignments(TestCase): assert n.goto_assignments()[0].name == 'upper' 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].type == 'import' n = nms[0].goto_assignments()[0] assert n.name == 'json' assert n.type == 'module' - assert nms[1].name == 'decode' + assert nms[1].name == 'load' assert nms[1].type == 'import' n = nms[1].goto_assignments()[0] - assert n.name == 'decode' + assert n.name == 'load' assert n.type == 'function'