tamer: diagnose::report: Vary mark character depending on level

Looking more and more Rust-like.  Shameless copy.

TBH I forget what character it uses for help, but it's easy enough to
change.

Also, to be clear: this is modeled after Rust, but it's not a requirement of
mine that it look exactly like it.  I just like the general style; I'll
surely deviate over time, as appropriate (or as I feel like it).

DEV-12151
main
Mike Gerwitz 2022-04-28 15:40:41 -04:00
parent 8119d1ca0d
commit 33baca113a
2 changed files with 63 additions and 3 deletions

View File

@ -558,8 +558,11 @@ impl<'d> Display for LineMark<'d> {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.col {
Some(col) => {
let underline = "^"
.repeat((col.end().get() - col.start().get()) as usize + 1);
let underline =
self.level.mark_char().to_string().repeat(
(col.end().get() - col.start().get()) as usize + 1,
);
let lpad = col.start().get() as usize - 1;
write!(f, " | {:lpad$}{underline}\n", "")?;
@ -577,6 +580,22 @@ impl<'d> Display for LineMark<'d> {
}
}
trait MarkChar {
fn mark_char(&self) -> char;
}
impl MarkChar for Level {
/// Character used to underline the columns applicable to a given span
/// underneath a source line.
fn mark_char(&self) -> char {
match self {
Level::InternalError => '!',
Level::Error => '^',
Level::Note | Level::Help => '-',
}
}
}
#[cfg(test)]
mod test {
use super::*;

View File

@ -357,7 +357,7 @@ internal error: multiple spans with labels of different severity level
--> foo/bar:4:9
|
| foo/bar line 4
| ^^^^^^
| !!!!!!
= internal error: an internal error
= error: an error
= note: a note
@ -506,3 +506,44 @@ error: offset at newline
"
);
}
#[test]
fn error_level_mark_styling() {
let ctx = Context::from("foo/bar");
assert_report!(
"multiple level mark styles",
vec![
ctx.span(0, 7).internal_error("A"),
ctx.span(15, 7).error("B"),
ctx.span(30, 7).note("C"),
ctx.span(45, 7).help("D"),
],
"\
internal error: multiple level mark styles
--> foo/bar:1:1
|
| foo/bar line 1
| !!!!!!!
= internal error: A
--> foo/bar:2:1
|
| foo/bar line 2
| ^^^^^^^
= error: B
--> foo/bar:3:1
|
| foo/bar line 3
| -------
= note: C
--> foo/bar:4:1
|
| foo/bar line 4
| -------
= help: D
"
);
}