From 9448fe665a4bebe801a8e247c5ddb7a3e4f57223 Mon Sep 17 00:00:00 2001 From: Martin von Zweigbergk Date: Sat, 26 Jun 2021 10:01:29 -0700 Subject: [PATCH] files: use diff::DiffHunk in DiffLine definition The new `diff::DiffHunk` type is very similar but more generic. We don't need the generality here. I just don't two very similar types with the same name. --- lib/src/files.rs | 21 +++++++++------------ src/commands.rs | 25 +++++++++++++++---------- 2 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/src/files.rs b/lib/src/files.rs index 9245921f9..904562a16 100644 --- a/lib/src/files.rs +++ b/lib/src/files.rs @@ -17,14 +17,7 @@ use std::fmt::{Debug, Error, Formatter}; use std::ops::Range; use crate::diff; -use crate::diff::SliceDiff; - -#[derive(PartialEq, Eq, Clone, Debug)] -pub enum DiffHunk<'a> { - Unmodified(&'a [u8]), - Added(&'a [u8]), - Removed(&'a [u8]), -} +use crate::diff::{DiffHunk, SliceDiff}; #[derive(PartialEq, Eq, Clone, Debug)] pub struct DiffLine<'a> { @@ -45,7 +38,7 @@ impl DiffLine<'_> { pub fn is_unmodified(&self) -> bool { self.hunks .iter() - .all(|hunk| matches!(hunk, DiffHunk::Unmodified(_))) + .all(|hunk| matches!(hunk, DiffHunk::Matching(_))) } } @@ -94,7 +87,7 @@ impl<'a> Iterator for DiffLineIterator<'a> { for line in lines { self.current_line.has_left_content = true; self.current_line.has_right_content = true; - self.current_line.hunks.push(DiffHunk::Unmodified(line)); + self.current_line.hunks.push(DiffHunk::Matching(line)); if line.ends_with(b"\n") { self.queued_lines.push_back(self.current_line.clone()); self.current_line.left_line_number += 1; @@ -107,7 +100,9 @@ impl<'a> Iterator for DiffLineIterator<'a> { let left_lines = left.split_inclusive(|b| *b == b'\n'); for left_line in left_lines { self.current_line.has_left_content = true; - self.current_line.hunks.push(DiffHunk::Removed(left_line)); + self.current_line + .hunks + .push(DiffHunk::Different(vec![left_line, b""])); if left_line.ends_with(b"\n") { self.queued_lines.push_back(self.current_line.clone()); self.current_line.left_line_number += 1; @@ -117,7 +112,9 @@ impl<'a> Iterator for DiffLineIterator<'a> { let right_lines = right.split_inclusive(|b| *b == b'\n'); for right_line in right_lines { self.current_line.has_right_content = true; - self.current_line.hunks.push(DiffHunk::Added(right_line)); + self.current_line + .hunks + .push(DiffHunk::Different(vec![b"", right_line])); if right_line.ends_with(b"\n") { self.queued_lines.push_back(self.current_line.clone()); self.current_line.right_line_number += 1; diff --git a/src/commands.rs b/src/commands.rs index 5b5110253..25af6c536 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -33,6 +33,7 @@ use itertools::Itertools; use jujutsu_lib::commit::Commit; use jujutsu_lib::commit_builder::CommitBuilder; use jujutsu_lib::dag_walk::topo_order_reverse; +use jujutsu_lib::diff::DiffHunk; use jujutsu_lib::evolution::{ DivergenceResolution, DivergenceResolver, OrphanResolution, OrphanResolver, }; @@ -1037,18 +1038,22 @@ fn print_diff_line(formatter: &mut dyn Formatter, diff_line: &DiffLine) -> io::R } for hunk in &diff_line.hunks { match hunk { - files::DiffHunk::Unmodified(data) => { + DiffHunk::Matching(data) => { formatter.write_bytes(data)?; } - files::DiffHunk::Removed(data) => { - formatter.add_label(String::from("left"))?; - formatter.write_bytes(data)?; - formatter.remove_label()?; - } - files::DiffHunk::Added(data) => { - formatter.add_label(String::from("right"))?; - formatter.write_bytes(data)?; - formatter.remove_label()?; + DiffHunk::Different(data) => { + let before = data[0]; + let after = data[1]; + if !before.is_empty() { + formatter.add_label(String::from("left"))?; + formatter.write_bytes(before)?; + formatter.remove_label()?; + } + if !after.is_empty() { + formatter.add_label(String::from("right"))?; + formatter.write_bytes(after)?; + formatter.remove_label()?; + } } } }