diff options
author | Dan Davison <dandavison7@gmail.com> | 2021-11-28 13:13:58 -0500 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-11-28 18:28:22 -0500 |
commit | 85367a6ab2e8db3fe5c348bd803f1d914a943b61 (patch) | |
tree | fd7a09d0bde71045b0494200aedab8e0025024d7 /src | |
parent | af07e54c7b5029a8f06177a9158f5f79cc211b81 (diff) |
Do not fill entirely new/removed lines with non-emph style
Diffstat (limited to 'src')
-rw-r--r-- | src/features/side_by_side.rs | 18 | ||||
-rw-r--r-- | src/paint.rs | 30 | ||||
-rw-r--r-- | src/tests/ansi_test_utils.rs | 1 |
3 files changed, 44 insertions, 5 deletions
diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs index 2a49728b..4f02c47c 100644 --- a/src/features/side_by_side.rs +++ b/src/features/side_by_side.rs @@ -6,6 +6,7 @@ use crate::ansi; use crate::cli; use crate::config::{self, delta_unreachable, Config}; use crate::delta::State; +use crate::edits; use crate::features::{line_numbers, OptionValueFunction}; use crate::minusplus::*; use crate::paint::{BgFillMethod, BgShouldFill, LineSections, Painter}; @@ -111,6 +112,7 @@ pub fn paint_minus_and_plus_lines_side_by_side( lines: LeftRight<&Vec<(String, State)>>, syntax_sections: LeftRight<Vec<LineSections<SyntectStyle>>>, diff_sections: LeftRight<Vec<LineSections<Style>>>, + lines_have_homolog: LeftRight<Vec<bool>>, line_alignment: Vec<(Option<usize>, Option<usize>)>, line_numbers_data: &mut Option<LineNumbersData>, output_buffer: &mut String, @@ -164,12 +166,18 @@ pub fn paint_minus_and_plus_lines_side_by_side( } else { (line_alignment, line_states, syntax_sections, diff_sections) }; + let lines_have_homolog = if should_wrap { + edits::make_lines_have_homolog(&line_alignment) + } else { + lines_have_homolog + }; for (minus_line_index, plus_line_index) in line_alignment { output_buffer.push_str(&paint_left_panel_minus_line( minus_line_index, &syntax_sections[Left], &diff_sections[Left], + &lines_have_homolog[Left], match minus_line_index { Some(i) => &line_states[Left][i], None => &State::HunkMinus(None), @@ -182,6 +190,7 @@ pub fn paint_minus_and_plus_lines_side_by_side( plus_line_index, &syntax_sections[Right], &diff_sections[Right], + &lines_have_homolog[Right], match plus_line_index { Some(i) => &line_states[Right][i], None => &State::HunkPlus(None), @@ -237,6 +246,7 @@ pub fn paint_zero_lines_side_by_side<'a>( panel_line_is_empty, Some(line_index), &diff_style_sections, + None, &state, *panel_side, background_color_extends_to_terminal_width, @@ -253,6 +263,7 @@ fn paint_left_panel_minus_line<'a>( line_index: Option<usize>, syntax_style_sections: &[LineSections<'a, SyntectStyle>], diff_style_sections: &[LineSections<'a, Style>], + lines_have_homolog: &[bool], state: &'a State, line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, background_color_extends_to_terminal_width: BgShouldFill, @@ -272,6 +283,7 @@ fn paint_left_panel_minus_line<'a>( panel_line_is_empty, line_index, diff_style_sections, + Some(lines_have_homolog), state, Left, background_color_extends_to_terminal_width, @@ -286,6 +298,7 @@ fn paint_right_panel_plus_line<'a>( line_index: Option<usize>, syntax_style_sections: &[LineSections<'a, SyntectStyle>], diff_style_sections: &[LineSections<'a, Style>], + lines_have_homolog: &[bool], state: &'a State, line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, background_color_extends_to_terminal_width: BgShouldFill, @@ -306,6 +319,7 @@ fn paint_right_panel_plus_line<'a>( panel_line_is_empty, line_index, diff_style_sections, + Some(lines_have_homolog), state, Right, background_color_extends_to_terminal_width, @@ -320,6 +334,7 @@ fn get_right_fill_style_for_panel<'a>( line_is_empty: bool, line_index: Option<usize>, diff_style_sections: &[LineSections<'a, Style>], + lines_have_homolog: Option<&[bool]>, state: &State, panel_side: PanelSide, background_color_extends_to_terminal_width: BgShouldFill, @@ -339,6 +354,7 @@ fn get_right_fill_style_for_panel<'a>( let (bg_fill_mode, fill_style) = Painter::get_should_right_fill_background_color_and_fill_style( &diff_style_sections[index], + lines_have_homolog.map(|h| h[index]), state, background_color_extends_to_terminal_width, config, @@ -435,6 +451,7 @@ fn pad_panel_line_to_width<'a>( panel_line_is_empty: bool, line_index: Option<usize>, diff_style_sections: &[LineSections<'a, Style>], + lines_have_homolog: Option<&[bool]>, state: &State, panel_side: PanelSide, background_color_extends_to_terminal_width: BgShouldFill, @@ -472,6 +489,7 @@ fn pad_panel_line_to_width<'a>( panel_line_is_empty, line_index, diff_style_sections, + lines_have_homolog, state, panel_side, background_color_extends_to_terminal_width, diff --git a/src/paint.rs b/src/paint.rs index ef1bb005..a4253133 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -190,6 +190,7 @@ impl<'p> Painter<'p> { lines, syntax_style_sections, diff_style_sections, + lines_have_homolog, line_alignment, &mut self.line_numbers_data, &mut self.output_buffer, @@ -202,6 +203,7 @@ impl<'p> Painter<'p> { lines[Minus], &syntax_style_sections[Minus], &diff_style_sections[Minus], + &lines_have_homolog[Minus], &mut self.output_buffer, self.config, &mut self.line_numbers_data.as_mut(), @@ -219,6 +221,7 @@ impl<'p> Painter<'p> { lines[Plus], &syntax_style_sections[Plus], &diff_style_sections[Plus], + &lines_have_homolog[Plus], &mut self.output_buffer, self.config, &mut self.line_numbers_data.as_mut(), @@ -270,6 +273,7 @@ impl<'p> Painter<'p> { &lines, &syntax_style_sections, &[diff_style_sections], + &[false], &mut self.output_buffer, self.config, &mut self.line_numbers_data.as_mut(), @@ -287,6 +291,7 @@ impl<'p> Painter<'p> { lines: &'a [(String, State)], syntax_style_sections: &[LineSections<'a, SyntectStyle>], diff_style_sections: &[LineSections<'a, Style>], + lines_have_homolog: &[bool], output_buffer: &mut String, config: &config::Config, line_numbers_data: &mut Option<&mut line_numbers::LineNumbersData>, @@ -301,10 +306,11 @@ impl<'p> Painter<'p> { // 2. We must ensure that we fill rightwards with the appropriate // non-emph background color. In that case we don't use the last // style of the line, because this might be emph. - for (((_, state), syntax_sections), diff_sections) in lines + for ((((_, state), syntax_sections), diff_sections), &line_has_homolog) in lines .iter() .zip_eq(syntax_style_sections) .zip_eq(diff_style_sections) + .zip_eq(lines_have_homolog) { let (mut line, line_is_empty) = Painter::paint_line( syntax_sections, @@ -318,6 +324,7 @@ impl<'p> Painter<'p> { let (bg_fill_mode, fill_style) = Painter::get_should_right_fill_background_color_and_fill_style( diff_sections, + Some(line_has_homolog), state, background_color_extends_to_terminal_width, config, @@ -372,6 +379,7 @@ impl<'p> Painter<'p> { &lines, &syntax_style_sections, &diff_style_sections, + &[false], &mut self.output_buffer, self.config, &mut None, @@ -385,14 +393,27 @@ impl<'p> Painter<'p> { /// the style for doing so. pub fn get_should_right_fill_background_color_and_fill_style( diff_sections: &[(Style, &str)], + line_has_homolog: Option<bool>, state: &State, background_color_extends_to_terminal_width: BgShouldFill, config: &config::Config, ) -> (Option<BgFillMethod>, Style) { - let non_emph_style = match state { - State::HunkMinus(None) | State::HunkMinusWrapped => config.minus_non_emph_style, + let fill_style = match state { + State::HunkMinus(None) | State::HunkMinusWrapped => { + if let Some(true) = line_has_homolog { + config.minus_non_emph_style + } else { + config.minus_style + } + } State::HunkZero | State::HunkZeroWrapped => config.zero_style, - State::HunkPlus(None) | State::HunkPlusWrapped => config.plus_non_emph_style, + State::HunkPlus(None) | State::HunkPlusWrapped => { + if let Some(true) = line_has_homolog { + config.plus_non_emph_style + } else { + config.plus_style + } + } State::HunkMinus(Some(_)) | State::HunkPlus(Some(_)) => { if !diff_sections.is_empty() { diff_sections[diff_sections.len() - 1].0 @@ -403,7 +424,6 @@ impl<'p> Painter<'p> { State::Blame(_, _) => diff_sections[0].0, _ => config.null_style, }; - let fill_style = non_emph_style; match ( fill_style.get_background_color().is_some(), diff --git a/src/tests/ansi_test_utils.rs b/src/tests/ansi_test_utils.rs index e2b637f5..04555b51 100644 --- a/src/tests/ansi_test_utils.rs +++ b/src/tests/ansi_test_utils.rs @@ -146,6 +146,7 @@ pub mod ansi_test_utils { &lines, &syntax_style_sections, &diff_style_sections, + &[false], &mut output_buffer, config, &mut None, |