mirror of
https://github.com/davidhalter/parso.git
synced 2025-12-16 01:17:13 +08:00
Fix adding error indents/dedents only at the right places
This commit is contained in:
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user