diff options
author | Thomas Otto <th1000s@posteo.net> | 2021-10-26 22:28:50 +0200 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2021-10-26 18:05:30 -0400 |
commit | fbb654d4370139b2d0fdef9c1b908fae9c54d797 (patch) | |
tree | f010ae42eb3b2b8a94fb48ecee0d95df957ac181 | |
parent | 0745f853d4bed52aca0b6739ac452d54ff54a153 (diff) |
Simplify incrementing of line numbers
-rw-r--r-- | src/features/line_numbers.rs | 9 | ||||
-rw-r--r-- | src/features/side_by_side.rs | 24 | ||||
-rw-r--r-- | src/paint.rs | 5 |
3 files changed, 10 insertions, 28 deletions
diff --git a/src/features/line_numbers.rs b/src/features/line_numbers.rs index 56cb3daa..3afce6b1 100644 --- a/src/features/line_numbers.rs +++ b/src/features/line_numbers.rs @@ -66,6 +66,7 @@ pub fn linenumbers_and_styles<'a>( line_numbers_data: &'a mut LineNumbersData, state: &State, config: &'a config::Config, + increment: bool, ) -> Option<(MinusPlus<Option<usize>>, MinusPlus<Style>)> { let nr_left = line_numbers_data.line_number[Left]; let nr_right = line_numbers_data.line_number[Right]; @@ -76,18 +77,18 @@ pub fn linenumbers_and_styles<'a>( ); let ((minus_number, plus_number), (minus_style, plus_style)) = match state { State::HunkMinus(_) => { - line_numbers_data.line_number[Left] += 1; + line_numbers_data.line_number[Left] += increment as usize; ((Some(nr_left), None), (minus_style, plus_style)) } State::HunkMinusWrapped => ((None, None), (minus_style, plus_style)), State::HunkZero => { - line_numbers_data.line_number[Left] += 1; - line_numbers_data.line_number[Right] += 1; + line_numbers_data.line_number[Left] += increment as usize; + line_numbers_data.line_number[Right] += increment as usize; ((Some(nr_left), Some(nr_right)), (zero_style, zero_style)) } State::HunkZeroWrapped => ((None, None), (zero_style, zero_style)), State::HunkPlus(_) => { - line_numbers_data.line_number[Right] += 1; + line_numbers_data.line_number[Right] += increment as usize; ((None, Some(nr_right)), (minus_style, plus_style)) } State::HunkPlusWrapped => ((None, None), (minus_style, plus_style)), diff --git a/src/features/side_by_side.rs b/src/features/side_by_side.rs index 1485254c..b052f97c 100644 --- a/src/features/side_by_side.rs +++ b/src/features/side_by_side.rs @@ -197,15 +197,6 @@ pub fn paint_zero_lines_side_by_side<'a>( config, ); output_buffer.push_str(&panel_line); - - if *panel_side == Left && state != State::HunkZeroWrapped { - // TODO: Avoid doing the superimpose_style_sections work twice. - // HACK: These are getting incremented twice, so knock them back down once. - if let Some(d) = line_numbers_data.as_mut() { - d.line_number[Left] -= 1; - d.line_number[Right] -= 1; - } - } } output_buffer.push('\n'); } @@ -386,21 +377,6 @@ fn paint_minus_or_plus_panel_line<'a>( config, ); - // Knock back down spuriously incremented line numbers. See comment above. - match (state, &state_for_line_numbers_field) { - (s, t) if s == t => {} - (State::HunkPlus(_), State::HunkMinus(_)) => { - if let Some(d) = line_numbers_data.as_mut() { - d.line_number[Left] -= 1; - } - } - (State::HunkMinus(_), State::HunkPlus(_)) => { - if let Some(d) = line_numbers_data.as_mut() { - d.line_number[Right] -= 1; - } - } - _ => unreachable!(), - } (line, line_is_empty) } diff --git a/src/paint.rs b/src/paint.rs index bcf3e941..2be015e3 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -532,10 +532,15 @@ impl<'a> Painter<'a> { let output_line_numbers = config.line_numbers && line_numbers_data.is_some(); if output_line_numbers { + // Unified diff lines are printed in one go, but side-by-side lines + // are printed in two parts, so do not increment line numbers when the + // first (left) part is printed. + let increment = !matches!(side_by_side_panel, Some(side_by_side::Left)); if let Some((line_numbers, styles)) = line_numbers::linenumbers_and_styles( line_numbers_data.as_mut().unwrap(), state, config, + increment, ) { ansi_strings.extend(line_numbers::format_and_paint_line_numbers( line_numbers_data.as_ref().unwrap(), |