Skip to content

Commit 983a5ce

Browse files
committed
Trim highlights
For example, if you add a number of spaces followed by a comment at the end of a line, the spaces will not be highlighted. I like this better.
1 parent dd62ad7 commit 983a5ce

13 files changed

+93
-30
lines changed

src/hunk_highlighter.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -316,7 +316,7 @@ mod tests {
316316
result.highlighted[0].get(),
317317
concat!(
318318
"\u{1b}[31m-\u{1b}[33mHello, my name is Johan\u{1b}[0m\n",
319-
"\u{1b}[32m+\u{1b}[33mHello, my \u{1b}[7m\u{1b}[32mfirst \u{1b}[27m\u{1b}[33mname is Johan\u{1b}[0m\n"
319+
"\u{1b}[32m+\u{1b}[33mHello, my \u{1b}[7m\u{1b}[32mfirst\u{1b}[27m \u{1b}[33mname is Johan\u{1b}[0m\n"
320320
)
321321
);
322322
assert_eq!(result.highlighted[1].get(), " I like pie.\n");

src/refiner.rs

Lines changed: 79 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -217,9 +217,34 @@ fn is_whitepace_replacement(old_run: &[&str], new_run: &[&str]) -> bool {
217217
return old_whitespace_only && new_whitespace_only;
218218
}
219219

220-
/// Returns two vectors for old and new sections. The first bool is true if
221-
/// there were any highlights found in the old text. The second bool is true if
222-
/// any highlights were removed for readability in the new text.
220+
fn push_styled_tokens(destination: &mut Vec<StyledToken>, run: Vec<&str>, style: Style) {
221+
// Except for just pushing the tokens, any leading or trailing
222+
// whitespace-only tokens in the run should always be midlighted.
223+
224+
let first_non_leading_whitespace_index = run
225+
.iter()
226+
.position(|token| !token.chars().all(|c| c.is_whitespace()));
227+
228+
let last_non_trailing_whitespace_index = run
229+
.iter()
230+
.rposition(|token| !token.chars().all(|c| c.is_whitespace()));
231+
232+
for (index, token) in run.iter().enumerate() {
233+
let in_leading_whitespace = first_non_leading_whitespace_index.is_some()
234+
&& index < first_non_leading_whitespace_index.unwrap();
235+
let in_trailing_whitespace = last_non_trailing_whitespace_index.is_some()
236+
&& index > last_non_trailing_whitespace_index.unwrap();
237+
let style = if in_leading_whitespace || in_trailing_whitespace {
238+
Style::DiffPartMidlighted
239+
} else {
240+
style
241+
};
242+
243+
destination.push(StyledToken::new(token.to_string(), style));
244+
}
245+
}
246+
247+
/// Returns two vectors for old and new sections.
223248
///
224249
/// `old_text` and `new_text` are multi line strings. Having or not having
225250
/// trailing newlines will affect tokenization. The lines are not expected to
@@ -280,9 +305,7 @@ pub fn to_highlighted_tokens(
280305
} else {
281306
Style::DiffPartMidlighted
282307
};
283-
for token in run.iter() {
284-
new_tokens.push(StyledToken::new(token.to_string(), style));
285-
}
308+
push_styled_tokens(&mut new_tokens, run, style);
286309
}
287310

288311
similar::DiffOp::Delete {
@@ -296,9 +319,7 @@ pub fn to_highlighted_tokens(
296319
} else {
297320
Style::DiffPartMidlighted
298321
};
299-
for token in run.iter() {
300-
old_tokens.push(StyledToken::new(token.to_string(), style));
301-
}
322+
push_styled_tokens(&mut old_tokens, run, style);
302323
}
303324

304325
similar::DiffOp::Replace {
@@ -319,13 +340,8 @@ pub fn to_highlighted_tokens(
319340
Style::DiffPartMidlighted
320341
};
321342

322-
for token in old_run.iter() {
323-
old_tokens.push(StyledToken::new(token.to_string(), style));
324-
}
325-
326-
for token in new_run.iter() {
327-
new_tokens.push(StyledToken::new(token.to_string(), style));
328-
}
343+
push_styled_tokens(&mut old_tokens, old_run, style);
344+
push_styled_tokens(&mut new_tokens, new_run, style);
329345
}
330346
}
331347

@@ -761,4 +777,51 @@ pub(crate) mod tests {
761777
[StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),]
762778
);
763779
}
780+
781+
#[test]
782+
fn test_push_styled_tokens() {
783+
let mut tokens = Vec::new();
784+
push_styled_tokens(&mut tokens, vec!["a", "b", "c"], Style::DiffPartHighlighted);
785+
assert_eq!(
786+
tokens,
787+
vec![
788+
StyledToken::new("a".to_string(), Style::DiffPartHighlighted),
789+
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
790+
StyledToken::new("c".to_string(), Style::DiffPartHighlighted),
791+
]
792+
);
793+
794+
let mut tokens = Vec::new();
795+
push_styled_tokens(&mut tokens, vec![" ", "b", "c"], Style::DiffPartHighlighted);
796+
assert_eq!(
797+
tokens,
798+
vec![
799+
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
800+
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
801+
StyledToken::new("c".to_string(), Style::DiffPartHighlighted),
802+
]
803+
);
804+
805+
let mut tokens = Vec::new();
806+
push_styled_tokens(&mut tokens, vec!["a", "b", " "], Style::DiffPartHighlighted);
807+
assert_eq!(
808+
tokens,
809+
vec![
810+
StyledToken::new("a".to_string(), Style::DiffPartHighlighted),
811+
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
812+
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
813+
]
814+
);
815+
816+
let mut tokens = Vec::new();
817+
push_styled_tokens(&mut tokens, vec![" ", "b", " "], Style::DiffPartHighlighted);
818+
assert_eq!(
819+
tokens,
820+
vec![
821+
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
822+
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
823+
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
824+
]
825+
);
826+
}
764827
}

testdata/adds-only.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@
22
+++ /tmp/b.txt 2023-08-06 16:07:39.000000000 +0200
33
@@ -1,2 +1,2 @@
44
-Hello, my name is Johan
5-
+Hello, my first name is Johan
5+
+Hello, my first name is Johan
66
I like pie.

testdata/conflict-markers-diff3.txt.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ This is an example of git conflict markers.
33
<<<<<<< HEAD
44
This line is changed on the main branch.
55
||||||| 07ffb9b
6-
This line is from the initial commit on the main branch.
6+
This line is from the initial commit on the main branch.
77
=======
88
This line is from the branch named "branch".
99
>>>>>>> branch

testdata/conflict-with-context.riff-output

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
 + }
2121
 +
2222
 + firstHitPosition := p.findFirstHit(*lineNumber, nil, false)
23-
 + if firstHitPosition == nil && (*lineNumber != linenumbers.LineNumber{}) {
23+
 + if firstHitPosition == nil && (*lineNumber != linenumbers.LineNumber{}) {
2424
 + // Try again from the top
2525
 + firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, lineNumber, false)
2626
 + }
@@ -37,7 +37,7 @@
3737
 + p.scrollPosition = *firstHitPosition
3838
 +}
3939
 +
40-
 +// NOTE: When we search, we do that by looping over the *input lines*, not the
40+
 +// NOTE: When we search, we do that by looping over the *input lines*, not the
4141
 +// screen lines. That's why startPosition is a LineNumber rather than a
4242
 +// scrollPosition.
4343
 +//
@@ -72,7 +72,7 @@
7272
++}
7373
++
7474
+ // NOTE: When we search, we do that by looping over the *input lines*, not
75-
+ // the screen lines. That's why we're using a line number rather than a
75+
+ // the screen lines. That's why we're using a line number rather than a
7676
+ // scrollPosition for searching.
7777
++=======
7878
+ // NOTE: When we search, we do that by looping over the *input lines*, not

testdata/copies-and-renames.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
copies of the Software, and to permit persons to whom the Software is
1717
furnished to do so, subject to the following conditions:
1818

19-
-The above copyright notice and this permission notice shall be included in all
19+
-The above copyright notice and this permission notice shall be included in all
2020
+The above notice and this permission notice shall be included in all
2121
copies or substantial portions of the Software.
2222

testdata/dont-highlight-complete-lines.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
+++ after.txt 2023-09-24 16:22:42
33
@@ -1,1 +1,6 @@
44
- let tokens = std::mem::take(&mut self.tokens);
5-
+ let mut tokens = std::mem::take(&mut self.tokens);
5+
+ let mut tokens = std::mem::take(&mut self.tokens);
66
+
77
+ // FIXME: Maybe do highlight_space_between_words() before this one? And
88
+ // not do that for each line?

testdata/git-diff-conflict-diff3.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
++<<<<<<< HEAD
99
 +This line is changed on the main branch.
1010
++||||||| c26bbf8
11-
++This line is from the initial commit on the main branch.
11+
++This line is from the initial commit on the main branch.
1212
++=======
1313
+ This line is from the branch named "branch".
1414
++>>>>>>> branch

testdata/issue-with-incomplete-output.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use crate::string_future::StringFuture;
88
use crate::token_collector::{
99
- lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,
10-
+ align_tabs, lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,
10+
+ align_tabs, lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,
1111
LINE_STYLE_OLD_FILENAME,
1212
};
1313

testdata/merge-two-way.riff-output

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,5 @@ Date: Thu Jan 25 08:57:37 2024 +0100
2929
This is placeholder line six.
3030
- -> A prefix will be added to this line on main, a suffix on branch and the middle will be changed during conflict resolution.
3131
 -A prefix will be added to this line on main, a suffix on branch and the middle will be changed during conflict resolution. <-
32-
++-> A prefix will be added to this line on main, a suffix on branch and the middle was changed during conflict resolution. <-
32+
++-> A prefix will be added to this line on main, a suffix on branch and the middle was changed during conflict resolution. <-
3333
This is placeholder line seven.

0 commit comments

Comments
 (0)