Skip to content

fix: Render Padding as end col separator if last in Group #253

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 8, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 13 additions & 5 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
131 changes: 130 additions & 1 deletion tests/formatter.rs
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -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, "::<GENERIC_ARG>")),
),
];

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::<GENERIC_ARG>() //~ 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::<GENERIC_ARG>() //~ ERROR type annotations needed
╰╴
"#]];
let renderer = renderer.theme(OutputTheme::Unicode);
assert_data_eq!(renderer.render(input), expected_unicode);
}
Loading
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy