Skip to content

fix: Match how rustc handles annotating newlines #216

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
Jun 12, 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
8 changes: 2 additions & 6 deletions src/renderer/source_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,20 +73,16 @@ impl<'a> SourceMap<'a> {
let end_info = self
.lines
.iter()
.find(|info| info.end_byte > span.end.saturating_sub(1))
.find(|info| span.end >= info.start_byte && span.end < info.end_byte)
.unwrap_or(self.lines.last().unwrap());
let (mut end_char_pos, end_display_pos) = end_info.line
let (end_char_pos, end_display_pos) = end_info.line
[0..(span.end - end_info.start_byte).min(end_info.line.len())]
.chars()
.fold((0, 0), |(char_pos, byte_pos), c| {
let display = char_width(c);
(char_pos + 1, byte_pos + display)
});

// correct the char pos if we are highlighting the end of a line
if (span.end - end_info.start_byte).saturating_sub(end_info.line.len()) > 0 {
end_char_pos += 1;
}
let mut end = Loc {
line: end_info.line_index,
char: end_char_pos,
Expand Down
12 changes: 7 additions & 5 deletions tests/color/ann_multiline2.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 8 additions & 6 deletions tests/color/simple.term.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
55 changes: 32 additions & 23 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,9 +415,9 @@ fn char_eol_annotate_char() {
error:
--> file/path:3:1
|
3 | a
| ^
4 | b
3 | / a
4 | | b
| |_^
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input), expected);
Expand All @@ -437,10 +437,11 @@ fn char_eol_annotate_char_double_width() {
error:
--> <current file>:1:2
|
1 | こん
| ^^
2 | にちは
3 | 世界
1 | こん
| ___^
2 | | にちは
| |_^
3 | 世界
"#]];

let renderer = Renderer::plain();
Expand Down Expand Up @@ -485,9 +486,10 @@ fn annotate_eol2() {
error:
--> file/path:3:2
|
3 | a
| ^
4 | b
3 | a
| __^
4 | | b
| |_^
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input), expected);
Expand All @@ -508,9 +510,10 @@ fn annotate_eol3() {
error:
--> file/path:3:3
|
3 | a
| ^
4 | b
3 | a
| __^
4 | | b
| |_^
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input), expected);
Expand Down Expand Up @@ -553,10 +556,11 @@ fn annotate_eol_double_width() {
error:
--> <current file>:1:4
|
1 | こん
| ^
2 | にちは
3 | 世界
1 | こん
| _____^
2 | | にちは
| |_^
3 | 世界
"#]];

let renderer = Renderer::plain();
Expand Down Expand Up @@ -678,8 +682,8 @@ error:
3 | a
| __^
4 | | b
| |__^
5 | c
5 | | c
| |_^
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input), expected);
Expand Down Expand Up @@ -728,8 +732,8 @@ error:
3 | a
| __^
4 | | b
| |__^
5 | c
5 | | c
| |_^
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(false);
assert_data_eq!(renderer.render(input), expected);
Expand Down Expand Up @@ -1532,6 +1536,7 @@ LL - T
LL - :
LL - ?
LL - Sized
LL + {
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
Expand Down Expand Up @@ -1640,8 +1645,12 @@ LL | struct Wrapper<T>(T);
| this could be changed to `T: ?Sized`...
help: consider removing the `?Sized` bound to make the type parameter `Sized`
|
LL ~ and
LL ~ + Send{
LL - and where
LL - T
LL - :
LL - ?
LL - Sized
LL + and + Send{
|
"#]];
let renderer = Renderer::plain().anonymized_line_numbers(true);
Expand Down
Loading