Skip to content

Commit 69c1566

Browse files
committed
Add support for dmp deltas
1 parent 4407650 commit 69c1566

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

tokdiff.py

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,22 @@
1010
This is a simple diff utility based upon pygments' lexer token streams.
1111
"""
1212

13-
parser = argparse.ArgumentParser(description=__file__.__doc__)
13+
parser = argparse.ArgumentParser(
14+
description="Generates tokenized diffs using Pygments")
1415
parser.add_argument('lexername', help="Pygments lexer to utilize")
1516
parser.add_argument('file1', type=argparse.FileType('r'))
1617
parser.add_argument('file2', type=argparse.FileType('r'))
1718
parser.add_argument('-o', '--out', type=argparse.FileType('w'),
1819
default=sys.stdout)
1920
group = parser.add_mutually_exclusive_group()
20-
group.add_argument('-v', '--verbose', action='store_true')
21-
group.add_argument('-u', '--unidiff', action='store_true')
21+
group.add_argument('-v', '--verbose', action='store_true',
22+
help='Verbose tokenization diff')
23+
group.add_argument('-u', '--unidiff', action='store_true',
24+
help='Unidiff-like character-based diff (default)')
25+
group.add_argument('-d', '--delta', action='store_true',
26+
help='Simplified intermediate delta')
27+
group.add_argument('-c', '--compare', action='store_true',
28+
help='HTML comparison of tokenized diff to char diffs')
2229
data = parser.parse_args()
2330

2431
lexer = pygments.lexers.get_lexer_by_name(data.lexername)
@@ -28,7 +35,9 @@
2835

2936
sm = SequenceMatcher(None, a, b)
3037

31-
if data.verbose or not data.unidiff:
38+
data.unidiff = not data.verbose and not data.delta and not data.compare
39+
40+
if data.verbose:
3241
for op, a1, a2, b1, b2 in sm.get_opcodes():
3342
if op == 'equal':
3443
for item in a[a1:a2]:
@@ -48,7 +57,7 @@
4857
data.out.write("- %s: %s\n" % item)
4958
else:
5059
data.out.write("<<%s>>\n" % op)
51-
elif data.unidiff:
60+
else:
5261
dmp = diff_match_patch()
5362
diffs = []
5463
for op, a1, a2, b1, b2 in sm.get_opcodes():
@@ -66,7 +75,10 @@
6675
elif op == 'delete':
6776
for line in ''.join(val for type, val in b[a1:a2]).splitlines(True):
6877
diffs.append((dmp.DIFF_DELETE, line))
69-
patches = dmp.patch_make(diffs)
70-
data.out.write(dmp.patch_toText(patches))
78+
if data.unidiff:
79+
patches = dmp.patch_make(diffs)
80+
data.out.write(dmp.patch_toText(patches))
81+
elif data.delta:
82+
data.out.write(dmp.diff_toDelta(diffs))
7183

7284
# vim: ai et ts=4 sts=4 sw=4

0 commit comments

Comments
 (0)