summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDan Davison <dandavison7@gmail.com>2021-12-06 22:36:15 -0500
committerDan Davison <dandavison7@gmail.com>2021-12-12 00:20:52 -0500
commitc1d8867f80ee60002362df47d959eb927a108c86 (patch)
treec63ae5d63bb2eaf493a19499abdb4f6c9e96e5de
parent34b99407a7e4acda44feabf2c34a5d63c07f3d3a (diff)
Raw pass-through handling of --word-diff and --color-words output
The previous implementation (5895cfa) was incorrect. Fixes #829
-rw-r--r--src/handlers/hunk.rs20
-rw-r--r--src/paint.rs19
2 files changed, 30 insertions, 9 deletions
diff --git a/src/handlers/hunk.rs b/src/handlers/hunk.rs
index 8b97a2d8..09fa9186 100644
--- a/src/handlers/hunk.rs
+++ b/src/handlers/hunk.rs
@@ -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, _)
diff --git a/src/paint.rs b/src/paint.rs
index 59dd38a9..0852c9fa 100644
--- a/src/paint.rs
+++ b/src/paint.rs
@@ -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