From 43ef46a64ea53030c0691940a138162e5631bb52 Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Tue, 8 Jul 2025 00:13:48 -0600 Subject: [PATCH 1/2] test: Padding last in group --- tests/formatter.rs | 131 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 130 insertions(+), 1 deletion(-) diff --git a/tests/formatter.rs b/tests/formatter.rs index e7c08d4..94f4ee7 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -1,4 +1,6 @@ -use annotate_snippets::{Annotation, AnnotationKind, Group, Level, Patch, Renderer, Snippet}; +use annotate_snippets::{ + Annotation, AnnotationKind, Group, Level, Padding, Patch, Renderer, Snippet, +}; use annotate_snippets::renderer::OutputTheme; use snapbox::{assert_data_eq, str}; @@ -2823,3 +2825,130 @@ error: let renderer = Renderer::plain().theme(OutputTheme::Unicode); assert_data_eq!(renderer.render(input), expected_unicode); } + +#[test] +fn padding_last_in_group() { + let source = r#"// When the type of a method call's receiver is unknown, the span should point +// to the receiver (and not the entire call, as was previously the case before +// the fix of which this tests). + +fn shines_a_beacon_through_the_darkness() { + let x: Option<_> = None; //~ ERROR type annotations needed + x.unwrap().method_that_could_exist_on_some_type(); +} + +fn courier_to_des_moines_and_points_west(data: &[u32]) -> String { + data.iter() + .sum::<_>() //~ ERROR type annotations needed + .to_string() +} + +fn main() {} +"#; + + let input = &[ + Group::with_title(Level::ERROR.title("type annotations needed").id("E0282")) + .element( + Snippet::source(source) + .path("$DIR/issue-42234-unknown-receiver-type.rs") + .annotation(AnnotationKind::Primary.span(449..452).label( + "cannot infer type of the type parameter `S` declared on the method `sum`", + )), + ) + .element(Padding), + ]; + + let expected_ascii = str![[r#" +error[E0282]: type annotations needed + --> $DIR/issue-42234-unknown-receiver-type.rs:12:10 + | +LL | .sum::<_>() //~ ERROR type annotations needed + | ^^^ cannot infer type of the type parameter `S` declared on the method `sum` + | +"#]]; + let renderer = Renderer::plain().anonymized_line_numbers(true); + assert_data_eq!(renderer.render(input), expected_ascii); + + let expected_unicode = str![[r#" +error[E0282]: type annotations needed + ╭▸ $DIR/issue-42234-unknown-receiver-type.rs:12:10 + │ +LL │ .sum::<_>() //~ ERROR type annotations needed + │ ━━━ cannot infer type of the type parameter `S` declared on the method `sum` + │ +"#]]; + let renderer = renderer.theme(OutputTheme::Unicode); + assert_data_eq!(renderer.render(input), expected_unicode); +} + +#[test] +fn padding_last_in_group_with_group_after() { + let source = r#"// When the type of a method call's receiver is unknown, the span should point +// to the receiver (and not the entire call, as was previously the case before +// the fix of which this tests). + +fn shines_a_beacon_through_the_darkness() { + let x: Option<_> = None; //~ ERROR type annotations needed + x.unwrap().method_that_could_exist_on_some_type(); +} + +fn courier_to_des_moines_and_points_west(data: &[u32]) -> String { + data.iter() + .sum::<_>() //~ ERROR type annotations needed + .to_string() +} + +fn main() {} +"#; + + let input = &[ + Group::with_title(Level::ERROR.title("type annotations needed").id("E0282")) + .element( + Snippet::source(source) + .path("$DIR/issue-42234-unknown-receiver-type.rs") + .annotation(AnnotationKind::Primary.span(449..452).label( + "cannot infer type of the type parameter `S` declared on the method `sum`", + )), + ) + .element(Padding), + Group::with_title(Level::HELP.title("consider specifying the generic argument")).element( + Snippet::source(source) + .path("$DIR/issue-42234-unknown-receiver-type.rs") + .line_start(12) + .fold(true) + .patch(Patch::new(452..457, "::")), + ), + ]; + + let expected_ascii = str![[r#" +error[E0282]: type annotations needed + --> $DIR/issue-42234-unknown-receiver-type.rs:12:10 + | +LL | .sum::<_>() //~ ERROR type annotations needed + | ^^^ cannot infer type of the type parameter `S` declared on the method `sum` + | +help: consider specifying the generic argument + | +LL - .sum::<_>() //~ ERROR type annotations needed +LL + .sum::() //~ ERROR type annotations needed + | +"#]]; + let renderer = Renderer::plain().anonymized_line_numbers(true); + assert_data_eq!(renderer.render(input), expected_ascii); + + let expected_unicode = str![[r#" +error[E0282]: type annotations needed + ╭▸ $DIR/issue-42234-unknown-receiver-type.rs:12:10 + │ +LL │ .sum::<_>() //~ ERROR type annotations needed + │ ━━━ cannot infer type of the type parameter `S` declared on the method `sum` + │ +help: consider specifying the generic argument + ╭╴ +LL - .sum::<_>() //~ ERROR type annotations needed +LL + .sum::() //~ ERROR type annotations needed + ╰╴ +"#]]; + let renderer = renderer.theme(OutputTheme::Unicode); + assert_data_eq!(renderer.render(input), expected_unicode); +} From 475dca6c838ba25599e6b0f8f97ec1684ac6b03c Mon Sep 17 00:00:00 2001 From: Scott Schafer Date: Tue, 8 Jul 2025 00:17:13 -0600 Subject: [PATCH 2/2] fix: Render Padding as end col separator if last in Group --- src/renderer/mod.rs | 18 +++++++++++++----- tests/formatter.rs | 4 ++-- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/src/renderer/mod.rs b/src/renderer/mod.rs index 8d3c6a2..5754dc7 100644 --- a/src/renderer/mod.rs +++ b/src/renderer/mod.rs @@ -413,11 +413,19 @@ impl Renderer { } Element::Padding(_) => { let current_line = buffer.num_lines(); - self.draw_col_separator_no_space( - &mut buffer, - current_line, - max_line_num_len + 1, - ); + if peek.is_none() { + self.draw_col_separator_end( + &mut buffer, + current_line, + max_line_num_len + 1, + ); + } else { + self.draw_col_separator_no_space( + &mut buffer, + current_line, + max_line_num_len + 1, + ); + } } } if g == 0 diff --git a/tests/formatter.rs b/tests/formatter.rs index 94f4ee7..5462130 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -2875,7 +2875,7 @@ error[E0282]: type annotations needed │ LL │ .sum::<_>() //~ ERROR type annotations needed │ ━━━ cannot infer type of the type parameter `S` declared on the method `sum` - │ + ╰╴ "#]]; let renderer = renderer.theme(OutputTheme::Unicode); assert_data_eq!(renderer.render(input), expected_unicode); @@ -2942,7 +2942,7 @@ error[E0282]: type annotations needed │ LL │ .sum::<_>() //~ ERROR type annotations needed │ ━━━ cannot infer type of the type parameter `S` declared on the method `sum` - │ + ╰╴ help: consider specifying the generic argument ╭╴ LL - .sum::<_>() //~ ERROR type annotations needed