diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index 55999b0..66760ff 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -657,9 +657,9 @@ impl GraphicalReportHandler { .iter() .map(|hl| { let byte_start = hl.offset(); - let byte_end = hl.offset() + hl.len().max(1); + let byte_end = hl.offset() + hl.len(); let start = self.visual_offset(line, byte_start).max(highest); - let end = self.visual_offset(line, byte_end); + let end = self.visual_offset(line, byte_end).max(start + 1); let vbar_offset = (start + end) / 2; let num_left = vbar_offset - start; diff --git a/tests/graphical.rs b/tests/graphical.rs index a2420c0..dd18449 100644 --- a/tests/graphical.rs +++ b/tests/graphical.rs @@ -68,7 +68,7 @@ fn empty_source() -> Result<(), MietteError> { } #[test] -fn full_line_span() { +fn single_line_highlight_span_full_line() { #[derive(Error, Debug, Diagnostic)] #[error("oops!")] #[diagnostic(severity(Error))] @@ -322,6 +322,42 @@ fn single_line_highlight_offset_zero() -> Result<(), MietteError> { Ok(()) } +#[test] +fn single_line_higlight_offset_end_of_line() -> Result<(), MietteError> { + #[derive(Debug, Diagnostic, Error)] + #[error("oops!")] + #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] + struct MyBad { + #[source_code] + src: NamedSource, + #[label("this bit here")] + highlight: SourceSpan, + } + + let src = "source\n text\n here".to_string(); + let err = MyBad { + src: NamedSource::new("bad_file.rs", src), + highlight: (6, 0).into(), + }; + let out = fmt_report(err.into()); + println!("Error: {}", out); + let expected = r#"oops::my::bad + + × oops! + ╭─[bad_file.rs:1:1] + 1 │ source + · ▲ + · ╰── this bit here + 2 │ text + ╰──── + help: try doing it better next time? +"# + .trim_start() + .to_string(); + assert_eq!(expected, out); + Ok(()) +} + #[test] fn single_line_highlight_with_empty_span() -> Result<(), MietteError> { #[derive(Debug, Diagnostic, Error)]