Diff parser: Make sure to pop nodes directly after error nodes, see also davidhalter/jedi#1499

This commit is contained in:
Dave Halter
2020-03-22 14:49:22 +01:00
parent bd37353042
commit 237dc9e135
2 changed files with 31 additions and 18 deletions

View File

@@ -638,6 +638,7 @@ class _NodesTree(object):
tos = working_stack[-1] tos = working_stack[-1]
last_node = new_nodes[-1] last_node = new_nodes[-1]
had_valid_suite_last = False had_valid_suite_last = False
# Pop incomplete suites from the list
if _func_or_class_has_suite(last_node): if _func_or_class_has_suite(last_node):
suite = last_node suite = last_node
while suite.type != 'suite': while suite.type != 'suite':
@@ -659,24 +660,36 @@ class _NodesTree(object):
working_stack = new_working_stack working_stack = new_working_stack
had_valid_suite_last = True had_valid_suite_last = True
# Pop error nodes at the end from the list
if new_nodes: if new_nodes:
last_node = new_nodes[-1] while new_nodes:
if (last_node.type in ('error_leaf', 'error_node') or last_node = new_nodes[-1]
_is_flow_node(new_nodes[-1])): if (last_node.type in ('error_leaf', 'error_node')
# Error leafs/nodes don't have a defined start/end. Error or _is_flow_node(new_nodes[-1])):
# nodes might not end with a newline (e.g. if there's an # Error leafs/nodes don't have a defined start/end. Error
# open `(`). Therefore ignore all of them unless they are # nodes might not end with a newline (e.g. if there's an
# succeeded with valid parser state. # open `(`). Therefore ignore all of them unless they are
# If we copy flows at the end, they might be continued # succeeded with valid parser state.
# after the copy limit (in the new parser). # If we copy flows at the end, they might be continued
# In this while loop we try to remove until we find a newline. # after the copy limit (in the new parser).
new_prefix = '' # In this while loop we try to remove until we find a newline.
new_nodes.pop() new_prefix = ''
while new_nodes:
last_node = new_nodes[-1]
if last_node.get_last_leaf().type == 'newline':
break
new_nodes.pop() new_nodes.pop()
while new_nodes:
last_node = new_nodes[-1]
if last_node.get_last_leaf().type == 'newline':
break
new_nodes.pop()
continue
if len(new_nodes) > 1 and new_nodes[-2].type == 'error_node':
# The problem here is that Jedi error recovery sometimes
# will mark last node.
# Since the new last node is an error node this will get
# cleaned up in the next while iteration.
new_nodes.pop()
continue
break
if new_nodes: if new_nodes:
if not _ends_with_newline(new_nodes[-1].get_last_leaf()) and not had_valid_suite_last: if not _ends_with_newline(new_nodes[-1].get_last_leaf()) and not had_valid_suite_last:

View File

@@ -1260,8 +1260,8 @@ def test_open_bracket_case2(differ):
d d
''') ''')
differ.initialize(code1) differ.initialize(code1)
differ.parse(code2, copies=1, parsers=2, expect_error_leaves=True) differ.parse(code2, copies=0, parsers=1, expect_error_leaves=True)
differ.parse(code1, copies=2, parsers=0, expect_error_leaves=True) differ.parse(code1, copies=0, parsers=1, expect_error_leaves=True)
def test_some_weird_removals(differ): def test_some_weird_removals(differ):