1
0
forked from VimPlug/jedi

Star imports are now part of the ModuleWrapper.

This commit is contained in:
Dave Halter
2014-11-19 18:09:49 +01:00
parent 08bdcfb8ca
commit ba0e61d99f
4 changed files with 38 additions and 5 deletions

View File

@@ -569,9 +569,11 @@ def get_names_of_scope(evaluator, scope, position=None, star_search=True, includ
# Add star imports. # Add star imports.
if star_search: if star_search:
"""
for s in imports.remove_star_imports(evaluator, non_flow.get_parent_until()): for s in imports.remove_star_imports(evaluator, non_flow.get_parent_until()):
for g in get_names_of_scope(evaluator, s, star_search=False): for g in get_names_of_scope(evaluator, s, star_search=False):
yield g yield g
"""
# Add builtins to the global scope. # Add builtins to the global scope.
if include_builtin: if include_builtin:

View File

@@ -77,9 +77,9 @@ class ImportWrapper(pr.Base):
else: else:
scopes = [module] scopes = [module]
star_imports = remove_star_imports(self._evaluator, module) #star_imports = remove_star_imports(self._evaluator, module)
if star_imports: #if star_imports:
scopes = [StarImportModule(scopes[0], star_imports)] # scopes = [StarImportModule(scopes[0], star_imports)]
# goto only accepts `Name` # goto only accepts `Name`
if is_goto and not rest: if is_goto and not rest:
@@ -616,6 +616,7 @@ def remove_star_imports(evaluator, scope, ignored_modules=()):
and follow these modules. and follow these modules.
""" """
raise NotImplementedError
if isinstance(scope, StarImportModule): if isinstance(scope, StarImportModule):
return scope.star_import_modules return scope.star_import_modules
modules = follow_imports(evaluator, (i for i in scope.get_imports() if i.star)) modules = follow_imports(evaluator, (i for i in scope.get_imports() if i.star))

View File

@@ -48,6 +48,7 @@ from jedi.evaluate import docstrings
from jedi.evaluate import helpers from jedi.evaluate import helpers
from jedi.evaluate import param from jedi.evaluate import param
from jedi.evaluate import flow_analysis from jedi.evaluate import flow_analysis
from jedi.evaluate import imports
def wrap(evaluator, element): def wrap(evaluator, element):
@@ -714,11 +715,22 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module, Wrapper)):
def scope_names_generator(self, position=None): def scope_names_generator(self, position=None):
yield self, pr.filter_after_position(self._module.get_defined_names(), position) yield self, pr.filter_after_position(self._module.get_defined_names(), position)
yield self, self._module_attributes() yield self, self._module_attributes()
for star_module in self.star_imports():
yield self, star_module.get_defined_names()
yield self, self.base.global_names yield self, self.base.global_names
sub_modules = self._sub_modules() sub_modules = self._sub_modules()
if sub_modules: if sub_modules:
yield self, self._sub_modules() yield self, self._sub_modules()
@underscore_memoization
def star_imports(self):
modules = []
for i in self.base.imports:
if i.is_star_import():
name = i.star_import_name()
modules += imports.ImportWrapper(self._evaluator, name).follow()
return modules
@memoize_default() @memoize_default()
def _module_attributes(self): def _module_attributes(self):
def parent_callback(): def parent_callback():

View File

@@ -483,11 +483,10 @@ class Scope(Simple, DocstringMixin):
:param start_pos: The position (line and column) of the scope. :param start_pos: The position (line and column) of the scope.
:type start_pos: tuple(int, int) :type start_pos: tuple(int, int)
""" """
__slots__ = ('imports', '_doc_token', 'asserts', 'names_dict') __slots__ = ('_doc_token', 'asserts', 'names_dict')
def __init__(self, children): def __init__(self, children):
super(Scope, self).__init__(children) super(Scope, self).__init__(children)
self.imports = []
self._doc_token = None self._doc_token = None
self.asserts = [] self.asserts = []
@@ -505,6 +504,10 @@ class Scope(Simple, DocstringMixin):
def flows(self): def flows(self):
return self._search_in_scope(Flow) return self._search_in_scope(Flow)
@property
def imports(self):
return self._search_in_scope(Import)
def _search_in_scope(self, typ): def _search_in_scope(self, typ):
def scan(children): def scan(children):
elements = [] elements = []
@@ -529,6 +532,8 @@ class Scope(Simple, DocstringMixin):
def get_imports(self): def get_imports(self):
""" Gets also the imports within flow statements """ """ Gets also the imports within flow statements """
raise NotImplementedError
return []
i = [] + self.imports i = [] + self.imports
for s in self.statements: for s in self.statements:
if isinstance(s, Scope): if isinstance(s, Scope):
@@ -700,6 +705,10 @@ class SubModule(Scope, Module):
is a ``__future__`` import. is a ``__future__`` import.
""" """
for imp in self.imports: for imp in self.imports:
# TODO implement!
continue
if not imp.from_names or not imp.namespace_names: if not imp.from_names or not imp.namespace_names:
continue continue
@@ -1141,6 +1150,12 @@ class ImportFrom(Import):
else: else:
yield as_name.children[::2] # yields x, y -> ``x as y`` yield as_name.children[::2] # yields x, y -> ``x as y``
def star_import_name(self):
"""
The last name defined in a star import.
"""
return self._paths()[-1][-1]
def _paths(self): def _paths(self):
for n in self.children[1:]: for n in self.children[1:]:
if n not in ('.', '...'): if n not in ('.', '...'):
@@ -1151,6 +1166,9 @@ class ImportFrom(Import):
dotted = [] dotted = []
else: # from x import else: # from x import
dotted = [n] dotted = [n]
if self.children[-1] == '*':
return [dotted]
return [dotted + [name] for name, alias in self._as_name_tuples()] return [dotted + [name] for name, alias in self._as_name_tuples()]