Skip to content

Commit ee6e975

Browse files
committed
fix: Don't fold suggestions with fold = false
1 parent 6a16e22 commit ee6e975

File tree

3 files changed

+76
-12
lines changed

3 files changed

+76
-12
lines changed

src/renderer/render.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1450,7 +1450,7 @@ fn emit_suggestion_default(
14501450
is_first: bool,
14511451
is_cont: bool,
14521452
) {
1453-
let suggestions = sm.splice_lines(suggestion.markers.clone());
1453+
let suggestions = sm.splice_lines(suggestion.markers.clone(), suggestion.fold);
14541454

14551455
let buffer_offset = buffer.num_lines();
14561456
let mut row_num = buffer_offset + usize::from(!matches_previous_suggestion);
@@ -1511,8 +1511,12 @@ fn emit_suggestion_default(
15111511
}
15121512

15131513
let file_lines = sm.span_to_lines(parts[0].span.clone());
1514-
// We use the original span to get original line_start
1515-
let (line_start, line_end) = sm.span_to_locations(parts[0].original_span.clone());
1514+
let (line_start, line_end) = if suggestion.fold {
1515+
// We use the original span to get original line_start
1516+
sm.span_to_locations(parts[0].original_span.clone())
1517+
} else {
1518+
sm.span_to_locations(0..sm.source.len())
1519+
};
15161520
let mut lines = complete.lines();
15171521
if lines.clone().next().is_none() {
15181522
// Account for a suggestion to completely remove a line(s) with whitespace (#94192).
@@ -1545,7 +1549,7 @@ fn emit_suggestion_default(
15451549
last_pos = line_pos;
15461550

15471551
// Remember lines that are not highlighted to hide them if needed
1548-
if highlight_parts.is_empty() {
1552+
if highlight_parts.is_empty() && suggestion.fold {
15491553
unhighlighted_lines.push((line_pos, line));
15501554
continue;
15511555
}

src/renderer/source_map.rs

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ impl<'a> SourceMap<'a> {
370370
pub(crate) fn splice_lines<'b>(
371371
&'b self,
372372
mut patches: Vec<Patch<'b>>,
373+
fold: bool,
373374
) -> Vec<(
374375
String,
375376
Vec<TrimmedPatch<'b>>,
@@ -430,11 +431,16 @@ impl<'a> SourceMap<'a> {
430431
patches.sort_by_key(|p| p.span.start);
431432

432433
// Find the bounding span.
433-
let Some(lo) = patches.iter().map(|p| p.span.start).min() else {
434-
return Vec::new();
435-
};
436-
let Some(hi) = patches.iter().map(|p| p.span.end).max() else {
437-
return Vec::new();
434+
let (lo, hi) = if fold {
435+
let Some(lo) = patches.iter().map(|p| p.span.start).min() else {
436+
return Vec::new();
437+
};
438+
let Some(hi) = patches.iter().map(|p| p.span.end).max() else {
439+
return Vec::new();
440+
};
441+
(lo, hi)
442+
} else {
443+
(0, source_len)
438444
};
439445

440446
let lines = self.span_to_lines(lo..hi);
@@ -536,9 +542,19 @@ impl<'a> SourceMap<'a> {
536542
}
537543
}
538544
highlights.push(std::mem::take(&mut line_highlight));
539-
// if the replacement already ends with a newline, don't print the next line
540-
if !buf.ends_with('\n') {
541-
push_trailing(&mut buf, prev_line, &prev_hi, None);
545+
if fold {
546+
// if the replacement already ends with a newline, don't print the next line
547+
if !buf.ends_with('\n') {
548+
push_trailing(&mut buf, prev_line, &prev_hi, None);
549+
}
550+
} else {
551+
// Add the trailing part of the source after the last patch
552+
if let Some(snippet) = self.span_to_snippet(prev_hi.byte..source_len) {
553+
buf.push_str(snippet);
554+
for _ in snippet.matches('\n') {
555+
highlights.push(std::mem::take(&mut line_highlight));
556+
}
557+
}
542558
}
543559
// remove trailing newlines
544560
while buf.ends_with('\n') {

tests/formatter.rs

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4600,10 +4600,16 @@ error[E0061]: this function takes 6 arguments but 5 arguments were supplied
46004600
|
46014601
help: provide the argument
46024602
|
4603+
1 | fn main() {
4604+
2 | let variable_name = 42;
46034605
3 | function_with_lots_of_arguments(
46044606
4 | variable_name,
46054607
5 ~ /* char */,
46064608
6 ~ variable_name,
4609+
7 | variable_name,
4610+
8 | variable_name,
4611+
9 | );
4612+
10| }
46074613
|
46084614
"#]];
46094615
let renderer_ascii = Renderer::plain();
@@ -4621,10 +4627,16 @@ error[E0061]: this function takes 6 arguments but 5 arguments were supplied
46214627
╰╴
46224628
help: provide the argument
46234629
╭╴
4630+
1 │ fn main() {
4631+
2 │ let variable_name = 42;
46244632
3 │ function_with_lots_of_arguments(
46254633
4 │ variable_name,
46264634
5 ± /* char */,
46274635
6 ± variable_name,
4636+
7 │ variable_name,
4637+
8 │ variable_name,
4638+
9 │ );
4639+
10│ }
46284640
╰╴
46294641
"#]];
46304642
let renderer_unicode = renderer_ascii.decor_style(DecorStyle::Unicode);
@@ -4677,7 +4689,23 @@ error[E0433]: failed to resolve: use of undeclared crate or module `st`
46774689
|
46784690
help: consider importing this module
46794691
|
4692+
1 |
46804693
2 + use std::cell;
4694+
3 ~ use st::cell::Cell;
4695+
4 |
4696+
5 | mod bar {
4697+
6 | pub fn bar() { bar::baz(); }
4698+
7 |
4699+
8 | fn baz() {}
4700+
9 | }
4701+
10 |
4702+
11 | use bas::bar;
4703+
12 |
4704+
13 | struct Foo {
4705+
14 | bar: st::cell::Cell<bool>
4706+
15 | }
4707+
16 |
4708+
17 | fn main() {}
46814709
|
46824710
"#]];
46834711

@@ -4692,7 +4720,23 @@ error[E0433]: failed to resolve: use of undeclared crate or module `st`
46924720
╰╴
46934721
help: consider importing this module
46944722
╭╴
4723+
1 │
46954724
2 + use std::cell;
4725+
3 ± use st::cell::Cell;
4726+
4 │
4727+
5 │ mod bar {
4728+
6 │ pub fn bar() { bar::baz(); }
4729+
7 │
4730+
8 │ fn baz() {}
4731+
9 │ }
4732+
10 │
4733+
11 │ use bas::bar;
4734+
12 │
4735+
13 │ struct Foo {
4736+
14 │ bar: st::cell::Cell<bool>
4737+
15 │ }
4738+
16 │
4739+
17 │ fn main() {}
46964740
╰╴
46974741
"#]];
46984742
let renderer = renderer.decor_style(DecorStyle::Unicode);

0 commit comments

Comments
 (0)