Skip to content

Commit

Permalink
Simplify preparation of hunk header code fragment
Browse files Browse the repository at this point in the history
  • Loading branch information
dandavison committed Dec 28, 2020
1 parent 37813de commit e94327e
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/delta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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('+') => {
Expand All @@ -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(' ') => {
Expand Down
10 changes: 3 additions & 7 deletions src/hunk_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 4 additions & 5 deletions src/paint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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()
}
}

Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit e94327e

Please sign in to comment.