From 29e3545241e137e4272177a269763f6a3a440716 Mon Sep 17 00:00:00 2001 From: Dave Halter Date: Fri, 27 Mar 2020 17:05:05 +0100 Subject: [PATCH] Fix adding error indents/dedents only at the right places --- parso/python/diff.py | 35 ++++++++++++++++++++--------------- test/test_diff_parser.py | 20 +++++++++++++++++++- 2 files changed, 39 insertions(+), 16 deletions(-) diff --git a/parso/python/diff.py b/parso/python/diff.py index 8787f27..400e9aa 100644 --- a/parso/python/diff.py +++ b/parso/python/diff.py @@ -534,27 +534,30 @@ class _NodesTree(object): node_indentation = tree_node.children[1].start_pos[1] if indentation > node_indentation: - if previous_node is not None: - # This means that it was not dedented enough. - node = previous_node - add_error_leaf = 'ERROR_DEDENT' - break - else: - add_error_leaf = 'INDENT' + latest_indentation = node.get_latest_indentation() + if indentation != latest_indentation: + if previous_node is None: + add_error_leaf = 'INDENT' + else: + # This means that it was not dedented enough. + node = previous_node + add_error_leaf = 'ERROR_DEDENT' + break if indentation >= node_indentation: # Not a Dedent # We might be at the most outer layer: modules. We # don't want to depend on the first statement # having the right indentation. break elif tree_node.type == 'file_input': - latest_indentation = node.get_latest_indentation() - if indentation > 0 and indentation != latest_indentation: - if previous_node is None and indentation > latest_indentation: - add_error_leaf = 'INDENT' - else: - if previous_node is not None: - node = previous_node - add_error_leaf = 'ERROR_DEDENT' + if indentation > 0: + latest_indentation = node.get_latest_indentation() + if indentation != latest_indentation: + if previous_node is None and indentation > latest_indentation: + add_error_leaf = 'INDENT' + else: + if previous_node is not None: + node = previous_node + add_error_leaf = 'ERROR_DEDENT' break previous_node = node @@ -635,6 +638,7 @@ class _NodesTree(object): tree_nodes = tree_nodes[:i] old_working_stack = list(self._working_stack) + old_prefix = self.prefix add_error_leaf, _ = self._get_insertion_node(tree_nodes[0]) new_nodes, self._working_stack, self.prefix = self._copy_nodes( @@ -647,6 +651,7 @@ class _NodesTree(object): ) if not new_nodes: self._working_stack = old_working_stack + self.prefix = old_prefix return new_nodes def _copy_nodes(self, working_stack, nodes, until_line, line_offset, diff --git a/test/test_diff_parser.py b/test/test_diff_parser.py index b7f03b2..4f4bc1c 100644 --- a/test/test_diff_parser.py +++ b/test/test_diff_parser.py @@ -54,7 +54,7 @@ def _assert_nodes_are_equal(node1, node2): children2 = node2.children except AttributeError: assert False, (node1, node2) - assert len(children1) == len(children2), (children1, children2) + assert len(children1) == len(children2) for n1, n2 in zip(children1, children2): _assert_nodes_are_equal(n1, n2) @@ -1334,3 +1334,21 @@ def test_parent_on_decorator(differ): cls = module_node.children[0] cls_suite = cls.children[-1] assert len(cls_suite.children) == 3 + + +def test_wrong_indent_in_def(differ): + code1 = dedent('''\ + def x(): + a + b + ''') + + code2 = dedent('''\ + def x(): + // + b + c + ''') + differ.initialize(code1) + differ.parse(code2, copies=1, parsers=2, expect_error_leaves=True) + differ.parse(code1, parsers=2)