diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index e2f0d74..8884b0d 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -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(); diff --git a/tests/formatter.rs b/tests/formatter.rs index 7f914de..6faab76 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -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); +}