Fix in diff parser: prefix calculation was wrong when copying nodes

This commit is contained in:
Dave Halter
2019-01-14 01:00:17 +01:00
parent 564be7882e
commit d3d28480ed
2 changed files with 21 additions and 3 deletions

View File

@@ -476,8 +476,6 @@ class _NodesTreeNode(object):
class _NodesTree(object): class _NodesTree(object):
endmarker_type = 'endmarker'
def __init__(self, module): def __init__(self, module):
self._base_node = _NodesTreeNode(module) self._base_node = _NodesTreeNode(module)
self._working_stack = [self._base_node] self._working_stack = [self._base_node]
@@ -543,7 +541,7 @@ class _NodesTree(object):
Helps cleaning up the tree nodes that get inserted. Helps cleaning up the tree nodes that get inserted.
""" """
last_leaf = tree_nodes[-1].get_last_leaf() last_leaf = tree_nodes[-1].get_last_leaf()
is_endmarker = last_leaf.type == self.endmarker_type is_endmarker = last_leaf.type == 'endmarker'
self._prefix_remainder = '' self._prefix_remainder = ''
if is_endmarker: if is_endmarker:
separation = max(last_leaf.prefix.rfind('\n'), last_leaf.prefix.rfind('\r')) separation = max(last_leaf.prefix.rfind('\n'), last_leaf.prefix.rfind('\r'))
@@ -632,6 +630,7 @@ class _NodesTree(object):
if len(suite_nodes) < 2: if len(suite_nodes) < 2:
# A suite only with newline is not valid. # A suite only with newline is not valid.
new_nodes.pop() new_nodes.pop()
new_prefix = ''
else: else:
assert new_nodes assert new_nodes
tos.add_child_node(suite_tos) tos.add_child_node(suite_tos)
@@ -649,6 +648,7 @@ class _NodesTree(object):
# If we copy flows at the end, they might be continued # If we copy flows at the end, they might be continued
# after the copy limit (in the new parser). # after the copy limit (in the new parser).
# In this while loop we try to remove until we find a newline. # In this while loop we try to remove until we find a newline.
new_prefix = ''
new_nodes.pop() new_nodes.pop()
while new_nodes: while new_nodes:
last_node = new_nodes[-1] last_node = new_nodes[-1]

View File

@@ -49,6 +49,8 @@ class Differ(object):
self.lines = split_lines(code, keepends=True) self.lines = split_lines(code, keepends=True)
self.module = parse(code, diff_cache=True, cache=True) self.module = parse(code, diff_cache=True, cache=True)
assert code == self.module.get_code()
_assert_valid_graph(self.module)
return self.module return self.module
def parse(self, code, copies=0, parsers=0, expect_error_leaves=False): def parse(self, code, copies=0, parsers=0, expect_error_leaves=False):
@@ -1034,3 +1036,19 @@ def test_random_character_insertion(differ):
differ.initialize(code1) differ.initialize(code1)
differ.parse(code2, copies=1, parsers=3, expect_error_leaves=True) differ.parse(code2, copies=1, parsers=3, expect_error_leaves=True)
differ.parse(code1, copies=1, parsers=1) differ.parse(code1, copies=1, parsers=1)
def test_import_opening_bracket(differ):
code1 = dedent('''\
1
2
from bubu import (X,
''')
code2 = dedent('''\
11
2
from bubu import (X,
''')
differ.initialize(code1)
differ.parse(code2, copies=1, parsers=2, expect_error_leaves=True)
differ.parse(code1, copies=1, parsers=2, expect_error_leaves=True)