Skip to content

Commit 8661279

Browse files
committed
Try ignoring trailing newlines in line-based differ
1 parent 0973998 commit 8661279

File tree

1 file changed

+48
-5
lines changed

1 file changed

+48
-5
lines changed

src/line_parser.rs

Lines changed: 48 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use lazy_static::lazy_static;
44
use line_numbers::{LinePositions, SingleLineSpan};
55
use regex::Regex;
6+
use std::hash::Hash;
67

78
use crate::words::split_words;
89
use crate::{
@@ -73,24 +74,66 @@ fn merge_novel<'a>(
7374
res
7475
}
7576

77+
#[derive(Debug, Clone)]
78+
struct StringIgnoringNewline<'a>(&'a str);
79+
80+
impl PartialEq for StringIgnoringNewline<'_> {
81+
fn eq(&self, other: &Self) -> bool {
82+
let mut s = self.0;
83+
if s.ends_with('\n') {
84+
s = &s[..s.len() - 1];
85+
}
86+
87+
let mut other_s = other.0;
88+
if other_s.ends_with('\n') {
89+
other_s = &other_s[..other_s.len() - 1];
90+
}
91+
92+
s == other_s
93+
}
94+
}
95+
96+
impl Eq for StringIgnoringNewline<'_> {}
97+
98+
impl Hash for StringIgnoringNewline<'_> {
99+
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
100+
let mut s = self.0;
101+
if s.ends_with('\n') {
102+
s = &s[..s.len() - 1];
103+
}
104+
105+
s.hash(state);
106+
}
107+
}
108+
76109
fn changed_parts<'a>(
77110
src: &'a str,
78111
opposite_src: &'a str,
79112
) -> Vec<(TextChangeKind, Vec<&'a str>, Vec<&'a str>)> {
80-
let src_lines = split_lines_keep_newline(src);
81-
let opposite_src_lines = split_lines_keep_newline(opposite_src);
113+
let src_lines = split_lines_keep_newline(src)
114+
.into_iter()
115+
.map(StringIgnoringNewline)
116+
.collect::<Vec<_>>();
117+
let opposite_src_lines = split_lines_keep_newline(opposite_src)
118+
.into_iter()
119+
.map(StringIgnoringNewline)
120+
.collect::<Vec<_>>();
82121

83122
let mut res: Vec<(TextChangeKind, Vec<&'a str>, Vec<&'a str>)> = vec![];
84123
for diff_res in myers_diff::slice_unique_by_hash(&src_lines, &opposite_src_lines) {
85124
match diff_res {
86125
myers_diff::DiffResult::Left(line) => {
87-
res.push((TextChangeKind::Novel, vec![line], vec![]));
126+
res.push((TextChangeKind::Novel, vec![line.0], vec![]));
88127
}
89128
myers_diff::DiffResult::Both(line, opposite_line) => {
90-
res.push((TextChangeKind::Unchanged, vec![line], vec![opposite_line]));
129+
res.push((
130+
TextChangeKind::Unchanged,
131+
vec![line.0],
132+
vec![opposite_line.0],
133+
));
91134
}
92135
myers_diff::DiffResult::Right(opposite_line) => {
93-
res.push((TextChangeKind::Novel, vec![], vec![opposite_line]));
136+
res.push((TextChangeKind::Novel, vec![], vec![opposite_line.0]));
94137
}
95138
}
96139
}

0 commit comments

Comments
 (0)