From a1b5a06cb53602982b47e5d7199e09cb598756e2 Mon Sep 17 00:00:00 2001 From: Thomas Otto <78380144+th1000s@users.noreply.github.com> Date: Sat, 21 Aug 2021 05:19:42 +0200 Subject: Fix empty line highlighting (#642) --- src/paint.rs | 37 +++++++++++++++++++++++++------------ src/tests/test_example_diffs.rs | 9 +-------- 2 files changed, 26 insertions(+), 20 deletions(-) (limited to 'src') diff --git a/src/paint.rs b/src/paint.rs index 3b5010b2..0e569232 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -381,9 +381,9 @@ impl<'a> Painter<'a> { painted_prefix: Option, config: &config::Config, ) -> (String, bool) { - let output_line_numbers = config.line_numbers && line_numbers_data.is_some(); - let mut handled_prefix = false; let mut ansi_strings = Vec::new(); + + let output_line_numbers = config.line_numbers && line_numbers_data.is_some(); if output_line_numbers { ansi_strings.extend(line_numbers::format_and_paint_line_numbers( line_numbers_data.as_mut().unwrap(), @@ -407,27 +407,40 @@ impl<'a> Painter<'a> { } _ => {} } - let mut is_empty = true; - for (section_style, mut text) in superimpose_style_sections( + + let superimposed = superimpose_style_sections( syntax_sections, diff_sections, config.true_color, config.null_syntect_style, - ) { - if !handled_prefix { - if let Some(painted_prefix) = painted_prefix.clone() { - ansi_strings.push(painted_prefix); + ); + + let mut handled_prefix = false; + for (section_style, text) in &superimposed { + let text = if handled_prefix { + &text + } else { + // Remove what was originally the +/- prefix, see `prepare()`, after + // (if requested) re-inserting it with proper styling. + if let Some(ref painted_prefix) = painted_prefix { + ansi_strings.push(painted_prefix.clone()); } + if !text.is_empty() { - text.remove(0); + &text[1..] + } else { + &text } - handled_prefix = true; - } + }; + if !text.is_empty() { ansi_strings.push(section_style.paint(text)); - is_empty = false; } + handled_prefix = true; } + + // Only if syntax is empty (implies diff empty) can a line actually be empty. + let is_empty = syntax_sections.is_empty(); (ansi_term::ANSIStrings(&ansi_strings).to_string(), is_empty) } diff --git a/src/tests/test_example_diffs.rs b/src/tests/test_example_diffs.rs index c605715e..3f28d200 100644 --- a/src/tests/test_example_diffs.rs +++ b/src/tests/test_example_diffs.rs @@ -1472,14 +1472,7 @@ src/align.rs:71: impl<'a> Alignment<'a> { │ .to_string() ); } else { - let style = style::Style::from_str(empty_line_marker_style, None, None, true, false); - assert_eq!( - line, - &style - .ansi_term_style - .paint(ansi::ANSI_CSI_CLEAR_TO_BOL) - .to_string() - ); + assert_eq!(line, ""); } } -- cgit v1.2.3