diff options
author | Dan Davison <dandavison7@gmail.com> | 2020-12-27 18:19:05 +0000 |
---|---|---|
committer | Dan Davison <dandavison7@gmail.com> | 2020-12-27 21:50:22 +0000 |
commit | f680b51646750adb9f6a9977ffb6b34dcb31801f (patch) | |
tree | ff1c0a7c5b7a09e29bd989bf353a140129662e38 | |
parent | 38ed1fd07df482a26a7968639669d0191c7ae3e2 (diff) |
Simplify preparation of hunk header code fragment
-rw-r--r-- | src/delta.rs | 4 | ||||
-rw-r--r-- | src/hunk_header.rs | 10 | ||||
-rw-r--r-- | src/paint.rs | 9 |
3 files changed, 9 insertions, 14 deletions
diff --git a/src/delta.rs b/src/delta.rs index 238ed3e3..52395f07 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 ec4db03a..abf042ca 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 bbff5a8b..bf85fb13 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, |