Skip to content

Commit d6b0b17

Browse files
committed
fix: display for Span with start == end
1 parent cec0e8c commit d6b0b17

1 file changed

Lines changed: 30 additions & 1 deletion

File tree

src/error.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ impl fmt::Display for RichError {
240240

241241
let (underline_start, underline_length) = match is_multiline {
242242
true => (0, start_line_len),
243-
false => (start_col, end_col - start_col),
243+
false => (start_col, (end_col - start_col).max(1)),
244244
};
245245
write!(f, "{:width$} |", " ", width = line_num_width)?;
246246
write!(f, "{:width$}", " ", width = underline_start)?;
@@ -763,4 +763,33 @@ let x: u32 = Left(
763763

764764
assert_eq!(&expected[1..], &error.to_string());
765765
}
766+
767+
#[test]
768+
fn display_span_as_point() {
769+
let file = "fn main()";
770+
let error = Error::Grammar("Error span at (0,0)".to_string())
771+
.with_span(Span::new(0, 0))
772+
.with_file(Arc::from(file));
773+
774+
let expected = r#"
775+
|
776+
1 | fn main()
777+
| ^ Grammar error: Error span at (0,0)"#;
778+
assert_eq!(&expected[1..], &error.to_string());
779+
}
780+
781+
#[test]
782+
fn display_span_as_point_on_trailing_empty_line() {
783+
let file = "fn main(){\n let a:\n";
784+
let error = Error::CannotParse("eof".to_string())
785+
.with_span(Span::new(file.len(), file.len()))
786+
.with_file(Arc::from(file));
787+
788+
let expected = r#"
789+
|
790+
3 |
791+
| ^ Cannot parse: eof"#;
792+
793+
assert_eq!(&expected[1..], &error.to_string());
794+
}
766795
}

0 commit comments

Comments
 (0)