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

Kirill Elagin kirelagin at gmail.com
Tue Oct 4 18:35:34 CDT 2011


# HG changeset patch
# User Kirill Elagin <kirelagin at gmail.com>
# Date 1317770811 -10800
# Node ID 92931d6d500b30472fecd188d87a1b343a5c3a5e
# Parent  6dc67dced8c122f6139ae20ccdc03a6b11e8b765
diff: enhance highlighting with color (issue3034)

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

diff -r 6dc67dced8c1 -r 92931d6d500b hgext/color.py
--- a/hgext/color.py	Sun Oct 02 13:02:15 2011 -0500
+++ b/hgext/color.py	Wed Oct 05 02:26:51 2011 +0300
@@ -48,7 +48,7 @@
   qseries.unapplied = black bold
   qseries.missing = red bold

-  diff.diffline = bold
+  diff.header = bold
   diff.extended = cyan bold
   diff.file_a = red bold
   diff.file_b = green bold
@@ -240,7 +240,7 @@
            'branches.inactive': 'none',
            'diff.changed': 'white',
            'diff.deleted': 'red',
-           'diff.diffline': 'bold',
+           'diff.header': 'bold',
            'diff.extended': 'cyan bold',
            'diff.file_a': 'red bold',
            'diff.file_b': 'green bold',
diff -r 6dc67dced8c1 -r 92931d6d500b mercurial/patch.py
--- a/mercurial/patch.py	Sun Oct 02 13:02:15 2011 -0500
+++ b/mercurial/patch.py	Wed Oct 05 02:26:51 2011 +0300
@@ -1619,33 +1619,47 @@

 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')]
+    in_head = False
+
+    def highlighted(line):
+        headprefixes = [(('new', 'old',
+                           'copy', 'rename',' deleted'), 'diff.extended'),
+                         ('---', 'diff.file_a'),
+                         ('+++', 'diff.file_b')]
+        textprefixes = [('-', 'diff.deleted'),
+                         ('+', 'diff.inserted'),
+                         ('@', 'diff.hunk')]
+        if in_head:
+            for prefs, label in headprefixes:
+                if line.startswith(prefs):
+                    return (line, label)
+            return (line, 'diff.header')
+        else:
+            for prefs, label in textprefixes:
+                if line.startswith(prefs):
+                    return (line, label)
+        return (line, '')

     for chunk in func(*args, **kw):
         lines = chunk.split('\n')
         for i, line in enumerate(lines):
             if i != 0:
                 yield ('\n', '')
+
+            if not in_head:
+                if not line.startswith((' ', '+', '-', '@')):
+                    in_head = True
+            else:
+                if line.startswith('@'):
+                    in_head = False
+
             stripline = line
-            if line and line[0] in '+-':
+            if not in_head and line.startswith(('+', '-')):
                 # highlight trailing whitespace, but only in changed lines
                 stripline = line.rstrip()
-            for prefix, label in prefixes:
-                if stripline.startswith(prefix):
-                    yield (stripline, label)
-                    break
-            else:
-                yield (line, '')
+
+            yield highlighted(stripline)
+
             if line != stripline:
                 yield (line[len(stripline):], 'diff.trailingwhitespace')


More information about the Mercurial-devel mailing list