Skip to content

Commit e41310d

Browse files
authored
Merge pull request #154 from KarelPeeters/fix-origin
Fix origin position computation
2 parents 5e1c7b2 + 695e428 commit e41310d

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

src/renderer/display_list.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1149,7 +1149,7 @@ fn format_header<'a>(
11491149
..
11501150
} = item
11511151
{
1152-
if main_range >= range.0 && main_range <= range.1 + end_line.len() {
1152+
if main_range >= range.0 && main_range < range.1 + max(*end_line as usize, 1) {
11531153
let char_column = text[0..(main_range - range.0).min(text.len())]
11541154
.chars()
11551155
.count();

tests/formatter.rs

+50
Original file line numberDiff line numberDiff line change
@@ -905,3 +905,53 @@ error: unused optional dependency
905905
let renderer = Renderer::plain();
906906
assert_data_eq!(renderer.render(input).to_string(), expected);
907907
}
908+
909+
#[test]
910+
fn origin_correct_start_line() {
911+
let source = "aaa\nbbb\nccc\nddd\n";
912+
let input = Level::Error.title("title").snippet(
913+
Snippet::source(source)
914+
.origin("origin.txt")
915+
.fold(false)
916+
.annotation(Level::Error.span(8..8 + 3).label("annotation")),
917+
);
918+
919+
let expected = str![[r#"
920+
error: title
921+
--> origin.txt:3:1
922+
|
923+
1 | aaa
924+
2 | bbb
925+
3 | ccc
926+
| ^^^ annotation
927+
4 | ddd
928+
|
929+
"#]];
930+
let renderer = Renderer::plain();
931+
assert_data_eq!(renderer.render(input).to_string(), expected);
932+
}
933+
934+
#[test]
935+
fn origin_correct_mid_line() {
936+
let source = "aaa\nbbb\nccc\nddd\n";
937+
let input = Level::Error.title("title").snippet(
938+
Snippet::source(source)
939+
.origin("origin.txt")
940+
.fold(false)
941+
.annotation(Level::Error.span(8 + 1..8 + 3).label("annotation")),
942+
);
943+
944+
let expected = str![[r#"
945+
error: title
946+
--> origin.txt:3:2
947+
|
948+
1 | aaa
949+
2 | bbb
950+
3 | ccc
951+
| ^^ annotation
952+
4 | ddd
953+
|
954+
"#]];
955+
let renderer = Renderer::plain();
956+
assert_data_eq!(renderer.render(input).to_string(), expected);
957+
}

0 commit comments

Comments
 (0)