Skip to content

Commit 526149a

Browse files
committed
fix: Properly calculate the max line num for suggestions
1 parent b13223c commit 526149a

File tree

2 files changed

+94
-89
lines changed

2 files changed

+94
-89
lines changed

src/renderer/render.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -2699,21 +2699,26 @@ fn pre_process<'a>(
26992699
let display_suggestion = DisplaySuggestion::new(&complete, &patches, &sm);
27002700

27012701
if suggestion.fold {
2702-
let end = suggestion
2703-
.markers
2704-
.iter()
2705-
.map(|a| a.span.end)
2706-
.max()
2707-
.unwrap_or(suggestion.source.len())
2708-
.min(suggestion.source.len());
2709-
2710-
max_line_num = max(
2711-
suggestion.line_start + newline_count(&suggestion.source[..end]),
2712-
max_line_num,
2713-
);
2702+
if let Some(first) = patches.first() {
2703+
let (l_start, _) =
2704+
sm.span_to_locations(first.original_span.clone());
2705+
let nc = newline_count(&complete);
2706+
let sugg_max_line_num = match display_suggestion {
2707+
DisplaySuggestion::Underline => l_start.line,
2708+
DisplaySuggestion::Diff => {
2709+
let file_lines = sm.span_to_lines(first.span.clone());
2710+
file_lines
2711+
.last()
2712+
.map_or(l_start.line + nc, |line| line.line_index)
2713+
}
2714+
DisplaySuggestion::None => l_start.line + nc,
2715+
DisplaySuggestion::Add => l_start.line + nc,
2716+
};
2717+
max_line_num = max(sugg_max_line_num, max_line_num);
2718+
}
27142719
} else {
27152720
max_line_num = max(
2716-
suggestion.line_start + newline_count(&suggestion.source),
2721+
suggestion.line_start + newline_count(&complete),
27172722
max_line_num,
27182723
);
27192724
}

tests/formatter.rs

Lines changed: 76 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -4387,52 +4387,52 @@ fn main() {
43874387

43884388
let expected_ascii = str![[r#"
43894389
error[E0061]: this function takes 6 arguments but 5 arguments were supplied
4390-
--> $DIR/trimmed_multiline_suggestion.rs:5:5
4391-
|
4392-
5 | function_with_lots_of_arguments(
4393-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4394-
6 | variable_name,
4395-
7 | variable_name,
4396-
| ------------- argument #2 of type `char` is missing
4397-
|
4390+
--> $DIR/trimmed_multiline_suggestion.rs:5:5
4391+
|
4392+
5 | function_with_lots_of_arguments(
4393+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4394+
6 | variable_name,
4395+
7 | variable_name,
4396+
| ------------- argument #2 of type `char` is missing
4397+
|
43984398
note: function defined here
4399-
--> $DIR/trimmed_multiline_suggestion.rs:1:4
4400-
|
4401-
1 | fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
4402-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------
4399+
--> $DIR/trimmed_multiline_suggestion.rs:1:4
4400+
|
4401+
1 | fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
4402+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ -------
44034403
help: provide the argument
4404-
|
4405-
5 | function_with_lots_of_arguments(
4406-
6 | variable_name,
4407-
7 ~ /* char */,
4408-
8 ~ variable_name,
4409-
|
4404+
|
4405+
5 | function_with_lots_of_arguments(
4406+
6 | variable_name,
4407+
7 ~ /* char */,
4408+
8 ~ variable_name,
4409+
|
44104410
"#]];
44114411
let renderer_ascii = Renderer::plain();
44124412
assert_data_eq!(renderer_ascii.render(input), expected_ascii);
44134413

44144414
let expected_unicode = str![[r#"
44154415
error[E0061]: this function takes 6 arguments but 5 arguments were supplied
4416-
╭▸ $DIR/trimmed_multiline_suggestion.rs:5:5
4417-
4418-
5 │ function_with_lots_of_arguments(
4419-
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4420-
6 │ variable_name,
4421-
7 │ variable_name,
4422-
│ ───────────── argument #2 of type `char` is missing
4423-
╰╴
4416+
╭▸ $DIR/trimmed_multiline_suggestion.rs:5:5
4417+
4418+
5 │ function_with_lots_of_arguments(
4419+
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4420+
6 │ variable_name,
4421+
7 │ variable_name,
4422+
│ ───────────── argument #2 of type `char` is missing
4423+
╰╴
44244424
note: function defined here
4425-
╭▸ $DIR/trimmed_multiline_suggestion.rs:1:4
4426-
4427-
1 │ fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
4428-
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ───────
4425+
╭▸ $DIR/trimmed_multiline_suggestion.rs:1:4
4426+
4427+
1 │ fn function_with_lots_of_arguments(a: i32, b: char, c: i32, d: i32, e: i32, f: i32) {}
4428+
╰╴ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ───────
44294429
help: provide the argument
4430-
╭╴
4431-
5 │ function_with_lots_of_arguments(
4432-
6 │ variable_name,
4433-
7 ± /* char */,
4434-
8 ± variable_name,
4435-
╰╴
4430+
╭╴
4431+
5 │ function_with_lots_of_arguments(
4432+
6 │ variable_name,
4433+
7 ± /* char */,
4434+
8 ± variable_name,
4435+
╰╴
44364436
"#]];
44374437
let renderer_unicode = renderer_ascii.decor_style(DecorStyle::Unicode);
44384438
assert_data_eq!(renderer_unicode.render(input), expected_unicode);
@@ -4590,54 +4590,54 @@ fn suggestion_no_fold() {
45904590

45914591
let expected_ascii = str![[r#"
45924592
error[E0061]: this function takes 6 arguments but 5 arguments were supplied
4593-
--> $DIR/trimmed_multiline_suggestion.rs:3:5
4594-
|
4595-
3 | function_with_lots_of_arguments(
4596-
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4597-
4 | variable_name,
4598-
5 | variable_name,
4599-
| ------------- argument #2 of type `char` is missing
4600-
|
4593+
--> $DIR/trimmed_multiline_suggestion.rs:3:5
4594+
|
4595+
3 | function_with_lots_of_arguments(
4596+
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
4597+
4 | variable_name,
4598+
5 | variable_name,
4599+
| ------------- argument #2 of type `char` is missing
4600+
|
46014601
help: provide the argument
4602-
|
4603-
1 | fn main() {
4604-
2 | let variable_name = 42;
4605-
3 | function_with_lots_of_arguments(
4606-
4 | variable_name,
4607-
5 ~ /* char */,
4608-
6 ~ variable_name,
4609-
7 | variable_name,
4610-
8 | variable_name,
4611-
9 | );
4612-
10| }
4613-
|
4602+
|
4603+
1 | fn main() {
4604+
2 | let variable_name = 42;
4605+
3 | function_with_lots_of_arguments(
4606+
4 | variable_name,
4607+
5 ~ /* char */,
4608+
6 ~ variable_name,
4609+
7 | variable_name,
4610+
8 | variable_name,
4611+
9 | );
4612+
10 | }
4613+
|
46144614
"#]];
46154615
let renderer_ascii = Renderer::plain();
46164616
assert_data_eq!(renderer_ascii.render(input), expected_ascii);
46174617

46184618
let expected_unicode = str![[r#"
46194619
error[E0061]: this function takes 6 arguments but 5 arguments were supplied
4620-
╭▸ $DIR/trimmed_multiline_suggestion.rs:3:5
4621-
4622-
3 │ function_with_lots_of_arguments(
4623-
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4624-
4 │ variable_name,
4625-
5 │ variable_name,
4626-
│ ───────────── argument #2 of type `char` is missing
4627-
╰╴
4620+
╭▸ $DIR/trimmed_multiline_suggestion.rs:3:5
4621+
4622+
3 │ function_with_lots_of_arguments(
4623+
│ ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━
4624+
4 │ variable_name,
4625+
5 │ variable_name,
4626+
│ ───────────── argument #2 of type `char` is missing
4627+
╰╴
46284628
help: provide the argument
4629-
╭╴
4630-
1 │ fn main() {
4631-
2 │ let variable_name = 42;
4632-
3 │ function_with_lots_of_arguments(
4633-
4 │ variable_name,
4634-
5 ± /* char */,
4635-
6 ± variable_name,
4636-
7 │ variable_name,
4637-
8 │ variable_name,
4638-
9 │ );
4639-
10│ }
4640-
╰╴
4629+
╭╴
4630+
1 │ fn main() {
4631+
2 │ let variable_name = 42;
4632+
3 │ function_with_lots_of_arguments(
4633+
4 │ variable_name,
4634+
5 ± /* char */,
4635+
6 ± variable_name,
4636+
7 │ variable_name,
4637+
8 │ variable_name,
4638+
9 │ );
4639+
10 │ }
4640+
╰╴
46414641
"#]];
46424642
let renderer_unicode = renderer_ascii.decor_style(DecorStyle::Unicode);
46434643
assert_data_eq!(renderer_unicode.render(input), expected_unicode);

0 commit comments

Comments
 (0)