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 instead of foreground-color.

git grep matches are bolded by default, but they probably aren't the
_only_ thing git grep uses bold text for. So there may still be cases
where the highlighting looks wrong here.
  • Loading branch information
jdpopkin committed Jul 8, 2022
1 parent 875e22c commit 85a21a9
Showing 1 changed file with 14 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/handlers/grep.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ 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 {
(match_style, *s)
} else {
(non_match_style, *s)
Expand Down Expand Up @@ -942,5 +942,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 85a21a9

Please sign in to comment.