Skip to content

Commit

Permalink
feat(printer): use uparrow for empty highlights and fix 0-offset disp…
Browse files Browse the repository at this point in the history
…lay bug
  • Loading branch information
zkat committed Sep 8, 2021
1 parent d5bb6f5 commit 824cd8b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 3 deletions.
6 changes: 4 additions & 2 deletions src/handlers/graphical.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down
44 changes: 43 additions & 1 deletion tests/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 824cd8b

Please sign in to comment.