From d5fbc006e2afb674347eb5f1280952081420a4f3 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 24 Sep 2014 12:44:24 +0200 Subject: [PATCH] Add a names_dict to scopes. This is good for the future parser and now useful to process self.foo and other stuff. --- jedi/evaluate/representation.py | 7 ++++++- jedi/parser/__init__.py | 3 +++ jedi/parser/representation.py | 21 ++++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/jedi/evaluate/representation.py b/jedi/evaluate/representation.py index abf0c12a..162ba092 100644 --- a/jedi/evaluate/representation.py +++ b/jedi/evaluate/representation.py @@ -164,6 +164,7 @@ class Instance(use_metaclass(CachedMetaClass, Executed)): # because to follow them and their self variables is too # complicated. sub = self._get_method_execution(sub) + print(sub.get_names_dict()) for n in sub.get_defined_names(): # Only names with the selfname are being added. # It is also important, that they have a len() of 2, @@ -579,6 +580,10 @@ class FunctionExecution(Executed): break return types + @underscore_memoization + def get_names_dict(self): + return self. + @memoize_default(default=()) def _get_params(self): """ @@ -631,7 +636,7 @@ class FunctionExecution(Executed): @common.safe_property @memoize_default([]) def returns(self): - return self._copy_list('returns') + return self._copy_list(self) @common.safe_property @memoize_default([]) diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index 101e315c..87d33f0d 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -100,6 +100,9 @@ class Parser(object): except KeyError: self.module.used_names[tok_name] = set([simple]) self.module.temp_used_names = [] + if isinstance(simple, pr.Statement): + for name, call in simple.get_names_dict().items(): + self._scope.add_name_call(name, call) def _parse_dotted_name(self, pre_used_token=None): """ diff --git a/jedi/parser/representation.py b/jedi/parser/representation.py index f587a247..58030d2e 100644 --- a/jedi/parser/representation.py +++ b/jedi/parser/representation.py @@ -244,7 +244,7 @@ class Scope(Simple, DocstringMixin): :type start_pos: tuple(int, int) """ __slots__ = ('subscopes', 'imports', 'statements', '_doc_token', 'asserts', - 'returns', 'is_generator') + 'returns', 'is_generator', '_names_dict') def __init__(self, module, start_pos): super(Scope, self).__init__(module, start_pos) @@ -256,11 +256,19 @@ class Scope(Simple, DocstringMixin): # Needed here for fast_parser, because the fast_parser splits and # returns will be in "normal" modules. self.returns = [] + self._names_dict = defaultdict(lambda: []) self.is_generator = False def is_scope(self): return True + def add_name_call(self, name, call): + """Add a name to the names_dict.""" + self._names_dict[name].append(call) + + def get_names_dict(self): + return self._names_dict + def add_scope(self, sub, decorators): sub.parent = self.use_as_parent sub.decorators = decorators @@ -672,6 +680,10 @@ class Flow(Scope): s.parent = self.use_as_parent self.set_vars = [] + def add_name_call(self, name, call): + """Add a name to the names_dict.""" + self.parent.add_name_call(name, call) + @property def parent(self): return self._parent @@ -996,12 +1008,11 @@ class Statement(Simple, DocstringMixin): c = call # Check if there's an execution in it, if so this is # not a set_var. - while c: - if isinstance(c.next, Array): + while True: + if c.next is None or isinstance(c.next, Array): break c = c.next - else: - dct[unicode(c.name)] = call + dct[unicode(c.name)] = call for calls, operation in self.assignment_details: search_calls(calls)