-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathdiff_highlight.py
More file actions
130 lines (107 loc) · 4.22 KB
/
diff_highlight.py
File metadata and controls
130 lines (107 loc) · 4.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# -*- coding: utf-8 -*-
# Copyright 2013 Takeshi KOMIYA
# Copyright (c) 2001-2013 Python Software Foundation; All Rights Reserved
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from mercurial import extensions, ui, util
from mercurial.i18n import _
from highlights.pprint import INSERTED, DELETED, pprint_hunk
INSERT_NORM = 'diff.inserted'
INSERT_EMPH = 'diff.inserted_highlight'
DELETE_NORM = 'diff.deleted'
DELETE_EMPH = 'diff.deleted_highlight'
class colorui(ui.ui):
hunk = None
tab = None
def __init__(self, src=None):
super(colorui, self).__init__(src)
self.hunk = []
def write(self, *args, **opts):
label = opts.get('label')
if label in (INSERT_NORM, DELETE_NORM):
if self.tab is not None:
change = self.hunk.pop()
self.hunk.append((change[0] + self.tab + "".join(args), change[1]))
self.tab = None
else:
self.hunk.append(("".join(args), opts))
elif label == 'diff.trailingwhitespace': # merge to hunk
change = self.hunk.pop()
self.hunk.append((change[0] + "".join(args), change[1]))
elif label == 'diff.tab':
self.tab = "".join(args)
elif label == '' and args == ("\n",) and self.hunk:
self.hunk.append((args[0], opts))
else:
self.flush_hunk()
super(colorui, self).write(*args, **opts)
def flush(self):
self.flush_hunk()
super(colorui, self).flush()
def flush_hunk(self):
if self.hunk is None: # not initialized yet
return
hunk = [(ret[0].decode('utf-8'), ret[1]) for ret in self.hunk]
new = [c[0] for c in hunk if c[1]['label'] == INSERT_NORM]
old = [c[0] for c in hunk if c[1]['label'] == DELETE_NORM]
write = super(colorui, self).write
for string, style, highlighted in pprint_hunk(new, 0, len(new),
old, 0, len(old)):
if style == INSERTED:
if highlighted:
write(string.encode('utf-8'), label=INSERT_EMPH)
else:
write(string.encode('utf-8'), label=INSERT_NORM)
elif style == DELETED:
if highlighted:
write(string.encode('utf-8'), label=DELETE_EMPH)
else:
write(string.encode('utf-8'), label=DELETE_NORM)
else:
write(string.encode('utf-8'), label='')
self.hunk = []
def uisetup(ui):
if ui.plain():
return
try:
from hgext import color as colorext
try:
extensions.find('color')
except KeyError:
ui.warn(_("warning: 'diff-highlight' requires 'color' extension "
"to be enabled, but not\n"))
return
except ImportError:
colorext = None
try:
from mercurial import color
except ImportError:
pass
if not isinstance(ui, colorui):
colorui.__bases__ = (ui.__class__,)
ui.__class__ = colorui
ver = tuple(int(s) if s.isdigit() else s for s in util.version().split('.'))
def colorconfig(orig, *args, **kwargs):
ret = orig(*args, **kwargs)
if ver < (4, 1):
styles = colorext._styles
elif ver < (4, 2):
styles = color._styles
else:
styles = color._defaultstyles
if INSERT_EMPH not in styles:
styles[INSERT_EMPH] = styles[INSERT_NORM] + ' inverse'
if DELETE_EMPH not in styles:
styles[DELETE_EMPH] = styles[DELETE_NORM] + ' inverse'
return ret
extensions.wrapfunction(colorext if ver < (4, 2) else color, 'configstyles', colorconfig)