From f680b51646750adb9f6a9977ffb6b34dcb31801f Mon Sep 17 00:00:00 2001 From: Dan Davison Date: Sun, 27 Dec 2020 18:19:05 +0000 Subject: [PATCH] Simplify preparation of hunk header code fragment --- src/delta.rs | 4 ++-- src/hunk_header.rs | 10 +++------- src/paint.rs | 9 ++++----- 3 files changed, 9 insertions(+), 14 deletions(-) diff --git a/src/delta.rs b/src/delta.rs index 238ed3e38..52395f07a 100644 --- a/src/delta.rs +++ b/src/delta.rs @@ -442,7 +442,7 @@ fn handle_hunk_line( }; painter .minus_lines - .push((painter.prepare(&line, true), state.clone())); + .push((painter.prepare(&line), state.clone())); state } Some('+') => { @@ -459,7 +459,7 @@ fn handle_hunk_line( }; painter .plus_lines - .push((painter.prepare(&line, true), state.clone())); + .push((painter.prepare(&line), state.clone())); state } Some(' ') => { diff --git a/src/hunk_header.rs b/src/hunk_header.rs index ec4db03a8..abf042caa 100644 --- a/src/hunk_header.rs +++ b/src/hunk_header.rs @@ -75,16 +75,12 @@ fn _write_hunk_header( config: &Config, ) -> std::io::Result<()> { let (mut draw_fn, _, decoration_ansi_term_style) = _get_draw_fn(config); - // Adjust the hunk-header-line before paint_lines. - // However in the case of color_only mode, - // we'll just use raw_line because we can't change raw_line structure. let line = if config.color_only { format!(" {}", &line) + } else if !raw_code_fragment.is_empty() { + format!("{} ", raw_code_fragment) } else { - match painter.prepare(&raw_code_fragment, false) { - s if !s.is_empty() => format!("{} ", s), - s => s, - } + "".to_string() }; // Add a blank line below the hunk-header-line for readability, unless diff --git a/src/paint.rs b/src/paint.rs index bbff5a8b6..bf85fb138 100644 --- a/src/paint.rs +++ b/src/paint.rs @@ -78,8 +78,7 @@ impl<'a> Painter<'a> { // Terminating with newline character is necessary for many of the sublime syntax definitions to // highlight correctly. // See https://docs.rs/syntect/3.2.0/syntect/parsing/struct.SyntaxSetBuilder.html#method.add_from_folder - pub fn prepare(&self, line: &str, append_newline: bool) -> String { - let terminator = if append_newline { "\n" } else { "" }; + pub fn prepare(&self, line: &str) -> String { if !line.is_empty() { let mut line = line.graphemes(true); @@ -90,9 +89,9 @@ impl<'a> Painter<'a> { // TODO: Things should, but do not, work if this leading space is omitted at this stage. // See comment in align::Alignment::new. line.next(); - format!(" {}{}", self.expand_tabs(line), terminator) + format!(" {}\n", self.expand_tabs(line)) } else { - terminator.to_string() + "\n".to_string() } } @@ -201,7 +200,7 @@ impl<'a> Painter<'a> { } else { None }; - let lines = vec![(self.prepare(line, true), state.clone())]; + let lines = vec![(self.prepare(line), state.clone())]; let syntax_style_sections = Painter::get_syntax_style_sections_for_lines( &lines, &state,