Skip to content

fix: If fold is false max_line_num is last source line #247

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 3, 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
44 changes: 26 additions & 18 deletions src/renderer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2919,26 +2919,34 @@ fn max_line_number(groups: &[Group<'_>]) -> usize {
| Element::Origin(_)
| Element::Padding(_) => 0,
Element::Cause(cause) => {
let end = cause
.markers
.iter()
.map(|a| a.span.end)
.max()
.unwrap_or(cause.source.len())
.min(cause.source.len());

cause.line_start + newline_count(&cause.source[..end])
if cause.fold {
let end = cause
.markers
.iter()
.map(|a| a.span.end)
.max()
.unwrap_or(cause.source.len())
.min(cause.source.len());

cause.line_start + newline_count(&cause.source[..end])
} else {
cause.line_start + newline_count(&cause.source)
}
}
Element::Suggestion(suggestion) => {
let end = suggestion
.markers
.iter()
.map(|a| a.span.end)
.max()
.unwrap_or(suggestion.source.len())
.min(suggestion.source.len());

suggestion.line_start + newline_count(&suggestion.source[..end])
if suggestion.fold {
let end = suggestion
.markers
.iter()
.map(|a| a.span.end)
.max()
.unwrap_or(suggestion.source.len())
.min(suggestion.source.len());

suggestion.line_start + newline_count(&suggestion.source[..end])
} else {
suggestion.line_start + newline_count(&suggestion.source)
}
}
})
.max()
Expand Down
38 changes: 38 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2574,3 +2574,41 @@ LL + break;
let renderer_unicode = renderer_ascii.theme(OutputTheme::Unicode);
assert_data_eq!(renderer_unicode.render(input), expected_unicode);
}

#[test]
fn max_line_num_no_fold() {
let source = r#"cargo
fuzzy
pizza
jumps
crazy
quack
zappy
"#;

let input_new = &[Group::with_title(
Level::ERROR
.title("the size for values of type `T` cannot be known at compilation time")
.id("E0277"),
)
.element(
Snippet::source(source)
.line_start(8)
.fold(false)
.annotation(AnnotationKind::Primary.span(6..11)),
)];
let expected = str![[r#"
error[E0277]: the size for values of type `T` cannot be known at compilation time
|
8 | cargo
9 | fuzzy
| ^^^^^
10 | pizza
11 | jumps
12 | crazy
13 | quack
14 | zappy
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input_new), expected);
}