Skip to content

Commit

Permalink
patch: highlight the trailing white space in diff view
Browse files Browse the repository at this point in the history
Summary:
This diffs make the worddiff highlights the trailing white space. Trailing white space is hard to see, highlighting it will make it easier to review the changes.

Another alternative solution is to change the "tokenizer" to make it cannot have two consecutive tokens like `(True, b" "), (False, b"\n")`. This approach is likely faster but less robust than the change proposed in this diff.

Reviewed By: sggutier

Differential Revision: D64603891

fbshipit-source-id: 608939288837ebae4bb522915e44a3b2df6480d5
  • Loading branch information
zzl0 authored and facebook-github-bot committed Oct 21, 2024
1 parent d5fe7f2 commit ad984d8
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
17 changes: 11 additions & 6 deletions eden/scm/sapling/patch.py
Original file line number Diff line number Diff line change
Expand Up @@ -2888,16 +2888,19 @@ def diffsinglehunkinline(hunklines):
(b"+", "diff.inserted", btokens),
]:
isprevnewline = True
for changed, token in tokens:
length = len(tokens)
for i, (changed, token) in enumerate(tokens):
if isprevnewline:
yield (prefix, label)
isprevnewline = False
# special handling line end
isnextnewline = i + 1 < length and tokens[i + 1][1] == b"\n"
isendofline = token.endswith(b"\n")
if isendofline:
chomp = token[:-1] # chomp
if isendofline or isnextnewline:
chomp = token[:-1] if isendofline else token # chomp
token = chomp.rstrip() # detect spaces at the end
endspaces = chomp[len(token) :]

# scan tabs
for maybetab in tabsplitter.findall(token):
if b"\t" == maybetab[0:1]:
Expand All @@ -2908,11 +2911,13 @@ def diffsinglehunkinline(hunklines):
else:
currentlabel = label + ".unchanged"
yield (maybetab, currentlabel)
if isendofline:

if isendofline or isnextnewline:
if endspaces:
yield (endspaces, "diff.trailingwhitespace")
yield (b"\n", "")
isprevnewline = True
if isendofline:
yield (b"\n", "")
isprevnewline = True


def difflabel(func, *args, **kw):
Expand Down
4 changes: 2 additions & 2 deletions eden/scm/tests/test-diff-color.t
Original file line number Diff line number Diff line change
Expand Up @@ -282,12 +282,12 @@ test trailing spaces color diff
> this is the first line
> this is the second line
> EOF
tofix: the second line should end with diff.trailingwhitespace
the second line should end with diff.trailingwhitespace
$ hg diff --config experimental.worddiff=True --color=debug
[diff.diffline|diff --git a/file1 b/file1]
[diff.file_a|--- a/file1]
[diff.file_b|+++ b/file1]
[diff.hunk|@@ -1,2 +1,2 @@]
this is the first line
[diff.deleted|-][diff.deleted.unchanged|this is the second line]
[diff.inserted|+][diff.inserted.unchanged|this is the second line][diff.inserted.changed| ]
[diff.inserted|+][diff.inserted.unchanged|this is the second line][diff.trailingwhitespace| ]

0 comments on commit ad984d8

Please sign in to comment.