Mercurial > kallithea
changeset 8709:53142fd5af4e
lib/diffs: make sure that trailing tabs are indicated
Between the initial submission and final version of commit f79c40759d6f,
changes were made that turn out to be incorrect. The changes assume that the
later match on trailing tabs will 'win' from the plain 'tab' match. However,
Python 're' documentation says:
As the target string is scanned, REs separated by '|' are tried from
left to right. When one pattern completely matches, that branch is
accepted. This means that once A matches, B will not be tested further,
even if it would produce a longer overall match. In other words, the '|'
operator is never greedy.
https://docs.python.org/3.8/library/re.html
As a result, a trailing tab is seen as a plain tab and not highlighted in a
special way.
Unify the tab handling to make it unambiguous how they should be parsed.
The change diff mainly shows re group numbers shifting.
author | Thomas De Schampheleire <thomas.de_schampheleire@nokia.com> |
---|---|
date | Mon, 19 Oct 2020 12:47:50 +0200 |
parents | 3fb80ff77bda |
children | 77f13ea8ad3b |
files | kallithea/lib/diffs.py kallithea/tests/models/test_diff_parsers.py |
diffstat | 2 files changed, 13 insertions(+), 13 deletions(-) [+] |
line wrap: on
line diff
--- a/kallithea/lib/diffs.py Wed Oct 28 14:51:26 2020 +0100 +++ b/kallithea/lib/diffs.py Mon Oct 19 12:47:50 2020 +0200 @@ -445,7 +445,7 @@ return self.adds, self.removes -_escape_re = re.compile(r'(&)|(<)|(>)|(\t)|(\r)|( $)|(\t$)') +_escape_re = re.compile(r'(&)|(<)|(>)|(\t)($)?|(\r)|( $)') def _escaper(diff_line): @@ -467,7 +467,7 @@ >>> _escaper(' foo\rbar\r') ' foo<u class="cr"></u>bar<u class="cr"></u>' >>> _escaper(' foo\t') - ' foo<u>\t</u>' + ' foo<u>\t</u><i></i>' >>> _escaper(' foo ') ' foo <i></i>' >>> _escaper(' foo ') @@ -477,15 +477,15 @@ >>> _escaper(' ') ' <i></i>' >>> _escaper(' \t') - ' <u>\t</u>' + ' <u>\t</u><i></i>' >>> _escaper(' \t ') ' <u>\t</u> <i></i>' >>> _escaper(' \t') - ' <u>\t</u>' + ' <u>\t</u><i></i>' >>> _escaper(' \t\t ') ' <u>\t</u><u>\t</u> <i></i>' >>> _escaper(' \t\t') - ' <u>\t</u><u>\t</u>' + ' <u>\t</u><u>\t</u><i></i>' >>> _escaper(' foo&bar<baz> ') ' foo&bar<baz> <i></i>' """ @@ -499,15 +499,15 @@ if groups[2]: return '>' if groups[3]: - return '<u>\t</u>' # Note: trailing tabs will get a longer match later - if groups[4]: + if groups[4] is not None: # end of line + return '<u>\t</u><i></i>' + return '<u>\t</u>' + if groups[5]: return '<u class="cr"></u>' - if groups[5]: + if groups[6]: if m.start() == 0: return ' ' # first column space shouldn't make empty lines show up as trailing space return ' <i></i>' - if groups[6]: - return '<u>\t</u><i></i>' assert False return _escape_re.sub(substitute, diff_line)
--- a/kallithea/tests/models/test_diff_parsers.py Wed Oct 28 14:51:26 2020 +0100 +++ b/kallithea/tests/models/test_diff_parsers.py Mon Oct 19 12:47:50 2020 +0200 @@ -297,13 +297,13 @@ assert s == r''' context '@@ -51,6 +51,13 @@' unmod 51 51 '<u>\t</u>begin();' -unmod 52 52 '<u>\t</u>' +unmod 52 52 '<u>\t</u><i></i>' add 53 '<u>\t</u>int foo;<u class="cr"></u>' add 54 '<u>\t</u>int bar; <u class="cr"></u>' add 55 '<u>\t</u>int baz;<u>\t</u><u class="cr"></u>' add 56 '<u>\t</u>int space; <i></i>' -add 57 '<u>\t</u>int tab;<u>\t</u>' -add 58 '<u>\t</u>' +add 57 '<u>\t</u>int tab;<u>\t</u><i></i>' +add 58 '<u>\t</u><i></i>' unmod 59 53 ' <i></i>' del 54 '<u>\t</u>#define MAX_STEPS (48)' add 60 '<u>\t</u><u class="cr"></u>'