Skip to content

Commit 8cdb59f

Browse files
committed
Normalise newlines before diffing
This produces good, consistent results on different trailing newlines before, consistent with the behaviour before 7edd2a8 (see #755).
1 parent f58c9e0 commit 8cdb59f

File tree

3 files changed

+28
-16
lines changed

3 files changed

+28
-16
lines changed

sample_files/compare.expected

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ sample_files/ada_1.adb sample_files/ada_2.adb
55
eb3b0e12e239ae33789136380e81406b -
66

77
sample_files/added_line_1.txt sample_files/added_line_2.txt
8-
f935214c79833318d39364145d36d8e2 -
8+
8a1587e6b5fc53f2ec2ac665a5d00372 -
99

1010
sample_files/align_footer_1.txt sample_files/align_footer_2.txt
1111
d640bd2de31e56a39f0efb92aff0f379 -
@@ -23,7 +23,7 @@ sample_files/bad_combine_1.rs sample_files/bad_combine_2.rs
2323
f5051bf7d2b8afa3a677388cbd458891 -
2424

2525
sample_files/big_text_hunk_1.txt sample_files/big_text_hunk_2.txt
26-
dcc22684c2a200afdd583d621b47dfa3 -
26+
fc26d41a5ff771670e04033b177973d2 -
2727

2828
sample_files/change_outer_1.el sample_files/change_outer_2.el
2929
2b9334a4cc72da63bba28eff958f0038 -
@@ -74,7 +74,7 @@ sample_files/erlang_1.erl sample_files/erlang_2.erl
7474
dccdb8f65d2f099ab1a8cb66011376a2 -
7575

7676
sample_files/f_sharp_1.fs sample_files/f_sharp_2.fs
77-
1a9173c15a42c1ebb9522109df172faf -
77+
0a6651925acadf366f213d15968ae353 -
7878

7979
sample_files/hack_1.php sample_files/hack_2.php
8080
c2bb0aa7d7b07d6ced79f6a5363e878b -
@@ -215,7 +215,7 @@ sample_files/racket_1.rkt sample_files/racket_2.rkt
215215
605aec93fa7b89d08e5d8ed56ad3c1be -
216216

217217
sample_files/repeated_line_no_eol_1.txt sample_files/repeated_line_no_eol_2.txt
218-
ac714893a2d28dc0204855d308972ccd -
218+
3786f55d2c9b1897e866b4602e50408d -
219219

220220
sample_files/ruby_1.rb sample_files/ruby_2.rb
221221
d4d591902030355656f5c18c78f965a6 -
@@ -302,5 +302,5 @@ sample_files/yaml_1.yaml sample_files/yaml_2.yaml
302302
f068239fc7bade0e6de96d81136c1ac5 -
303303

304304
sample_files/zig_1.zig sample_files/zig_2.zig
305-
e36d1ea126b8b68e3344434bb63f205e -
305+
4516796003b81f35bfa57d84bb7c0cbe -
306306

src/display/side_by_side.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -407,8 +407,15 @@ pub(crate) fn print(
407407
let mut prev_lhs_line_num = None;
408408
let mut prev_rhs_line_num = None;
409409

410-
let lhs_lines = split_on_newlines(lhs_src).collect::<Vec<_>>();
411-
let rhs_lines = split_on_newlines(rhs_src).collect::<Vec<_>>();
410+
let mut lhs_lines = split_on_newlines(lhs_src).collect::<Vec<_>>();
411+
let mut rhs_lines = split_on_newlines(rhs_src).collect::<Vec<_>>();
412+
413+
if lhs_lines.last() == Some(&"") && lhs_lines.len() > 1 {
414+
lhs_lines.pop();
415+
}
416+
if rhs_lines.last() == Some(&"") && rhs_lines.len() > 1 {
417+
rhs_lines.pop();
418+
}
412419

413420
let matched_lines = all_matched_lines_filled(lhs_mps, rhs_mps, &lhs_lines, &rhs_lines);
414421
let mut matched_lines_to_print = &matched_lines[..];

src/main.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -373,16 +373,21 @@ fn diff_file(
373373
rhs_src.retain(|c| c != '\r');
374374
}
375375

376-
// If "foo" is one line, is "foo\n" two lines? Generally we want
377-
// to care about newlines when deciding whether content differs.
376+
// Ensure that lhs_src and rhs_src both have trailing
377+
// newlines.
378378
//
379-
// Ending a file with a trailing newline is extremely common
380-
// though. If both files have a trailing newline, consider "foo\n"
381-
// to be "foo" so we don't end up displaying a blank line on both
382-
// sides.
383-
if lhs_src.ends_with('\n') && rhs_src.ends_with('\n') {
384-
lhs_src.pop();
385-
rhs_src.pop();
379+
// This is important when textually diffing files that don't have
380+
// a trailing newline, e.g. "foo\n\bar\n" versus "foo". We want to
381+
// consider `foo` to be unchanged in this case.
382+
//
383+
// Theoretically a tree-sitter parser coud change its AST due to
384+
// the additional trailing newline, but it seems vanishingly
385+
// unlikely.
386+
if !lhs_src.is_empty() && !lhs_src.ends_with('\n') {
387+
lhs_src.push('\n');
388+
}
389+
if !rhs_src.is_empty() && !rhs_src.ends_with('\n') {
390+
rhs_src.push('\n');
386391
}
387392

388393
let mut extra_info = renamed;

0 commit comments

Comments
 (0)