diff --git a/src/handlers/graphical.rs b/src/handlers/graphical.rs index b3f9d965..4ebe392b 100644 --- a/src/handlers/graphical.rs +++ b/src/handlers/graphical.rs @@ -401,7 +401,9 @@ impl GraphicalReportHandler { "{:width$}{}{}{}", "", chars.underline.to_string().repeat(num_left), - if hl.label().is_some() { + if hl.len() == 0 { + chars.uarrow + } else if hl.label().is_some() { chars.underbar } else { chars.underline @@ -427,7 +429,7 @@ impl GraphicalReportHandler { let num_right = local_offset + hl_len - vbar_offset - 1; let lines = format!( "{:width$}{}{} {}", - " ", + "", chars.lbot, chars.hbar.to_string().repeat(num_right + 1), label, diff --git a/tests/printer.rs b/tests/printer.rs index b485c5cf..088db0af 100644 --- a/tests/printer.rs +++ b/tests/printer.rs @@ -65,6 +65,48 @@ fn single_line_highlight() -> Result<(), MietteError> { Ok(()) } +#[test] +fn single_line_highlight_offset_zero() -> Result<(), MietteError> { + #[derive(Debug, Diagnostic, Error)] + #[error("oops!")] + #[diagnostic(code(oops::my::bad), help("try doing it better next time?"))] + struct MyBad { + src: NamedSource, + #[snippet(src, message("This is the part that broke"))] + ctx: SourceSpan, + #[highlight(ctx, label = "this bit here")] + highlight: SourceSpan, + } + + let src = "source\n text\n here".to_string(); + let len = src.len(); + let err = MyBad { + src: NamedSource::new("bad_file.rs", src), + ctx: (0, len).into(), + highlight: (0, 0).into(), + }; + let out = fmt_report(err.into()); + println!("{}", out); + let expected = r#" +────[oops::my::bad]──────────────────── + + × oops! + + ╭───[bad_file.rs:1:1] This is the part that broke: + 1 │ source + · ▲ + · ╰─ this bit here + 2 │ text + 3 │ here + + ‽ 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)] @@ -95,7 +137,7 @@ fn single_line_highlight_with_empty_span() -> Result<(), MietteError> { ╭───[bad_file.rs:1:1] This is the part that broke: 1 │ source 2 │ text - · ┬ + · ▲ · ╰─ this bit here 3 │ here