[PATCH] diff: enhance highlighting with color (issue3034)

Kirill Elagin kirelagin at gmail.com
Wed Oct 5 01:28:01 CDT 2011


# HG changeset patch
# User Kirill Elagin <kirelagin at gmail.com>
# Date 1317795638 -10800
# Branch stable
# Node ID 9e8873197a81bf613468df93f9e1c70e1c73c232
# Parent  fccd350acf799f756e3e09166d02b22f1360336c
diff: enhance highlighting with color (issue3034)

Now the highlighter knows whether it is in the header of the patch or not.

diff -r fccd350acf79 -r 9e8873197a81 mercurial/patch.py
--- a/mercurial/patch.py	Sun Oct 02 16:41:07 2011 -0500
+++ b/mercurial/patch.py	Wed Oct 05 09:20:38 2011 +0300
@@ -1619,27 +1619,36 @@

 def difflabel(func, *args, **kw):
     '''yields 2-tuples of (output, label) based on the output of func()'''
-    prefixes = [('diff', 'diff.diffline'),
-                ('copy', 'diff.extended'),
-                ('rename', 'diff.extended'),
-                ('old', 'diff.extended'),
-                ('new', 'diff.extended'),
-                ('deleted', 'diff.extended'),
-                ('---', 'diff.file_a'),
-                ('+++', 'diff.file_b'),
-                ('@@', 'diff.hunk'),
-                ('-', 'diff.deleted'),
-                ('+', 'diff.inserted')]
-
+    headprefixes = [('diff', 'diff.diffline'),
+                    ('copy', 'diff.extended'),
+                    ('rename', 'diff.extended'),
+                    ('old', 'diff.extended'),
+                    ('new', 'diff.extended'),
+                    ('deleted', 'diff.extended'),
+                    ('---', 'diff.file_a'),
+                    ('+++', 'diff.file_b'),]
+    textprefixes = [('@', 'diff.hunk'),
+                    ('-', 'diff.deleted'),
+                    ('+', 'diff.inserted')]
+    head = False
     for chunk in func(*args, **kw):
         lines = chunk.split('\n')
         for i, line in enumerate(lines):
             if i != 0:
                 yield ('\n', '')
+            if head:
+                if line.startswith('@'):
+                    head = False
+            else:
+                if line and not line[0] in ' +-@':
+                    head = True
             stripline = line
-            if line and line[0] in '+-':
+            if not head and line and line[0] in '+-':
                 # highlight trailing whitespace, but only in changed lines
                 stripline = line.rstrip()
+            prefixes = textprefixes
+            if head:
+                prefixes = headprefixes
             for prefix, label in prefixes:
                 if stripline.startswith(prefix):
                     yield (stripline, label)


More information about the Mercurial-devel mailing list