Skip to content

Commit

Permalink
Fix handling of non-match highlight
Browse files Browse the repository at this point in the history
In some cases, `git grep` will customize the foreground-color for more
than just the subset of the line that matches the grep pattern. This
breaks the current match-detection behavior, which considers any
characters with a non-default "foreground color" within the "code" part
of a git-grep-ouput-line to be part of the match. This commit makes
match-detection check for boldness and a red foreground instead of just
checking for a non-default foreground-color.

git grep matches are bold and red by default. But git grep isn't the
only reason input to delta may contain color codes; there may still be
cases where the highlighting looks wrong here.
  • Loading branch information
jdpopkin committed Jul 15, 2022
1 parent 875e22c commit 2aa2c0c
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/handlers/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,9 @@ fn get_code_style_sections<'b>(
let match_style_sections = ansi::parse_style_sections(&raw_line[(prefix_end + 1)..])
.iter()
.map(|(ansi_term_style, s)| {
if ansi_term_style.foreground.is_some() {
if ansi_term_style.is_bold
&& ansi_term_style.foreground == Some(ansi_term::Colour::Red)
{
(match_style, *s)
} else {
(non_match_style, *s)
Expand Down Expand Up @@ -942,5 +944,18 @@ mod tests {
"kind: Service"
)]))
);

let plus_example = format!("etc/examples/189-merge-conflict.2.diff{escape}[36m:{escape}[m10{escape}[36m:{escape}[m{escape}[32m + let (style, non_emph_style) = {escape}[1;31mmatch{escape}[m state {{{escape}[m");
let plus_stripped = strip_ansi_codes(&plus_example);
let plus_grep = parse_grep_line(&plus_stripped).unwrap();

assert_eq!(
get_code_style_sections(&plus_example, hit, miss, &plus_grep),
Some(StyleSectionSpecifier::StyleSections(vec![
(miss, " + let (style, non_emph_style) = "),
(hit, "match"),
(miss, " state {")
]))
);
}
}

0 comments on commit 2aa2c0c

Please sign in to comment.