Fix diff parser: get_last_line was sometimes wrong

Now the calculation is way simpler. Still annoying that it even happened.
This commit is contained in:
Dave Halter
2019-01-02 01:39:53 +01:00
parent 64cf24d9da
commit d2542983e9
2 changed files with 38 additions and 13 deletions

View File

@@ -196,7 +196,7 @@ class DiffParser(object):
LOG.debug('line_lengths old: %s; new: %s' % (len(old_lines), line_length)) LOG.debug('line_lengths old: %s; new: %s' % (len(old_lines), line_length))
for operation, i1, i2, j1, j2 in opcodes: for operation, i1, i2, j1, j2 in opcodes:
LOG.debug('code[%s] old[%s:%s] new[%s:%s]', LOG.debug('-> code[%s] old[%s:%s] new[%s:%s]',
operation, i1 + 1, i2, j1 + 1, j2) operation, i1 + 1, i2, j1 + 1, j2)
if j2 == line_length and new_lines[-1] == '': if j2 == line_length and new_lines[-1] == '':
@@ -403,7 +403,7 @@ class DiffParser(object):
class _NodesTreeNode(object): class _NodesTreeNode(object):
ChildrenGroup = namedtuple('ChildrenGroup', 'children line_offset last_line_offset_leaf') _ChildrenGroup = namedtuple('_ChildrenGroup', 'children line_offset last_line_offset_leaf')
def __init__(self, tree_node, parent=None): def __init__(self, tree_node, parent=None):
self.tree_node = tree_node self.tree_node = tree_node
@@ -433,23 +433,17 @@ class _NodesTreeNode(object):
self._node_children.append(child_node) self._node_children.append(child_node)
def add_tree_nodes(self, children, line_offset=0, last_line_offset_leaf=None): def add_tree_nodes(self, children, line_offset=0, last_line_offset_leaf=None):
group = self.ChildrenGroup(children, line_offset, last_line_offset_leaf) if last_line_offset_leaf is None:
last_line_offset_leaf = children[-1].get_last_leaf()
group = self._ChildrenGroup(children, line_offset, last_line_offset_leaf)
self._children_groups.append(group) self._children_groups.append(group)
def get_last_line(self, suffix): def get_last_line(self, suffix):
line = 0 line = 0
if self._children_groups: if self._children_groups:
children_group = self._children_groups[-1] children_group = self._children_groups[-1]
last_leaf = children_group.children[-1].get_last_leaf() last_leaf = children_group.last_line_offset_leaf
line = last_leaf.end_pos[0] line = last_leaf.end_pos[0] + children_group.line_offset
# Calculate the line offsets
offset = children_group.line_offset
if offset:
# In case the line_offset is not applied to this specific leaf,
# just ignore it.
if last_leaf.line <= children_group.last_line_offset_leaf.line:
line += children_group.line_offset
# Newlines end on the next line, which means that they would cover # Newlines end on the next line, which means that they would cover
# the next line. That line is not fully parsed at this point. # the next line. That line is not fully parsed at this point.

View File

@@ -609,3 +609,34 @@ def test_differing_docstrings(differ):
differ.initialize(code1) differ.initialize(code1)
differ.parse(code2, parsers=3, copies=1) differ.parse(code2, parsers=3, copies=1)
differ.parse(code1, parsers=3, copies=1) differ.parse(code1, parsers=3, copies=1)
def test_one_call_in_function_change(differ):
code1 = dedent('''\
def f(self):
mro = [self]
for a in something:
yield a
def g(self):
return C(
a=str,
b=self,
)
''')
code2 = dedent('''\
def f(self):
mro = [self]
def g(self):
return C(
a=str,
t
b=self,
)
''')
differ.initialize(code1)
differ.parse(code2, parsers=1, copies=1, expect_error_leaves=True)
differ.parse(code1, parsers=2, copies=1)