Skip to content

Commit

Permalink
Raw pass-through handling of --word-diff and --color-words output
Browse files Browse the repository at this point in the history
The previous implementation (5895cfa) was incorrect.

Fixes #829
  • Loading branch information
dandavison committed Dec 9, 2021
1 parent ce661e1 commit 6208c8a
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 9 deletions.
20 changes: 16 additions & 4 deletions src/handlers/hunk.rs
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
Expand All @@ -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 {
Expand All @@ -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, _)
Expand Down
19 changes: 14 additions & 5 deletions src/paint.rs
Expand Up @@ -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(),
Expand All @@ -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,
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 6208c8a

Please sign in to comment.