replace get_defined_names with scope_names_generator in a lot of places (the cleanup still needs to be done, though).

This commit is contained in:
Dave Halter
2014-07-02 16:12:49 +02:00
parent 749d0121fc
commit 13ada3154b
5 changed files with 39 additions and 15 deletions

View File

@@ -18,12 +18,16 @@ class InterpreterNamespace(pr.Module):
self.parser_module = parser_module self.parser_module = parser_module
self._evaluator = evaluator self._evaluator = evaluator
@underscore_memoization
def get_defined_names(self): def get_defined_names(self):
for name in self.parser_module.get_defined_names(): for name in self.parser_module.get_defined_names():
yield name yield name
for key, value in self.namespace.items(): for key, value in self.namespace.items():
yield LazyName(self._evaluator, key, value) yield LazyName(self._evaluator, key, value)
def scope_names_generator(self, position=None):
yield self, list(self.get_defined_names())
def __getattr__(self, name): def __getattr__(self, name):
return getattr(self.parser_module, name) return getattr(self.parser_module, name)

View File

@@ -88,6 +88,9 @@ class CompiledObject(Base):
else: else:
return type_names + self.instance_names() return type_names + self.instance_names()
def scope_names_generator(self, position=None):
yield self, self.get_defined_names()
@underscore_memoization @underscore_memoization
def instance_names(self): def instance_names(self):
names = [] names = []

View File

@@ -435,9 +435,7 @@ def _get_defined_names_for_position(scope, position=None, start_scope=None):
names = scope.get_defined_names() names = scope.get_defined_names()
# Instances have special rules, always return all the possible completions, # Instances have special rules, always return all the possible completions,
# because class variables are always valid and the `self.` variables, too. # because class variables are always valid and the `self.` variables, too.
if not position or isinstance(scope, (iterable.Array, er.Instance, compiled.CompiledObject)) \ if not position or isinstance(scope, (iterable.Array, er.Instance, compiled.CompiledObject)):
or start_scope != scope \
and isinstance(start_scope, (pr.Function, er.FunctionExecution)):
return names return names
names_new = [] names_new = []
for n in names: for n in names:
@@ -518,6 +516,10 @@ def get_names_of_scope(evaluator, scope, position=None, star_search=True, includ
try: try:
if isinstance(scope, (pr.SubModule, fast.Module)): if isinstance(scope, (pr.SubModule, fast.Module)):
scope = er.ModuleWrapper(evaluator, scope) scope = er.ModuleWrapper(evaluator, scope)
for g in scope.scope_names_generator(position):
yield g
"""
try: try:
sng = scope.scope_names_generator sng = scope.scope_names_generator
except AttributeError: except AttributeError:
@@ -525,6 +527,7 @@ def get_names_of_scope(evaluator, scope, position=None, star_search=True, includ
else: else:
for g in sng(position): for g in sng(position):
yield g yield g
"""
except StopIteration: except StopIteration:
reraise(common.MultiLevelStopIteration, sys.exc_info()[2]) reraise(common.MultiLevelStopIteration, sys.exc_info()[2])
if scope.isinstance(pr.ListComprehension): if scope.isinstance(pr.ListComprehension):

View File

@@ -315,6 +315,10 @@ class Class(use_metaclass(CachedMetaClass, pr.IsScope)):
type_cls = self._evaluator.find_types(compiled.builtin, 'type')[0] type_cls = self._evaluator.find_types(compiled.builtin, 'type')[0]
return result + list(type_cls.get_defined_names()) return result + list(type_cls.get_defined_names())
def scope_names_generator(self, position=None):
yield self, self.instance_names()
yield self, compiled.type_names
def get_subscope_by_name(self, name): def get_subscope_by_name(self, name):
for s in [self] + self.get_super_classes(): for s in [self] + self.get_super_classes():
for sub in reversed(s.subscopes): for sub in reversed(s.subscopes):
@@ -468,6 +472,10 @@ class FunctionExecution(Executable):
""" """
return self._get_params() + pr.Scope.get_defined_names(self) return self._get_params() + pr.Scope.get_defined_names(self)
def scope_names_generator(self, position=None):
names = pr.filter_after_position(pr.Scope.get_defined_names(self), position)
yield self, self._get_params() + names
def _copy_properties(self, prop): def _copy_properties(self, prop):
""" """
Literally copies a property of a Function. Copying is very expensive, Literally copies a property of a Function. Copying is very expensive,
@@ -541,7 +549,7 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module)):
self._module = module self._module = module
def scope_names_generator(self, position=None): def scope_names_generator(self, position=None):
yield self, filter_after_position(self.get_defined_names(), position) yield self, pr.filter_after_position(self.get_defined_names(), position)
yield self, self.module_attributes() yield self, self.module_attributes()
sub_modules = self._sub_modules() sub_modules = self._sub_modules()
if sub_modules: if sub_modules:
@@ -577,14 +585,3 @@ class ModuleWrapper(use_metaclass(CachedMetaClass, pr.Module)):
def __repr__(self): def __repr__(self):
return "<%s: %s>" % (type(self).__name__, self._module) return "<%s: %s>" % (type(self).__name__, self._module)
def filter_after_position(names, position):
if position is None:
return names
names_new = []
for n in names:
if n.start_pos[0] is not None and n.start_pos < position:
names_new.append(n)
return names_new

View File

@@ -49,6 +49,17 @@ from jedi.parser import tokenize
SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns' SCOPE_CONTENTS = 'asserts', 'subscopes', 'imports', 'statements', 'returns'
def filter_after_position(names, position):
if position is None:
return names
names_new = []
for n in names:
if n.start_pos[0] is not None and n.start_pos < position:
names_new.append(n)
return names_new
class GetCodeState(object): class GetCodeState(object):
"""A helper class for passing the state of get_code in a thread-safe """A helper class for passing the state of get_code in a thread-safe
manner.""" manner."""
@@ -499,6 +510,9 @@ class Class(Scope):
sub.get_call_signature(funcname=self.name.names[-1]), docstr) sub.get_call_signature(funcname=self.name.names[-1]), docstr)
return docstr return docstr
def scope_names_generator(self, position=None):
yield self, filter_after_position(self.get_defined_names(), position)
class Function(Scope): class Function(Scope):
""" """
@@ -545,6 +559,9 @@ class Function(Scope):
debug.warning("multiple names in param %s", n) debug.warning("multiple names in param %s", n)
return n return n
def scope_names_generator(self, position=None):
yield self, filter_after_position(self.get_defined_names(), position)
def get_call_signature(self, width=72, funcname=None): def get_call_signature(self, width=72, funcname=None):
""" """
Generate call signature of this function. Generate call signature of this function.