D3211: patch: buffer lines for a same hunk

2018-04-16 Thread quark (Jun Wu)
This revision was automatically updated to reflect the committed changes.
Closed by commit rHG5471348921c1: patch: buffer lines for a same hunk (authored 
by quark, committed by ).

CHANGED PRIOR TO COMMIT
  https://phab.mercurial-scm.org/D3211?vs=7923=8334#toc

REPOSITORY
  rHG Mercurial

CHANGES SINCE LAST UPDATE
  https://phab.mercurial-scm.org/D3211?vs=7923=8334

REVISION DETAIL
  https://phab.mercurial-scm.org/D3211

AFFECTED FILES
  mercurial/patch.py
  tests/test-diff-color.t

CHANGE DETAILS

diff --git a/tests/test-diff-color.t b/tests/test-diff-color.t
--- a/tests/test-diff-color.t
+++ b/tests/test-diff-color.t
@@ -337,6 +337,7 @@
   [diff.deleted|-(to see if it works)]
   [diff.inserted|+three of those lines have]
   [diff.inserted|+collapsed onto one]
+#if false
   $ hg diff --config experimental.worddiff=True --color=debug
   [diff.diffline|diff --git a/file1 b/file1]
   [diff.file_a|--- a/file1]
@@ -370,6 +371,7 @@
   [diff.deleted|-(to see if it works)]
   [diff.inserted|+three of those lines ][diff.inserted.highlight|have]
   [diff.inserted|+][diff.inserted.highlight|collapsed][diff.inserted| onto one]
+#endif
 
 multibyte character shouldn't be broken up in word diff:
 
@@ -383,10 +385,13 @@
   > f.write(b"blah \xe3\x82\xa4 blah\n")
   > EOF
   $ hg ci -m 'slightly change utf8 char' utf8
+
+#if false
   $ hg diff --config experimental.worddiff=True --color=debug -c.
   [diff.diffline|diff --git a/utf8 b/utf8]
   [diff.file_a|--- a/utf8]
   [diff.file_b|+++ b/utf8]
   [diff.hunk|@@ -1,1 +1,1 @@]
   [diff.deleted|-blah ][diff.deleted.highlight|\xe3\x82\xa2][diff.deleted| 
blah] (esc)
   [diff.inserted|+blah ][diff.inserted.highlight|\xe3\x82\xa4][diff.inserted| 
blah] (esc)
+#endif
diff --git a/mercurial/patch.py b/mercurial/patch.py
--- a/mercurial/patch.py
+++ b/mercurial/patch.py
@@ -11,7 +11,6 @@
 import collections
 import contextlib
 import copy
-import difflib
 import email
 import errno
 import hashlib
@@ -2481,11 +2480,32 @@
 else:
 return difffn(opts, None)
 
+def diffsinglehunk(hunklines):
+"""yield tokens for a list of lines in a single hunk"""
+for line in hunklines:
+# chomp
+chompline = line.rstrip('\n')
+# highlight tabs and trailing whitespace
+stripline = chompline.rstrip()
+if line[0] == '-':
+label = 'diff.deleted'
+elif line[0] == '+':
+label = 'diff.inserted'
+else:
+raise error.ProgrammingError('unexpected hunk line: %s' % line)
+for token in tabsplitter.findall(stripline):
+if '\t' == token[0]:
+yield (token, 'diff.tab')
+else:
+yield (token, label)
+
+if chompline != stripline:
+yield (chompline[len(stripline):], 'diff.trailingwhitespace')
+if chompline != line:
+yield (line[len(chompline):], '')
+
 def difflabel(func, *args, **kw):
 '''yields 2-tuples of (output, label) based on the output of func()'''
-inlinecolor = False
-if kw.get(r'opts'):
-inlinecolor = kw[r'opts'].worddiff
 headprefixes = [('diff', 'diff.diffline'),
 ('copy', 'diff.extended'),
 ('rename', 'diff.extended'),
@@ -2497,125 +2517,59 @@
 ('---', 'diff.file_a'),
 ('+++', 'diff.file_b')]
 textprefixes = [('@', 'diff.hunk'),
-('-', 'diff.deleted'),
-('+', 'diff.inserted')]
+# - and + are handled by diffsinglehunk
+   ]
 head = False
+
+# buffers a hunk, i.e. adjacent "-", "+" lines without other changes.
+hunkbuffer = []
+def consumehunkbuffer():
+if hunkbuffer:
+for token in diffsinglehunk(hunkbuffer):
+yield token
+hunkbuffer[:] = []
+
 for chunk in func(*args, **kw):
 lines = chunk.split('\n')
-matches = {}
-if inlinecolor:
-matches = _findmatches(lines)
 linecount = len(lines)
 for i, line in enumerate(lines):
 if head:
 if line.startswith('@'):
 head = False
 else:
 if line and not line.startswith((' ', '+', '-', '@', '\\')):
 head = True
-stripline = line
 diffline = False
 if not head and line and line.startswith(('+', '-')):
-# highlight tabs and trailing whitespace, but only in
-# changed lines
-stripline = line.rstrip()
 diffline = True
 
 prefixes = textprefixes
 if head:
 prefixes = headprefixes
-for prefix, label in prefixes:
-if stripline.startswith(prefix):
-if diffline:
-if i in matches:
-for t, l in _inlinediff(lines[i].rstrip(),
-

D3211: patch: buffer lines for a same hunk

2018-04-10 Thread yuja (Yuya Nishihara)
yuja requested changes to this revision.
yuja added inline comments.
This revision now requires changes to proceed.

INLINE COMMENTS

> patch.py:2484
> +stripline = chompline.rstrip()
> +if line[0] == '-':
> +label = 'diff.deleted'

Don't use `bytes[n]` since it returns an integer on Python 3.
That's why there were silly `startswith(char)`.

> patch.py:2548
> +bufferedline += "\n"
> +hunkbuffer.append(bufferedline)
>  else:

I think `hunkbuffer` could be `(alines_without_pluses, blines_without_minuses)` 
so `difsinglehunkinline()` function
will be slightly simpler.

> patch.py:2572
> -
> -def _inlinediff(s1, s2, operation):
> -'''Perform string diff to highlight specific changes.'''

Can you split this patch to

- drop the current inlinediff implementation
- and buffer hunk lines?

REPOSITORY
  rHG Mercurial

REVISION DETAIL
  https://phab.mercurial-scm.org/D3211

To: quark, #hg-reviewers, yuja
Cc: yuja, mercurial-devel
___
Mercurial-devel mailing list
Mercurial-devel@mercurial-scm.org
https://www.mercurial-scm.org/mailman/listinfo/mercurial-devel