Skip to content
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

Fix origin position computation #154

Merged
merged 2 commits into from
Oct 22, 2024
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
2 changes: 1 addition & 1 deletion src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1149,7 +1149,7 @@ fn format_header<'a>(
..
} = item
{
if main_range >= range.0 && main_range <= range.1 + end_line.len() {
if main_range >= range.0 && main_range < range.1 + max(*end_line as usize, 1) {
let char_column = text[0..(main_range - range.0).min(text.len())]
.chars()
.count();
Expand Down
50 changes: 50 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -905,3 +905,53 @@ error: unused optional dependency
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}

#[test]
fn origin_correct_start_line() {
let source = "aaa\nbbb\nccc\nddd\n";
let input = Level::Error.title("title").snippet(
Snippet::source(source)
.origin("origin.txt")
.fold(false)
.annotation(Level::Error.span(8..8 + 3).label("annotation")),
);

let expected = str![[r#"
error: title
--> origin.txt:3:1
|
1 | aaa
2 | bbb
3 | ccc
| ^^^ annotation
4 | ddd
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}

#[test]
fn origin_correct_mid_line() {
let source = "aaa\nbbb\nccc\nddd\n";
let input = Level::Error.title("title").snippet(
Snippet::source(source)
.origin("origin.txt")
.fold(false)
.annotation(Level::Error.span(8 + 1..8 + 3).label("annotation")),
);

let expected = str![[r#"
error: title
--> origin.txt:3:2
|
1 | aaa
2 | bbb
3 | ccc
| ^^ annotation
4 | ddd
|
"#]];
let renderer = Renderer::plain();
assert_data_eq!(renderer.render(input).to_string(), expected);
}
KarelPeeters marked this conversation as resolved.
Show resolved Hide resolved
Loading