Ref #829
This commit is contained in:
Dan Davison 2021-12-06 21:00:48 -05:00
parent 354731b95a
commit 9554f47ce9
3 changed files with 38 additions and 44 deletions

View File

@ -71,36 +71,22 @@ impl<'a> StateMachine<'a> {
}
let n_parents = diff_type.n_parents();
let line = self.painter.prepare(&self.line, n_parents);
let state = match self.config.inspect_raw_lines {
cli::InspectRawLines::True
if style::line_has_style_other_than(
&self.raw_line,
[*style::GIT_DEFAULT_MINUS_STYLE, self.config.git_minus_style].iter(),
) =>
{
let raw_line = self.painter.prepare_raw_line(&self.raw_line, n_parents);
HunkMinus(diff_type, Some(raw_line))
}
_ => HunkMinus(diff_type, None),
};
let raw_line = self.maybe_raw_line(
n_parents,
&[*style::GIT_DEFAULT_MINUS_STYLE, self.config.git_minus_style],
);
let state = HunkMinus(diff_type, raw_line);
self.painter.minus_lines.push((line, state.clone()));
state
}
Some(HunkPlus(diff_type, _)) => {
let n_parents = diff_type.n_parents();
let line = self.painter.prepare(&self.line, n_parents);
let state = match self.config.inspect_raw_lines {
cli::InspectRawLines::True
if style::line_has_style_other_than(
&self.raw_line,
[*style::GIT_DEFAULT_PLUS_STYLE, self.config.git_plus_style].iter(),
) =>
{
let raw_line = self.painter.prepare_raw_line(&self.raw_line, n_parents);
HunkPlus(diff_type, Some(raw_line))
}
_ => HunkPlus(diff_type, None),
};
let raw_line = self.maybe_raw_line(
n_parents,
&[*style::GIT_DEFAULT_PLUS_STYLE, self.config.git_plus_style],
);
let state = HunkPlus(diff_type, raw_line);
self.painter.plus_lines.push((line, state.clone()));
state
}
@ -109,8 +95,11 @@ impl<'a> StateMachine<'a> {
// sequence of consecutive minus (removed) and/or plus (added) lines). Process that
// subhunk and flush the line buffers.
self.painter.paint_buffered_minus_and_plus_lines();
self.painter.paint_zero_line(&self.line, diff_type.clone());
HunkZero(diff_type)
let n_parents = diff_type.n_parents();
let line = self.painter.prepare(&self.line, n_parents);
let state = State::HunkZero(diff_type);
self.painter.paint_zero_line(&line, state.clone());
state
}
_ => {
// The first character here could be e.g. '\' from '\ No newline at end of file'. This
@ -127,6 +116,16 @@ impl<'a> StateMachine<'a> {
self.painter.emit()?;
Ok(true)
}
fn maybe_raw_line(&self, n_parents: usize, non_raw_styles: &[style::Style]) -> Option<String> {
let emit_raw_line = self.config.inspect_raw_lines == cli::InspectRawLines::True
&& style::line_has_style_other_than(&self.raw_line, non_raw_styles);
if emit_raw_line {
Some(self.painter.prepare_raw_line(&self.raw_line, n_parents))
} else {
None
}
}
}
// Return the new state corresponding to `new_line`, given the previous state. A return value of

View File

@ -176,12 +176,10 @@ impl<'p> Painter<'p> {
self.plus_lines.clear();
}
pub fn paint_zero_line(&mut self, line: &str, diff_type: DiffType) {
let line = self.prepare(line, diff_type.n_parents());
let state = State::HunkZero(diff_type);
let lines = vec![(line, state.clone())];
pub fn paint_zero_line(&mut self, line: &str, state: State) {
let lines = &[(line.to_string(), state.clone())];
let syntax_style_sections =
get_syntax_style_sections_for_lines(&lines, self.highlighter.as_mut(), self.config);
get_syntax_style_sections_for_lines(lines, self.highlighter.as_mut(), self.config);
let diff_style_sections = vec![(self.config.zero_style, lines[0].0.as_str())]; // TODO: compute style from state
if self.config.side_by_side {
@ -198,7 +196,7 @@ impl<'p> Painter<'p> {
);
} else {
Painter::paint_lines(
&lines,
lines,
&syntax_style_sections,
&[diff_style_sections],
&[false],

View File

@ -355,7 +355,7 @@ lazy_static! {
};
}
pub fn line_has_style_other_than<'a>(line: &str, styles: impl Iterator<Item = &'a Style>) -> bool {
pub fn line_has_style_other_than(line: &str, styles: &[Style]) -> bool {
if !ansi::string_starts_with_ansi_style_sequence(line) {
return false;
}
@ -443,38 +443,35 @@ pub mod tests {
let plus_line_from_unconfigured_git = "\x1b[32m+\x1b[m\x1b[32m____\x1b[m\n";
// Unstyled lines should test negative, regardless of supplied styles.
assert!(!line_has_style_other_than("", [].iter()));
assert!(!line_has_style_other_than(
"",
[*GIT_DEFAULT_MINUS_STYLE].iter()
));
assert!(!line_has_style_other_than("", &[]));
assert!(!line_has_style_other_than("", &[*GIT_DEFAULT_MINUS_STYLE]));
// Lines from git should test negative when corresponding default is supplied
assert!(!line_has_style_other_than(
minus_line_from_unconfigured_git,
[*GIT_DEFAULT_MINUS_STYLE].iter()
&[*GIT_DEFAULT_MINUS_STYLE]
));
assert!(!line_has_style_other_than(
plus_line_from_unconfigured_git,
[*GIT_DEFAULT_PLUS_STYLE].iter()
&[*GIT_DEFAULT_PLUS_STYLE]
));
// Styled lines should test positive when unless their style is supplied.
assert!(line_has_style_other_than(
minus_line_from_unconfigured_git,
[*GIT_DEFAULT_PLUS_STYLE].iter()
&[*GIT_DEFAULT_PLUS_STYLE]
));
assert!(line_has_style_other_than(
minus_line_from_unconfigured_git,
[].iter()
&[]
));
assert!(line_has_style_other_than(
plus_line_from_unconfigured_git,
[*GIT_DEFAULT_MINUS_STYLE].iter()
&[*GIT_DEFAULT_MINUS_STYLE]
));
assert!(line_has_style_other_than(
plus_line_from_unconfigured_git,
[].iter()
&[]
));
}