Skip to content

Commit e164aa4

Browse files
committed
fix: Show multiple line removals in Diff format
1 parent 5b355ce commit e164aa4

File tree

5 files changed

+53
-12
lines changed

5 files changed

+53
-12
lines changed

src/renderer/render.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,7 +1479,10 @@ fn emit_suggestion_default(
14791479
row_num += 1;
14801480
}
14811481

1482-
let file_lines = sm.span_to_lines(parts[0].span.clone());
1482+
let lo = parts.iter().map(|p| p.span.start).min().unwrap();
1483+
let hi = parts.iter().map(|p| p.span.end).max().unwrap();
1484+
1485+
let file_lines = sm.span_to_lines(lo..hi);
14831486
let (line_start, line_end) = if suggestion.fold {
14841487
// We use the original span to get original line_start
14851488
sm.span_to_locations(parts[0].original_span.clone())
@@ -1613,6 +1616,7 @@ fn emit_suggestion_default(
16131616
if let DisplaySuggestion::Diff | DisplaySuggestion::Underline | DisplaySuggestion::Add =
16141617
show_code_change
16151618
{
1619+
let mut prev_lines: Option<(usize, usize)> = None;
16161620
for part in parts {
16171621
let snippet = sm.span_to_snippet(part.span.clone()).unwrap_or_default();
16181622
let (span_start, span_end) = sm.span_to_locations(part.span.clone());
@@ -1702,6 +1706,12 @@ fn emit_suggestion_default(
17021706

17031707
let newlines = snippet.lines().count();
17041708
if newlines > 0 && row_num > newlines {
1709+
let offset = match prev_lines {
1710+
Some((start, end)) => {
1711+
file_lines.len().saturating_sub(end.saturating_sub(start))
1712+
}
1713+
None => file_lines.len(),
1714+
};
17051715
// Account for removals where the part being removed spans multiple
17061716
// lines.
17071717
// FIXME: We check the number of rows because in some cases, like in
@@ -1715,7 +1725,7 @@ fn emit_suggestion_default(
17151725
// Going lower than buffer_offset (+ 1) would mean
17161726
// overwriting existing content in the buffer
17171727
let min_row = buffer_offset + usize::from(!matches_previous_suggestion);
1718-
let row = (row_num - 2 - (newlines - i - 1)).max(min_row);
1728+
let row = (row_num - 2 - (offset - i - 1)).max(min_row);
17191729
// On the first line, we highlight between the start of the part
17201730
// span, and the end of that line.
17211731
// On the last line, we highlight between the start of the line, and
@@ -1746,6 +1756,7 @@ fn emit_suggestion_default(
17461756
true,
17471757
);
17481758
}
1759+
prev_lines = Some((span_start.line, span_end.line));
17491760
}
17501761

17511762
// length of the code after substitution

tests/color/multiple_multiline_removal.ascii.term.svg

Lines changed: 10 additions & 4 deletions
Loading

tests/color/multiple_multiline_removal.unicode.term.svg

Lines changed: 10 additions & 4 deletions
Loading

tests/formatter.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2371,6 +2371,9 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
23712371
9 - fuzzy
23722372
10 - pizza
23732373
11 - jumps
2374+
12 - crazy
2375+
13 - quack
2376+
14 - zappy
23742377
8 + campy
23752378
|
23762379
"#]];
@@ -2386,6 +2389,9 @@ help: consider removing the `?Sized` bound to make the type parameter `Sized`
23862389
9 - fuzzy
23872390
10 - pizza
23882391
11 - jumps
2392+
12 - crazy
2393+
13 - quack
2394+
14 - zappy
23892395
8 + campy
23902396
╰╴
23912397
"#]];

tests/rustc_tests.rs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5325,6 +5325,9 @@ help: you might have meant to use an associated function to build this type
53255325
help: consider using the `Default` trait
53265326
|
53275327
11 - wtf: Some(Box(U {
5328+
12 - wtf: None,
5329+
13 - x: (),
5330+
14 - })),
53285331
11 + wtf: Some(<Box as std::default::Default>::default()),
53295332
|
53305333
"#]];
@@ -5374,6 +5377,9 @@ help: you might have meant to use an associated function to build this type
53745377
help: consider using the `Default` trait
53755378
╭╴
53765379
11 - wtf: Some(Box(U {
5380+
12 - wtf: None,
5381+
13 - x: (),
5382+
14 - })),
53775383
11 + wtf: Some(<Box as std::default::Default>::default()),
53785384
╰╴
53795385
"#]];
@@ -5775,6 +5781,10 @@ note: associated function defined here
57755781
help: remove the extra arguments
57765782
|
57775783
4 - generate_setter,
5784+
5 - r#"
5785+
6 - pub(crate) struct Person<T: Clone> {}
5786+
7 - "#,
5787+
8 - r#""#,
57785788
4 + /* usize */,
57795789
|
57805790
"##]];
@@ -5807,6 +5817,10 @@ note: associated function defined here
58075817
help: remove the extra arguments
58085818
╭╴
58095819
4 - generate_setter,
5820+
5 - r#"
5821+
6 - pub(crate) struct Person<T: Clone> {}
5822+
7 - "#,
5823+
8 - r#""#,
58105824
4 + /* usize */,
58115825
╰╴
58125826
"##]];
@@ -5867,7 +5881,6 @@ help: otherwise remove the non-wildcard arms
58675881
|
58685882
20 - 2 => 'b',
58695883
21 - 3 => 'b',
5870-
20 + _ => 'b',
58715884
|
58725885
"#]];
58735886
let renderer_ascii = Renderer::plain();
@@ -5889,7 +5902,6 @@ help: otherwise remove the non-wildcard arms
58895902
╭╴
58905903
20 - 2 => 'b',
58915904
21 - 3 => 'b',
5892-
20 + _ => 'b',
58935905
╰╴
58945906
"#]];
58955907
let renderer_unicode = renderer_ascii.decor_style(DecorStyle::Unicode);

0 commit comments

Comments
 (0)