feat(printer): use uparrow for empty highlights and fix 0-offset display bug

This commit is contained in:
Kat Marchán 2021-09-07 17:36:25 -07:00
parent d5bb6f5eab
commit 824cd8bebe
No known key found for this signature in database
GPG Key ID: AEB529C08A3C7E9E
2 changed files with 47 additions and 3 deletions

View File

@ -401,7 +401,9 @@ impl GraphicalReportHandler {
"{:width$}{}{}{}", "{:width$}{}{}{}",
"", "",
chars.underline.to_string().repeat(num_left), 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 chars.underbar
} else { } else {
chars.underline chars.underline
@ -427,7 +429,7 @@ impl GraphicalReportHandler {
let num_right = local_offset + hl_len - vbar_offset - 1; let num_right = local_offset + hl_len - vbar_offset - 1;
let lines = format!( let lines = format!(
"{:width$}{}{} {}", "{:width$}{}{} {}",
" ", "",
chars.lbot, chars.lbot,
chars.hbar.to_string().repeat(num_right + 1), chars.hbar.to_string().repeat(num_right + 1),
label, label,

View File

@ -65,6 +65,48 @@ fn single_line_highlight() -> Result<(), MietteError> {
Ok(()) 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] #[test]
fn single_line_highlight_with_empty_span() -> Result<(), MietteError> { fn single_line_highlight_with_empty_span() -> Result<(), MietteError> {
#[derive(Debug, Diagnostic, Error)] #[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: [bad_file.rs:1:1] This is the part that broke:
1 source 1 source
2 text 2 text
· ·
· this bit here · this bit here
3 here 3 here