Ensure that neighboring changes of the same kind are merged

This commit is contained in:
Kovid Goyal 2014-01-19 20:31:09 +05:30
parent 9e68f3a488
commit 5a02780254

View File

@ -244,12 +244,25 @@ class TextDiffView(QSplitter):
c.movePosition(c.Start) c.movePosition(c.Start)
v.setTextCursor(c) v.setTextCursor(c)
self.coalesce_changes()
for ltop, lbot, rtop, rbot, kind in self.changes: for ltop, lbot, rtop, rbot, kind in self.changes:
self.left.changes.append((ltop, lbot, kind)) if kind != 'equal':
self.right.changes.append((rtop, rbot, kind)) self.left.changes.append((ltop, lbot, kind))
self.right.changes.append((rtop, rbot, kind))
self.update() self.update()
def coalesce_changes(self):
'Merge neighboring changes of the same kind, if any'
changes = []
for x in self.changes:
if changes and changes[-1].kind == x.kind:
changes[-1] = changes[-1]._replace(lbot=x.lbot, rbot=x.rbot)
else:
changes.append(x)
self.changes = changes
def do_insert(self, cursor, highlighter, line_number_map, lo, hi): def do_insert(self, cursor, highlighter, line_number_map, lo, hi):
start_block = cursor.block() start_block = cursor.block()
highlighter.copy_lines(lo, hi, cursor) highlighter.copy_lines(lo, hi, cursor)
@ -258,7 +271,10 @@ class TextDiffView(QSplitter):
return start_block.blockNumber(), cursor.block().blockNumber() return start_block.blockNumber(), cursor.block().blockNumber()
def equal(self, alo, ahi, blo, bhi): def equal(self, alo, ahi, blo, bhi):
self.left_insert(alo, ahi), self.right_insert(blo, bhi) lsb, lcb = self.left_insert(alo, ahi)
rsb, rcb = self.right_insert(blo, bhi)
self.changes.append(Change(
rtop=rsb, rbot=rcb, ltop=lsb, lbot=lcb, kind='equal'))
def delete(self, alo, ahi, blo, bhi): def delete(self, alo, ahi, blo, bhi):
start_block, current_block = self.left_insert(alo, ahi) start_block, current_block = self.left_insert(alo, ahi)