From a24cacabc18162f69d6ec4706d6ee54c18a3633c Mon Sep 17 00:00:00 2001 From: Jackson Popkin Date: Fri, 8 Jul 2022 15:44:31 -0400 Subject: [PATCH] Fix handling of non-match highlight 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. --- src/handlers/grep.rs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/handlers/grep.rs b/src/handlers/grep.rs index afa2a148b..ce5f81fff 100644 --- a/src/handlers/grep.rs +++ b/src/handlers/grep.rs @@ -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) @@ -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 {") + ])) + ); } }