Compare books: Fix non-optimal display of changes in some situations

Extra changes were being displayed because the patience diff algorithm
can return large replace blocks with identical leading lines. Instead of
showing them as diffs, show them as extra context.
This commit is contained in:
Kovid Goyal 2014-02-06 11:21:43 +05:30
parent 91a229ae38
commit 4175c1cf7f

View File

@ -750,11 +750,29 @@ class DiffSplit(QSplitter): # {{{
self.changes.append(Change( self.changes.append(Change(
rtop=start_block, rbot=current_block, ltop=l, lbot=l, kind='insert')) rtop=start_block, rbot=current_block, ltop=l, lbot=l, kind='insert'))
def trim_identical_leading_lines(self, alo, ahi, blo, bhi):
''' The patience diff algorithm sometimes results in a block of replace
lines with identical leading lines. Remove these. This can cause extra
lines of context, but that is better than having extra lines of diff
with no actual changes. '''
a, b = self.left_lines, self.right_lines
leading = 0
while alo < ahi and blo < bhi and a[alo] == b[blo]:
leading += 1
alo += 1
blo += 1
if leading > 0:
self.equal(alo - leading, alo, blo - leading, blo)
return alo, ahi, blo, bhi
def replace(self, alo, ahi, blo, bhi): def replace(self, alo, ahi, blo, bhi):
''' When replacing one block of lines with another, search the blocks ''' When replacing one block of lines with another, search the blocks
for *similar* lines; the best-matching pair (if any) is used as a synch for *similar* lines; the best-matching pair (if any) is used as a synch
point, and intraline difference marking is done on the similar pair. point, and intraline difference marking is done on the similar pair.
Lots of work, but often worth it. ''' Lots of work, but often worth it. '''
alo, ahi, blo, bhi = self.trim_identical_leading_lines(alo, ahi, blo, bhi)
if alo == ahi and blo == bhi:
return
if ahi + bhi - alo - blo > 100: if ahi + bhi - alo - blo > 100:
# Too many lines, this will be too slow # Too many lines, this will be too slow
# http://bugs.python.org/issue6931 # http://bugs.python.org/issue6931