Fix adding error indents/dedents only at the right places

This commit is contained in:
Dave Halter
2020-03-27 17:05:05 +01:00
parent 3d95b65b21
commit 29e3545241
2 changed files with 39 additions and 16 deletions
+20 -15
View File
@@ -534,27 +534,30 @@ class _NodesTree(object):
node_indentation = tree_node.children[1].start_pos[1] node_indentation = tree_node.children[1].start_pos[1]
if indentation > node_indentation: if indentation > node_indentation:
if previous_node is not None: latest_indentation = node.get_latest_indentation()
# This means that it was not dedented enough. if indentation != latest_indentation:
node = previous_node if previous_node is None:
add_error_leaf = 'ERROR_DEDENT' add_error_leaf = 'INDENT'
break else:
else: # This means that it was not dedented enough.
add_error_leaf = 'INDENT' node = previous_node
add_error_leaf = 'ERROR_DEDENT'
break
if indentation >= node_indentation: # Not a Dedent if indentation >= node_indentation: # Not a Dedent
# We might be at the most outer layer: modules. We # We might be at the most outer layer: modules. We
# don't want to depend on the first statement # don't want to depend on the first statement
# having the right indentation. # having the right indentation.
break break
elif tree_node.type == 'file_input': elif tree_node.type == 'file_input':
latest_indentation = node.get_latest_indentation() if indentation > 0:
if indentation > 0 and indentation != latest_indentation: latest_indentation = node.get_latest_indentation()
if previous_node is None and indentation > latest_indentation: if indentation != latest_indentation:
add_error_leaf = 'INDENT' if previous_node is None and indentation > latest_indentation:
else: add_error_leaf = 'INDENT'
if previous_node is not None: else:
node = previous_node if previous_node is not None:
add_error_leaf = 'ERROR_DEDENT' node = previous_node
add_error_leaf = 'ERROR_DEDENT'
break break
previous_node = node previous_node = node
@@ -635,6 +638,7 @@ class _NodesTree(object):
tree_nodes = tree_nodes[:i] tree_nodes = tree_nodes[:i]
old_working_stack = list(self._working_stack) old_working_stack = list(self._working_stack)
old_prefix = self.prefix
add_error_leaf, _ = self._get_insertion_node(tree_nodes[0]) add_error_leaf, _ = self._get_insertion_node(tree_nodes[0])
new_nodes, self._working_stack, self.prefix = self._copy_nodes( new_nodes, self._working_stack, self.prefix = self._copy_nodes(
@@ -647,6 +651,7 @@ class _NodesTree(object):
) )
if not new_nodes: if not new_nodes:
self._working_stack = old_working_stack self._working_stack = old_working_stack
self.prefix = old_prefix
return new_nodes return new_nodes
def _copy_nodes(self, working_stack, nodes, until_line, line_offset, def _copy_nodes(self, working_stack, nodes, until_line, line_offset,
+19 -1
View File
@@ -54,7 +54,7 @@ def _assert_nodes_are_equal(node1, node2):
children2 = node2.children children2 = node2.children
except AttributeError: except AttributeError:
assert False, (node1, node2) assert False, (node1, node2)
assert len(children1) == len(children2), (children1, children2) assert len(children1) == len(children2)
for n1, n2 in zip(children1, children2): for n1, n2 in zip(children1, children2):
_assert_nodes_are_equal(n1, n2) _assert_nodes_are_equal(n1, n2)
@@ -1334,3 +1334,21 @@ def test_parent_on_decorator(differ):
cls = module_node.children[0] cls = module_node.children[0]
cls_suite = cls.children[-1] cls_suite = cls.children[-1]
assert len(cls_suite.children) == 3 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)