diff --git a/functions.py b/functions.py index 97df8196..e80a592f 100644 --- a/functions.py +++ b/functions.py @@ -270,15 +270,32 @@ def goto(source, line, column, source_path): s = s.follow()[0] definitions.append(s) else: + def remove_unreal_imports(names): + """ + These imports are only virtual, because of multi-line imports. + """ + new_names = [] + for n in names: + par = n.parent + if isinstance(par, parsing.Import) and not par.start_pos[0]: + # this is a special case: If + for scope in imports.ImportPath(par).follow(): + if isinstance(scope, parsing.Import): + temp = scope.get_defined_names() + new_names += remove_unreal_imports(temp) + elif isinstance(scope, parsing.Module): + new_names.append(scope.get_module_name(n.names)) + else: + new_names.append(n) + return new_names + names = [] - #print 's', scopes for s in scopes: names += s.get_defined_names() + names = remove_unreal_imports(names) definitions = [n for n in names if n.names[-1] == search_name] - #print evaluate.statement_path - #print scopes, definitions _clear_caches() - return [Definition(d) for d in definitions] + return [Definition(d) for d in set(definitions)] def set_debug_function(func_cb): diff --git a/imports.py b/imports.py index 22c6b5e8..898bfebc 100644 --- a/imports.py +++ b/imports.py @@ -49,7 +49,9 @@ class ImportPath(object): def get_nested_import(self, parent): i = self.import_stmt - zero = (1,0) + # This is not an existing Import statement. Therefore, set position to + # None. + zero = (None, None) n = parsing.Name(i.namespace.names[1:], zero, zero) new = parsing.Import(zero, zero, n) new.parent = parent diff --git a/parsing.py b/parsing.py index 13491bfe..7e60d8b3 100644 --- a/parsing.py +++ b/parsing.py @@ -234,6 +234,7 @@ class Module(Scope): super(Module, self).__init__((1, 0), docstr) self.path = path self.global_vars = [] + self._name = None def add_global(self, name): """ @@ -252,6 +253,12 @@ class Module(Scope): n += self.global_vars return n + def get_module_name(self, names): + """ This is used for the goto function. """ + if not self._name: + self._name = Name(names, self.start_pos, self.end_pos, self) + return self._name + class Class(Scope): """ diff --git a/test/completion/imports.py b/test/completion/imports.py index e7403cb1..845587bc 100644 --- a/test/completion/imports.py +++ b/test/completion/imports.py @@ -49,9 +49,6 @@ def scope_nested(): #? float() import_tree.pkg.mod1.a - #? ['a', 'pkg'] - import_tree. - # ----------------- # std lib modules # ----------------- diff --git a/test/completion/thirdparty/pylab.py b/test/completion/thirdparty/pylab.py index e7ad2661..45e121cf 100644 --- a/test/completion/thirdparty/pylab.py +++ b/test/completion/thirdparty/pylab.py @@ -5,7 +5,7 @@ pylab. #! ['module numpy'] import numpy -#! ['module random'] +#! ['import random', 'module random'] import numpy.random #? ['array2string']