Raw pass-through handling of --word-diff and --color-words output

The previous implementation (5895cfa) was incorrect.

Fixes #829
This commit is contained in:
Dan Davison 2021-12-06 22:36:15 -05:00
parent 34b99407a7
commit c1d8867f80
2 changed files with 30 additions and 9 deletions

View File

@ -33,7 +33,7 @@ impl<'a> StateMachine<'a> {
| State::HunkZero(_, _)
| State::HunkMinus(_, _)
| State::HunkPlus(_, _)
) && !&*IS_WORD_DIFF
)
}
/// Handle a hunk line, i.e. a minus line, a plus line, or an unchanged line.
@ -95,7 +95,12 @@ 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();
let n_parents = diff_type.n_parents();
let n_parents = if *IS_WORD_DIFF {
// HACK: WordDiff should probably be a distinct top-level line state
0
} else {
diff_type.n_parents()
};
let line = self.painter.prepare(&self.line, n_parents);
let raw_line = self.maybe_raw_line(n_parents, &[]);
let state = State::HunkZero(diff_type, raw_line);
@ -119,8 +124,10 @@ impl<'a> StateMachine<'a> {
}
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);
// HACK: WordDiff should probably be a distinct top-level line state
let emit_raw_line = *IS_WORD_DIFF
|| 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 {
@ -136,6 +143,11 @@ fn new_line_state(new_line: &str, prev_state: &State) -> Option<State> {
use MergeParents::*;
use State::*;
if *IS_WORD_DIFF {
// HACK: WordDiff should probably be a distinct top-level line state
return Some(HunkZero(Unified, None));
}
let diff_type = match prev_state {
HunkMinus(Unified, _)
| HunkZero(Unified, _)

View File

@ -180,14 +180,21 @@ impl<'p> Painter<'p> {
let lines = &[(line.to_string(), state.clone())];
let syntax_style_sections =
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
let mut diff_style_sections = vec![vec![(self.config.zero_style, lines[0].0.as_str())]]; // TODO: compute style from state
Painter::update_styles(
lines,
&mut diff_style_sections,
None,
None,
&[false],
self.config,
);
if self.config.side_by_side {
// `lines[0].0` so the line has the '\n' already added (as in the +- case)
side_by_side::paint_zero_lines_side_by_side(
&lines[0].0,
syntax_style_sections,
vec![diff_style_sections],
diff_style_sections,
&mut self.output_buffer,
self.config,
&mut self.line_numbers_data.as_mut(),
@ -198,7 +205,7 @@ impl<'p> Painter<'p> {
Painter::paint_lines(
lines,
&syntax_style_sections,
&[diff_style_sections],
diff_style_sections.as_slice(),
&[false],
&mut self.output_buffer,
self.config,
@ -564,7 +571,9 @@ impl<'p> Painter<'p> {
.zip_eq(lines_style_sections)
.zip_eq(lines_have_homolog)
{
if let State::HunkMinus(_, Some(raw_line)) | State::HunkPlus(_, Some(raw_line)) = state
if let State::HunkMinus(_, Some(raw_line))
| State::HunkZero(_, Some(raw_line))
| State::HunkPlus(_, Some(raw_line)) = state
{
// raw_line is captured in handle_hunk_line under certain conditions. If we have
// done so, then overwrite the style sections with styles parsed directly from the