Skip to content

Commit

Permalink
files: replace precomputed has_left/right_content flags with functions
Browse files Browse the repository at this point in the history
I don't think the iteration cost would matter here, and it doesn't make sense
that has_left/right_content are cached whereas is_unmodified() isn't.
  • Loading branch information
yuja committed Aug 16, 2024
1 parent cca5277 commit a973c7b
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
4 changes: 2 additions & 2 deletions cli/src/diff_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,7 @@ fn show_color_words_diff_line(
formatter: &mut dyn Formatter,
diff_line: &DiffLine,
) -> io::Result<()> {
if diff_line.has_left_content {
if diff_line.has_left_content() {
formatter.with_label("removed", |formatter| {
write!(
formatter.labeled("line_number"),
Expand All @@ -478,7 +478,7 @@ fn show_color_words_diff_line(
} else {
write!(formatter, " ")?;
}
if diff_line.has_right_content {
if diff_line.has_right_content() {
formatter.with_label("added", |formatter| {
write!(
formatter.labeled("line_number"),
Expand Down
22 changes: 12 additions & 10 deletions lib/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,26 @@ use crate::merge::{trivial_merge, Merge};
pub struct DiffLine<'a> {
pub left_line_number: u32,
pub right_line_number: u32,
pub has_left_content: bool,
pub has_right_content: bool,
pub hunks: Vec<(DiffLineHunkSide, &'a BStr)>,
}

impl DiffLine<'_> {
fn reset_line(&mut self) {
self.has_left_content = false;
self.has_right_content = false;
self.hunks.clear();
}

pub fn has_left_content(&self) -> bool {
self.hunks
.iter()
.any(|&(side, _)| side != DiffLineHunkSide::Right)
}

pub fn has_right_content(&self) -> bool {
self.hunks
.iter()
.any(|&(side, _)| side != DiffLineHunkSide::Left)
}

pub fn is_unmodified(&self) -> bool {
self.hunks
.iter()
Expand Down Expand Up @@ -72,8 +80,6 @@ where
let current_line = DiffLine {
left_line_number: 1,
right_line_number: 1,
has_left_content: false,
has_right_content: false,
hunks: vec![],
};
DiffLineIterator {
Expand Down Expand Up @@ -102,8 +108,6 @@ where
DiffHunk::Matching(text) => {
let lines = text.split_inclusive(|b| *b == b'\n').map(BStr::new);
for line in lines {
self.current_line.has_left_content = true;
self.current_line.has_right_content = true;
self.current_line.hunks.push((DiffLineHunkSide::Both, line));
if line.ends_with(b"\n") {
self.queued_lines.push_back(self.current_line.clone());
Expand All @@ -119,7 +123,6 @@ where
.expect("hunk should have exactly two inputs");
let left_lines = left_text.split_inclusive(|b| *b == b'\n').map(BStr::new);
for left_line in left_lines {
self.current_line.has_left_content = true;
self.current_line
.hunks
.push((DiffLineHunkSide::Left, left_line));
Expand All @@ -131,7 +134,6 @@ where
}
let right_lines = right_text.split_inclusive(|b| *b == b'\n').map(BStr::new);
for right_line in right_lines {
self.current_line.has_right_content = true;
self.current_line
.hunks
.push((DiffLineHunkSide::Right, right_line));
Expand Down

0 comments on commit a973c7b

Please sign in to comment.