diff --git a/tamer/src/diagnose/report.rs b/tamer/src/diagnose/report.rs index 909a6c11..e60a8c35 100644 --- a/tamer/src/diagnose/report.rs +++ b/tamer/src/diagnose/report.rs @@ -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::*; diff --git a/tamer/src/diagnose/report/test/integration.rs b/tamer/src/diagnose/report/test/integration.rs index 9eb15de1..cab98876 100644 --- a/tamer/src/diagnose/report/test/integration.rs +++ b/tamer/src/diagnose/report/test/integration.rs @@ -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 +" + ); +}