diff mercurial/patch.py @ 22460:c343557a8442

patch: enable diff.tab markup for the color extension The following patch splits up changed lines along tabs (using re.findall), and gives them a "diff.tab" label. This can be used by the color extension for colorising tabs, like it does right now with trailing whitespace. I also provide corresponding tests.
author Jordi Gutiérrez Hermoso <jordigh@octave.org>
date Wed, 20 Aug 2014 15:15:50 -0400
parents 650b5b6e75ed
children ac072c79bd9d
line wrap: on
line diff
--- a/mercurial/patch.py	Wed Sep 17 13:08:03 2014 -0700
+++ b/mercurial/patch.py	Wed Aug 20 15:15:50 2014 -0400
@@ -18,6 +18,7 @@
 import base85, mdiff, scmutil, util, diffhelpers, copies, encoding, error
 
 gitre = re.compile('diff --git a/(.*) b/(.*)')
+tabsplitter = re.compile(r'(\t+|[^\t]+)')
 
 class PatchError(Exception):
     pass
@@ -1673,15 +1674,26 @@
                 if line and line[0] not in ' +-@\\':
                     head = True
             stripline = line
+            diffline = False
             if not head and line and line[0] in '+-':
-                # highlight trailing whitespace, but only in changed lines
+                # 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):
-                    yield (stripline, label)
+                    if diffline:
+                        for token in tabsplitter.findall(stripline):
+                            if '\t' == token[0]:
+                                yield (token, 'diff.tab')
+                            else:
+                                yield (token, label)
+                    else:
+                        yield (stripline, label)
                     break
             else:
                 yield (line, '')