# HG changeset patch # User Mads Kiilerich # Date 1470964439 -7200 # Node ID 6b723a49a9a1f9590e8585f6c418eb25f3ee0fe5 # Parent 59a38ec5ad8bfdd5090e6560f684d6e58c790c36 diff: only highlight of difference between del and add line for one-liners Comparing a single del line with the first of several add lines would often be quite arbitrary and misleading. Note: the non-gitdiff code paths in diffs.py are incomplete and unused and unusable. diff -r 59a38ec5ad8b -r 6b723a49a9a1 kallithea/lib/diffs.py --- a/kallithea/lib/diffs.py Fri Aug 12 03:04:48 2016 +0200 +++ b/kallithea/lib/diffs.py Fri Aug 12 03:13:59 2016 +0200 @@ -283,15 +283,13 @@ self.removes += 1 return safe_unicode(l) - def _highlight_line_difflib(self, line, next_): + def _highlight_line_difflib(self, old, new): """ Highlight inline changes in both lines. """ - if line['action'] == 'del': - old, new = line, next_ - else: - old, new = next_, line + assert old['action'] == 'del' + assert new['action'] == 'add' oldwords = self._token_re.split(old['line']) newwords = self._token_re.split(new['line']) @@ -484,19 +482,34 @@ if not inline_diff: return diff_container(_files) - # highlight inline changes + # highlight inline changes when one del is followed by one add for diff_data in _files: for chunk in diff_data['chunks']: lineiter = iter(chunk) try: - while 1: - line = lineiter.next() - if line['action'] not in ['unmod', 'context']: - nextline = lineiter.next() - if nextline['action'] in ['unmod', 'context'] or \ - nextline['action'] == line['action']: - continue - self.differ(line, nextline) + peekline = lineiter.next() + while True: + # find a first del line + while peekline['action'] != 'del': + peekline = lineiter.next() + delline = peekline + peekline = lineiter.next() + # if not followed by add, eat all following del lines + if peekline['action'] != 'add': + while peekline['action'] == 'del': + peekline = lineiter.next() + continue + # found an add - make sure it is the only one + addline = peekline + try: + peekline = lineiter.next() + except StopIteration: + # add was last line - ok + self.differ(delline, addline) + raise + if peekline['action'] != 'add': + # there was only one add line - ok + self.differ(delline, addline) except StopIteration: pass diff -r 59a38ec5ad8b -r 6b723a49a9a1 kallithea/tests/fixtures/markuptest.diff --- a/kallithea/tests/fixtures/markuptest.diff Fri Aug 12 03:04:48 2016 +0200 +++ b/kallithea/tests/fixtures/markuptest.diff Fri Aug 12 03:13:59 2016 +0200 @@ -1,7 +1,7 @@ diff --git a/f b/f --- a/f +++ b/f -@@ -51,5 +51,12 @@ +@@ -51,6 +51,13 @@ begin(); + int foo; @@ -15,3 +15,5 @@ + + #define MAX_STEPS (64) +- #define MIN_STEPS (48) ++ #define MIN_STEPS (42) diff -r 59a38ec5ad8b -r 6b723a49a9a1 kallithea/tests/models/test_diff_parsers.py --- a/kallithea/tests/models/test_diff_parsers.py Fri Aug 12 03:04:48 2016 +0200 +++ b/kallithea/tests/models/test_diff_parsers.py Fri Aug 12 03:13:59 2016 +0200 @@ -298,7 +298,7 @@ s = ''.join(l) print s assert s == r''' -context ... ... u'@@ -51,5 +51,12 @@\n' +context ... ... u'@@ -51,6 +51,13 @@\n' unmod 51 51 u'\tbegin();\n' unmod 52 52 u'\t\n' add 53 u'\tint foo;\n' @@ -308,8 +308,10 @@ add 57 u'\tint tab;\t\n' add 58 u'\t\n' unmod 59 53 u' ' -del 54 u'\t#define MAX_STEPS (48)\n' -add 60 u'\t\n' +del 54 u'\t#define MAX_STEPS (48)\n' +add 60 u'\t\n' add 61 u'\t#define MAX_STEPS (64)\n' unmod 62 55 u'\n' +del 56 u'\t#define MIN_STEPS (48)\n' +add 63 u'\t#define MIN_STEPS (42)\n' '''