Skip to content

Commit

Permalink
Trim highlights
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
walles committed Oct 23, 2024
1 parent dd62ad7 commit 983a5ce
Show file tree
Hide file tree
Showing 13 changed files with 93 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/hunk_highlighter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ mod tests {
result.highlighted[0].get(),
concat!(
"\u{1b}[31m-\u{1b}[33mHello, my name is Johan\u{1b}[0m\n",
"\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"
"\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"
)
);
assert_eq!(result.highlighted[1].get(), " I like pie.\n");
Expand Down
95 changes: 79 additions & 16 deletions src/refiner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,9 +217,34 @@ fn is_whitepace_replacement(old_run: &[&str], new_run: &[&str]) -> bool {
return old_whitespace_only && new_whitespace_only;
}

/// Returns two vectors for old and new sections. The first bool is true if
/// there were any highlights found in the old text. The second bool is true if
/// any highlights were removed for readability in the new text.
fn push_styled_tokens(destination: &mut Vec<StyledToken>, run: Vec<&str>, style: Style) {
// Except for just pushing the tokens, any leading or trailing
// whitespace-only tokens in the run should always be midlighted.

let first_non_leading_whitespace_index = run
.iter()
.position(|token| !token.chars().all(|c| c.is_whitespace()));

let last_non_trailing_whitespace_index = run
.iter()
.rposition(|token| !token.chars().all(|c| c.is_whitespace()));

for (index, token) in run.iter().enumerate() {
let in_leading_whitespace = first_non_leading_whitespace_index.is_some()
&& index < first_non_leading_whitespace_index.unwrap();
let in_trailing_whitespace = last_non_trailing_whitespace_index.is_some()
&& index > last_non_trailing_whitespace_index.unwrap();
let style = if in_leading_whitespace || in_trailing_whitespace {
Style::DiffPartMidlighted
} else {
style
};

destination.push(StyledToken::new(token.to_string(), style));
}
}

/// Returns two vectors for old and new sections.
///
/// `old_text` and `new_text` are multi line strings. Having or not having
/// trailing newlines will affect tokenization. The lines are not expected to
Expand Down Expand Up @@ -280,9 +305,7 @@ pub fn to_highlighted_tokens(
} else {
Style::DiffPartMidlighted
};
for token in run.iter() {
new_tokens.push(StyledToken::new(token.to_string(), style));
}
push_styled_tokens(&mut new_tokens, run, style);
}

similar::DiffOp::Delete {
Expand All @@ -296,9 +319,7 @@ pub fn to_highlighted_tokens(
} else {
Style::DiffPartMidlighted
};
for token in run.iter() {
old_tokens.push(StyledToken::new(token.to_string(), style));
}
push_styled_tokens(&mut old_tokens, run, style);
}

similar::DiffOp::Replace {
Expand All @@ -319,13 +340,8 @@ pub fn to_highlighted_tokens(
Style::DiffPartMidlighted
};

for token in old_run.iter() {
old_tokens.push(StyledToken::new(token.to_string(), style));
}

for token in new_run.iter() {
new_tokens.push(StyledToken::new(token.to_string(), style));
}
push_styled_tokens(&mut old_tokens, old_run, style);
push_styled_tokens(&mut new_tokens, new_run, style);
}
}

Expand Down Expand Up @@ -761,4 +777,51 @@ pub(crate) mod tests {
[StyledToken::new("\t".to_string(), Style::DiffPartUnchanged),]
);
}

#[test]
fn test_push_styled_tokens() {
let mut tokens = Vec::new();
push_styled_tokens(&mut tokens, vec!["a", "b", "c"], Style::DiffPartHighlighted);
assert_eq!(
tokens,
vec![
StyledToken::new("a".to_string(), Style::DiffPartHighlighted),
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
StyledToken::new("c".to_string(), Style::DiffPartHighlighted),
]
);

let mut tokens = Vec::new();
push_styled_tokens(&mut tokens, vec![" ", "b", "c"], Style::DiffPartHighlighted);
assert_eq!(
tokens,
vec![
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
StyledToken::new("c".to_string(), Style::DiffPartHighlighted),
]
);

let mut tokens = Vec::new();
push_styled_tokens(&mut tokens, vec!["a", "b", " "], Style::DiffPartHighlighted);
assert_eq!(
tokens,
vec![
StyledToken::new("a".to_string(), Style::DiffPartHighlighted),
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
]
);

let mut tokens = Vec::new();
push_styled_tokens(&mut tokens, vec![" ", "b", " "], Style::DiffPartHighlighted);
assert_eq!(
tokens,
vec![
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
StyledToken::new("b".to_string(), Style::DiffPartHighlighted),
StyledToken::new(" ".to_string(), Style::DiffPartMidlighted),
]
);
}
}
2 changes: 1 addition & 1 deletion testdata/adds-only.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
[1m+++ [0m/tmp/[7m[32mb[27m[1m[39m.txt[22m[2m 2023-08-06 16:07:39.000000000 +0200[0m
[36m@@ -1,2 +1,2 @@[0m
[31m-Hello, my name is Johan[0m
[32m+Hello, my [7mfirst [27mname is Johan[0m
[32m+Hello, my [7mfirst[27m name is Johan[0m
I like pie.
2 changes: 1 addition & 1 deletion testdata/conflict-markers-diff3.txt.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ This is an example of git conflict markers.
[7m<<<<<<< HEAD
[7m[0mThis line is [7m[32mchanged[0m on the main branch.
[7m||||||| 07ffb9b
[7m[0mThis line is [7m[31mfrom the initial commit on the main [0mbranch.
[7m[0mThis line is [7m[31mfrom the initial commit on the main[27m [0mbranch.
[7m=======
[7m[0mThis line is from the [7m[32mbranch named "[0mbranch[7m[32m"[0m.
[7m>>>>>>> branch
6 changes: 3 additions & 3 deletions testdata/conflict-with-context.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
[7m +[27m[32m }[0m
[7m +[0m
[7m +[27m[32m firstHitPosition := p.findFirstHit(*lineNumber[0m, nil, false)
[7m +[0m if firstHitPosition == nil [7m[32m&& (*lineNumber != linenumbers.LineNumber{}) [0m{
[7m +[0m if firstHitPosition == nil [7m[32m&& (*lineNumber != linenumbers.LineNumber{})[27m [0m{
[7m +[0m // Try again from the top
[7m +[0m firstHitPosition = p.findFirstHit(linenumbers.LineNumber{}, lineNumber, false)
[7m +[0m }
Expand All @@ -37,7 +37,7 @@
[7m +[0m p.scrollPosition = *firstHitPosition
[7m +[0m}
[7m +[0m
[7m +[0m// NOTE: When we search, we do that by looping over the *input lines*, not[7m[32m the[0m
[7m +[0m// NOTE: When we search, we do that by looping over the *input lines*, not[32m [7mthe[0m
[7m +[0m// screen lines. That's why [7m[32mstartPosition is[0m a [7m[32mLineNumber[0m rather than a
[7m +[0m// scrollPosition[32m.[0m
[7m +[27m[32m//[0m
Expand Down Expand Up @@ -72,7 +72,7 @@
[7m++[27m[31m}[0m
[7m++[0m
[7m+ [0m// NOTE: When we search, we do that by looping over the *input lines*, not
[7m+ [0m//[7m[31m the[0m screen lines. That's why [7m[31mwe're using[0m a[7m[31m line number[0m rather than a
[7m+ [0m//[31m [7mthe[0m screen lines. That's why [7m[31mwe're using[0m a[31m [7mline number[0m rather than a
[7m+ [0m// scrollPosition [31mfor[0m searching.
[7m++=======[0m
[7m+ [0m// NOTE: When we search, we do that by looping over the *input lines*, not
Expand Down
2 changes: 1 addition & 1 deletion testdata/copies-and-renames.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

[31m-The above [7mcopyright [27mnotice and this permission notice shall be included in all[0m
[31m-The above [7mcopyright[27m notice and this permission notice shall be included in all[0m
[32m+The above notice and this permission notice shall be included in all[0m
copies or substantial portions of the Software.

Expand Down
2 changes: 1 addition & 1 deletion testdata/dont-highlight-complete-lines.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
[1m+++ [7m[22m[32mafter[27m[1m[39m.txt[22m[2m 2023-09-24 16:22:42[0m
[36m@@ -1,1 +1,6 @@[0m
[31m- let tokens = std::mem::take(&mut self.tokens);[0m
[32m+ let [7mmut [27mtokens = std::mem::take(&mut self.tokens);[0m
[32m+ let [7mmut[27m tokens = std::mem::take(&mut self.tokens);[0m
[32m+[0m
[32m+ // FIXME: Maybe do highlight_space_between_words() before this one? And[0m
[32m+ // not do that for each line?[0m
Expand Down
2 changes: 1 addition & 1 deletion testdata/git-diff-conflict-diff3.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
[7m++<<<<<<< HEAD[0m
[7m +[0mThis line is [7m[32mchanged[0m on the main branch.
[7m++||||||| c26bbf8[0m
[7m++[0mThis line is [7m[31mfrom the initial commit on the main [0mbranch.
[7m++[0mThis line is [7m[31mfrom the initial commit on the main[27m [0mbranch.
[7m++=======[0m
[7m+ [0mThis line is from the [7m[32mbranch named "[0mbranch[7m[32m"[0m.
[7m++>>>>>>> branch[0m
2 changes: 1 addition & 1 deletion testdata/issue-with-incomplete-output.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::string_future::StringFuture;
use crate::token_collector::{
[31m- lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,[0m
[32m+ [7malign_tabs, [27mlowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,[0m
[32m+ [7malign_tabs,[27m lowlight_timestamp, render, unhighlight_git_prefix, LINE_STYLE_NEW_FILENAME,[0m
LINE_STYLE_OLD_FILENAME,
};

Expand Down
2 changes: 1 addition & 1 deletion testdata/merge-two-way.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ Date: Thu Jan 25 08:57:37 2024 +0100
This is placeholder line six.
[31m- -> A prefix will be added to this line on main, a suffix on branch and the middle [7mwill be[27m changed during conflict resolution.[0m
[31m -A prefix will be added to this line on main, a suffix on branch and the middle [7mwill be[27m changed during conflict resolution. <-[0m
[32m++[7m-> [27mA prefix will be added to this line on main, a suffix on branch and the middle [7mwas[27m changed during conflict resolution.[7m <-[0m
[32m++[7m->[27m A prefix will be added to this line on main, a suffix on branch and the middle [7mwas[27m changed during conflict resolution. [7m<-[0m
This is placeholder line seven.
2 changes: 1 addition & 1 deletion testdata/no-newline-in-context.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
[1m+++ [0m/tmp/dom/[7m[32mbepa[27m[1m[39m.txt[22m[2m 2024-02-04 08:37:13[0m
[36m@@ -1,2 +1,2 @@[0m
[31m-[7mChange this[27m line[0m
[32m+[7mThis[27m line[7m was changed[0m
[32m+[7mThis[27m line [7mwas changed[0m
This line has no EOL
[2m\ No newline at end of file[0m
2 changes: 1 addition & 1 deletion testdata/partial-refine.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

fn print(stream: &mut BufWriter<&mut dyn Write>, text: &str) {
[31m- stream.write_all(text.as_bytes()).[7munwrap[27m();[0m
[32m+ [7mif let Err(error) = [27mstream.write_all(text.as_bytes()) {[0m
[32m+ [7mif let Err(error) =[27m stream.write_all(text.as_bytes()) {[0m
[32m+ if error.[7mkind[27m() == ErrorKind::BrokenPipe {[0m
[32m+ // This is fine, somebody probably just quit their pager before it[0m
[32m+ // was done reading our output.[0m
Expand Down
2 changes: 1 addition & 1 deletion testdata/screenshot.riff-output
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ Date: Tue Nov 17 08:44:29 2020 +0100
search if your search string is a valid regexp
* Supports displaying ANSI color coded texts (like the output from
[31m- `git diff` for example)[0m
[32m+ `git diff` [7m[| `riff`](https://github.com/walles/riff) [27mfor example)[0m
[32m+ `git diff` [7m[| `riff`](https://github.com/walles/riff)[27m for example)[0m
* Supports UTF-8 input and output
* The position in the file is always shown

Expand Down

0 comments on commit 983a5ce

Please sign in to comment.