Skip to content

Commit 47e7ec6

Browse files
committed
fix: Match how rustc handles annotating newlines
1 parent 5cc5a3c commit 47e7ec6

File tree

5 files changed

+60
-48
lines changed

5 files changed

+60
-48
lines changed

src/renderer/source_map.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -73,20 +73,16 @@ impl<'a> SourceMap<'a> {
7373
let end_info = self
7474
.lines
7575
.iter()
76-
.find(|info| info.end_byte > span.end.saturating_sub(1))
76+
.find(|info| span.end >= info.start_byte && span.end < info.end_byte)
7777
.unwrap_or(self.lines.last().unwrap());
78-
let (mut end_char_pos, end_display_pos) = end_info.line
78+
let (end_char_pos, end_display_pos) = end_info.line
7979
[0..(span.end - end_info.start_byte).min(end_info.line.len())]
8080
.chars()
8181
.fold((0, 0), |(char_pos, byte_pos), c| {
8282
let display = char_width(c);
8383
(char_pos + 1, byte_pos + display)
8484
});
8585

86-
// correct the char pos if we are highlighting the end of a line
87-
if (span.end - end_info.start_byte).saturating_sub(end_info.line.len()) > 0 {
88-
end_char_pos += 1;
89-
}
9086
let mut end = Loc {
9187
line: end_info.line_index,
9288
char: end_char_pos,

tests/color/ann_multiline2.term.svg

Lines changed: 7 additions & 5 deletions
Loading

tests/color/simple.term.svg

Lines changed: 8 additions & 6 deletions
Loading

tests/formatter.rs

Lines changed: 32 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,9 @@ fn char_eol_annotate_char() {
415415
error:
416416
--> file/path:3:1
417417
|
418-
3 | a
419-
| ^
420-
4 | b
418+
3 | / a
419+
4 | | b
420+
| |_^
421421
"#]];
422422
let renderer = Renderer::plain().anonymized_line_numbers(false);
423423
assert_data_eq!(renderer.render(input), expected);
@@ -437,10 +437,11 @@ fn char_eol_annotate_char_double_width() {
437437
error:
438438
--> <current file>:1:2
439439
|
440-
1 | こん
441-
| ^^
442-
2 | にちは
443-
3 | 世界
440+
1 | こん
441+
| ___^
442+
2 | | にちは
443+
| |_^
444+
3 | 世界
444445
"#]];
445446

446447
let renderer = Renderer::plain();
@@ -485,9 +486,10 @@ fn annotate_eol2() {
485486
error:
486487
--> file/path:3:2
487488
|
488-
3 | a
489-
| ^
490-
4 | b
489+
3 | a
490+
| __^
491+
4 | | b
492+
| |_^
491493
"#]];
492494
let renderer = Renderer::plain().anonymized_line_numbers(false);
493495
assert_data_eq!(renderer.render(input), expected);
@@ -508,9 +510,10 @@ fn annotate_eol3() {
508510
error:
509511
--> file/path:3:3
510512
|
511-
3 | a
512-
| ^
513-
4 | b
513+
3 | a
514+
| __^
515+
4 | | b
516+
| |_^
514517
"#]];
515518
let renderer = Renderer::plain().anonymized_line_numbers(false);
516519
assert_data_eq!(renderer.render(input), expected);
@@ -553,10 +556,11 @@ fn annotate_eol_double_width() {
553556
error:
554557
--> <current file>:1:4
555558
|
556-
1 | こん
557-
| ^
558-
2 | にちは
559-
3 | 世界
559+
1 | こん
560+
| _____^
561+
2 | | にちは
562+
| |_^
563+
3 | 世界
560564
"#]];
561565

562566
let renderer = Renderer::plain();
@@ -678,8 +682,8 @@ error:
678682
3 | a
679683
| __^
680684
4 | | b
681-
| |__^
682-
5 | c
685+
5 | | c
686+
| |_^
683687
"#]];
684688
let renderer = Renderer::plain().anonymized_line_numbers(false);
685689
assert_data_eq!(renderer.render(input), expected);
@@ -728,8 +732,8 @@ error:
728732
3 | a
729733
| __^
730734
4 | | b
731-
| |__^
732-
5 | c
735+
5 | | c
736+
| |_^
733737
"#]];
734738
let renderer = Renderer::plain().anonymized_line_numbers(false);
735739
assert_data_eq!(renderer.render(input), expected);
@@ -1532,6 +1536,7 @@ LL - T
15321536
LL - :
15331537
LL - ?
15341538
LL - Sized
1539+
LL + {
15351540
|
15361541
"#]];
15371542
let renderer = Renderer::plain().anonymized_line_numbers(true);
@@ -1640,8 +1645,12 @@ LL | struct Wrapper<T>(T);
16401645
| this could be changed to `T: ?Sized`...
16411646
help: consider removing the `?Sized` bound to make the type parameter `Sized`
16421647
|
1643-
LL ~ and
1644-
LL ~ + Send{
1648+
LL - and where
1649+
LL - T
1650+
LL - :
1651+
LL - ?
1652+
LL - Sized
1653+
LL + and + Send{
16451654
|
16461655
"#]];
16471656
let renderer = Renderer::plain().anonymized_line_numbers(true);

tests/rustc_tests.rs

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2166,12 +2166,14 @@ fn main() {
21662166
error: character constant must be escaped: `/n`
21672167
--> $DIR/bad-char-literals.rs:10:6
21682168
|
2169-
LL | '
2170-
| ^
2169+
LL | '
2170+
| ______^
2171+
LL | | ';
2172+
| |_^
21712173
|
21722174
help: escape the character
21732175
|
2174-
LL | '/n
2176+
LL | '/n';
21752177
| ++
21762178
"#]];
21772179
let renderer = Renderer::plain().anonymized_line_numbers(true);
@@ -2220,8 +2222,8 @@ error: unclosed frontmatter
22202222
|
22212223
LL | / ----cargo
22222224
... |
2223-
LL | | // are properly parsed.
2224-
| |________________________^
2225+
LL | |
2226+
| |_^
22252227
|
22262228
note: frontmatter opening here was not closed
22272229
--> $DIR/unclosed-1.rs:1:1
@@ -2396,7 +2398,8 @@ error: unclosed frontmatter
23962398
|
23972399
LL | / ----cargo
23982400
LL | | //~^ ERROR: unclosed frontmatter
2399-
| |_________________________________^
2401+
LL | |
2402+
| |_^
24002403
|
24012404
note: frontmatter opening here was not closed
24022405
--> $DIR/unclosed-4.rs:1:1
@@ -2451,8 +2454,8 @@ error: unclosed frontmatter
24512454
|
24522455
LL | / ----cargo
24532456
... |
2454-
LL | | // per unclosed-1.rs)
2455-
| |______________________^
2457+
LL | |
2458+
| |_^
24562459
|
24572460
note: frontmatter opening here was not closed
24582461
--> $DIR/unclosed-5.rs:1:1

0 commit comments

Comments
 (0)