From 5c98f6cf04afa152ae86755aaf2ba12d255a5550 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Mon, 12 Jan 2015 01:11:46 +0100 Subject: [PATCH] Suites don't have to contain statements anymore, this makes autocompletion better in certain cases. --- jedi/evaluate/representation.py | 1 + jedi/parser/__init__.py | 36 +++++++++++---------------------- 2 files changed, 13 insertions(+), 24 deletions(-) diff --git a/jedi/evaluate/representation.py b/jedi/evaluate/representation.py index 8b921ab5..8c54028a 100644 --- a/jedi/evaluate/representation.py +++ b/jedi/evaluate/representation.py @@ -431,6 +431,7 @@ class Class(use_metaclass(CachedMetaClass, Wrapper)): yield self.names_dict else: for scope in self.py__mro__(self._evaluator): + print(scope.names_dict) if isinstance(scope, compiled.CompiledObject): yield scope.names_dicts(False, is_instance)[0] else: diff --git a/jedi/parser/__init__.py b/jedi/parser/__init__.py index f361d4a8..4a5cad4c 100644 --- a/jedi/parser/__init__.py +++ b/jedi/parser/__init__.py @@ -107,6 +107,7 @@ class Parser(object): self.global_names = [] + print('start') # TODO do print absolute import detection here. #try: # del python_grammar_no_print_statement.keywords["print"] @@ -233,19 +234,17 @@ class Parser(object): if value in ('import', 'from', 'class', 'def', 'try', 'while', 'return'): # Those can always be new statements. add_token_callback(typ, value, prefix, start_pos) - elif typ == token.DEDENT: - if symbol == 'suite': - # If a function or anything else contains a suite that is - # "empty" (just NEWLINE/INDENT), we remove it. If it's not - # empty, we can close it. - if len(nodes) > 2: - # Finish the suite. - add_token_callback(typ, value, prefix, start_pos) - else: - # Remove the current suite from the stack to look deeper in - # the stack for a suite/file_input. - index, symbol, nodes = current_suite(stack[:-1]) - self._stack_removal(grammar, stack, index + 1, value, start_pos) + elif typ == token.DEDENT and symbol == 'suite': + # Close the current suite, with DEDENT. + # Note that this may cause some suites to not contain any + # statements at all. This is contrary to valid Python syntax. We + # keep incomplete suites in Jedi to be able to complete param names + # or `with ... as foo` names. If we want to use this parser for + # syntax checks, we have to check in a separate turn if suites + # contain statements or not. However, a second check is necessary + # anyway (compile.c does that for Python), because Python's grammar + # doesn't stop you from defining `continue` in a module, etc. + add_token_callback(typ, value, prefix, start_pos) def _stack_removal(self, grammar, stack, start_index, value, start_pos): def clear_names(children): @@ -280,17 +279,6 @@ class Parser(object): stack[start_index:] = [] def _tokenize(self, tokenizer): - """ - while first_pos[1] <= self._scope.start_pos[1] \ - and (token_type == token.NAME or tok_str in ('(', '['))\ - and self._scope != self.module: - self._scope.end_pos = first_pos - self._scope = self._scope.parent - if isinstance(self._scope, pr.Module) \ - and not isinstance(self._scope, pr.SubModule): - self._scope = self.module - """ - for typ, value, start_pos, prefix in tokenizer: if typ == token.OP: typ = token.opmap[value]