# HG changeset patch # User Mads Kiilerich # Date 1506982480 -7200 # Node ID 22074446ac5b9f8beb737e6c481c7e52a3b31b31 # Parent 182570502b6afb9b1ba22b32716a64d1e121bb5b diffs: move _highlight_inline_diff to a pure function Make it clear that it doesn't modify any state. diff -r 182570502b6a -r 22074446ac5b kallithea/lib/diffs.py --- a/kallithea/lib/diffs.py Tue Oct 03 00:14:40 2017 +0200 +++ b/kallithea/lib/diffs.py Tue Oct 03 00:14:40 2017 +0200 @@ -305,9 +305,6 @@ (?:^\+\+\+[ ](b/(?P.+?)|/dev/null)\t?(?:\n|$))? """, re.VERBOSE | re.MULTILINE) - # Used for inline highlighter word split, must match the substitutions in _escaper - _token_re = re.compile(r'()(&|<|>|\t|| |\W+?)') - _escape_re = re.compile(r'(&)|(<)|(>)|(\t)|(\r)|(?<=.)( \n| $)') def __init__(self, diff, vcs='hg', diff_limit=None, inline_diff=True): @@ -351,33 +348,6 @@ return self._escape_re.sub(substitute, safe_unicode(string)) - def _highlight_inline_diff(self, old, new): - """ - Highlight simple add/remove in two lines given as info dicts. They are - modified in place and given markup with /. - """ - assert old['action'] == 'del' - assert new['action'] == 'add' - - oldwords = self._token_re.split(old['line']) - newwords = self._token_re.split(new['line']) - sequence = difflib.SequenceMatcher(None, oldwords, newwords) - - oldfragments, newfragments = [], [] - for tag, i1, i2, j1, j2 in sequence.get_opcodes(): - oldfrag = ''.join(oldwords[i1:i2]) - newfrag = ''.join(newwords[j1:j2]) - if tag != 'equal': - if oldfrag: - oldfrag = '%s' % oldfrag - if newfrag: - newfrag = '%s' % newfrag - oldfragments.append(oldfrag) - newfragments.append(newfrag) - - old['line'] = "".join(oldfragments) - new['line'] = "".join(newfragments) - def _get_header(self, diff_chunk): """ Parses a Git diff for a single file (header and chunks) and returns a tuple with: @@ -540,11 +510,11 @@ peekline = lineiter.next() except StopIteration: # add was last line - ok - self._highlight_inline_diff(delline, addline) + _highlight_inline_diff(delline, addline) raise if peekline['action'] != 'add': # there was only one add line - ok - self._highlight_inline_diff(delline, addline) + _highlight_inline_diff(delline, addline) except StopIteration: pass @@ -652,3 +622,35 @@ Returns tuple of added, and removed lines for this instance """ return self.adds, self.removes + + +# Used for inline highlighter word split, must match the substitutions in _escaper +_token_re = re.compile(r'()(&|<|>|\t|| |\W+?)') + + +def _highlight_inline_diff(old, new): + """ + Highlight simple add/remove in two lines given as info dicts. They are + modified in place and given markup with /. + """ + assert old['action'] == 'del' + assert new['action'] == 'add' + + oldwords = _token_re.split(old['line']) + newwords = _token_re.split(new['line']) + sequence = difflib.SequenceMatcher(None, oldwords, newwords) + + oldfragments, newfragments = [], [] + for tag, i1, i2, j1, j2 in sequence.get_opcodes(): + oldfrag = ''.join(oldwords[i1:i2]) + newfrag = ''.join(newwords[j1:j2]) + if tag != 'equal': + if oldfrag: + oldfrag = '%s' % oldfrag + if newfrag: + newfrag = '%s' % newfrag + oldfragments.append(oldfrag) + newfragments.append(newfrag) + + old['line'] = "".join(oldfragments) + new['line'] = "".join(newfragments)