From e412694fa2ebe115e57f9b882e9c44e84a27516c Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Wed, 28 Jan 2015 17:06:18 +0100 Subject: [PATCH] Fix issues with flows in the fast parser. --- jedi/parser/fast.py | 22 +++++++++++++--------- test/test_parser/test_fast_parser.py | 11 +++++++++-- 2 files changed, 22 insertions(+), 11 deletions(-) diff --git a/jedi/parser/fast.py b/jedi/parser/fast.py index 2c63e4e1..f7e17a43 100644 --- a/jedi/parser/fast.py +++ b/jedi/parser/fast.py @@ -174,15 +174,13 @@ class ParserNode(object): nodes should be added anymore. """ print('CLOSE NODE', id(self), self.parent, self._node_children) - if self.parser: print(self.parser.module.names_dict, [p.parser.module.names_dict for p in + print(self.parser.module.names_dict, [p.parser.module.names_dict for p in self._node_children]) # We only need to replace the dict if multiple dictionaries are used: if self._node_children: dcts = [n.parser.module.names_dict for n in self._node_children] - if self.parser is not None: - # The first Parser node contains all the others and is - # typically empty. - dcts.insert(0, self._content_scope.names_dict) + # Need to insert the own node as well. + dcts.insert(0, self._content_scope.names_dict) print('DCTS', self.parser, dcts, self._node_children) self._content_scope.names_dict = MergedNamesDict(dcts) @@ -512,6 +510,7 @@ class FastTokenizer(object): self._indent_counter = 0 self._flow_indent_counter = 0 self._returned_endmarker = False + self._next_dedent_noclose = False def __iter__(self): return self @@ -541,14 +540,19 @@ class FastTokenizer(object): self._indent_counter += 1 elif typ == DEDENT: self._indent_counter -= 1 + print(self._flow_indent_counter) if self._in_flow and self._indent_counter == self._flow_indent_counter: self._in_flow = False + self._next_dedent_noclose = True return current - if self.previous[0] == DEDENT and self._indent_counter == 0: - self._first_stmt = False - return self._close() - elif self.previous[0] in (NEWLINE, INDENT): + if self.previous[0] in (NEWLINE, INDENT, DEDENT): + if self.previous[0] == DEDENT: + if not self._next_dedent_noclose: + self._first_stmt = False + return self._close() + if not self._in_flow: + self._next_dedent_noclose = False # Check for NEWLINE, which symbolizes the indent. #print('X', repr(value), tokenize.tok_name[typ]) indent = start_pos[1] diff --git a/test/test_parser/test_fast_parser.py b/test/test_parser/test_fast_parser.py index 60fdc129..36ebb4f0 100644 --- a/test/test_parser/test_fast_parser.py +++ b/test/test_parser/test_fast_parser.py @@ -106,8 +106,6 @@ def test_positions(): def test_if(): - # Empty the parser cache for the path None. - cache.parser_cache.pop(None, None) src = dedent('''\ def func(): x = 3 @@ -124,6 +122,15 @@ def test_if(): assert [d.name for d in jedi.Script(src, 8, 6).goto_definitions()] == ['int'] +def test_if_simple(): + src = dedent('''\ + if 1: + a = 3 + ''') + check_fp(src + 'a', 1) + check_fp(src + "else:\n a = ''\na", 1) + + def test_incomplete_function(): source = '''return ImportErr'''