fix(printer): Show snippet message for unnamed sources (#39)

Fixes: https://github.com/zkat/miette/issues/38
This commit is contained in:
Cormac Relf 2021-08-30 12:28:17 +10:00 committed by GitHub
parent 50c7a88360
commit 84219f6c80
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 92 additions and 17 deletions

View File

@ -189,23 +189,29 @@ impl GraphicalReportPrinter {
.len();
// Header
write!(
f,
"{}{}{}",
" ".repeat(linum_width + 2),
self.theme.characters.ltop,
self.theme.characters.hbar.to_string().repeat(3),
)?;
if let Some(source_name) = snippet.source.name() {
let source_name = source_name.style(self.theme.styles.filename);
write!(
f,
"{}{}{}[{}:{}:{}]",
" ".repeat(linum_width + 2),
self.theme.characters.ltop,
self.theme.characters.hbar.to_string().repeat(3),
"[{}:{}:{}]",
source_name,
contents.line() + 1,
contents.column() + 1
)?;
if let Some(msg) = &snippet.message {
write!(f, " {}:", msg)?;
}
writeln!(f)?;
} else {
write!(f, "[{}:{}]", contents.line() + 1, contents.column() + 1)?;
}
if let Some(msg) = &snippet.message {
write!(f, " {}:", msg)?;
}
writeln!(f)?;
// Now it's time for the fun part--actually rendering everything!
for line in &lines {

View File

@ -93,12 +93,16 @@ impl NarratableReportPrinter {
if let Some(filename) = snippet.source.name() {
write!(f, " for {}", filename,)?;
}
writeln!(
write!(
f,
" starting at line {}, column {}",
contents.line() + 1,
contents.column() + 1
)?;
if let Some(message) = snippet.message.as_deref() {
write!(f, ": {}", message)?;
}
writeln!(f)?;
writeln!(f)?;
// Highlights are the bits we're going to underline in our overall

View File

@ -45,7 +45,7 @@ fn single_line_highlight() -> Result<(), MietteError> {
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
snippet line 2: text
@ -87,7 +87,7 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> {
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
snippet line 2: text
@ -132,7 +132,7 @@ fn multiple_same_line_highlights() -> Result<(), MietteError> {
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
snippet line 2: text text text text text
@ -175,7 +175,7 @@ fn multiline_highlight_adjacent() -> Result<(), MietteError> {
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
snippet line 2: text
@ -226,7 +226,7 @@ line5
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: line1
highlight starting at line 1, column 1: block 1
@ -280,7 +280,7 @@ line5
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: line1
highlight starting at line 1, column 1: block 1
@ -328,7 +328,7 @@ fn multiple_multiline_highlights_adjacent() -> Result<(), MietteError> {
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
highlight starting at line 1, column 1: this bit here
@ -377,7 +377,7 @@ fn multiple_multiline_highlights_overlapping_lines() -> Result<(), MietteError>
oops!
Diagnostic severity: error
Begin snippet for bad_file.rs starting at line 1, column 1
Begin snippet for bad_file.rs starting at line 1, column 1: This is the part that broke
snippet line 1: source
highlight starting at line 1, column 1: this bit here
@ -441,3 +441,36 @@ diagnostic error code: oops::my::bad
assert_eq!(expected, out);
Ok(())
}
#[test]
fn unnamed_snippet_shows_message() {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
src: String,
#[snippet(src, message("This is the part that broke"))]
ctx: SourceSpan,
}
let src = "source_text_here".to_string();
let len = src.len();
let err = MyBad {
src,
ctx: (0, len).into(),
};
let out = fmt_report(err.into());
println!("{}", out);
let expected = r#"
oops!
Diagnostic severity: error
Begin snippet starting at line 1, column 1: This is the part that broke
snippet line 1: source_text_here
diagnostic help: try doing it better next time?
diagnostic error code: oops::my::bad
"#
.trim_start();
assert_eq!(out, expected);
}

View File

@ -491,3 +491,35 @@ fn disable_url_links() -> Result<(), MietteError> {
assert!(out.contains("oops::my::bad"));
Ok(())
}
#[test]
fn unnamed_snippet_shows_message() {
#[derive(Debug, Diagnostic, Error)]
#[error("oops!")]
#[diagnostic(code(oops::my::bad), help("try doing it better next time?"))]
struct MyBad {
src: String,
#[snippet(src, message("This is the part that broke"))]
ctx: SourceSpan,
}
let src = "source_text_here".to_string();
let len = src.len();
let err = MyBad {
src,
ctx: (0, len).into(),
};
let out = fmt_report(err.into());
println!("{}", out);
let expected = r#"
[oops::my::bad]
× oops!
[1:1] This is the part that broke:
1 source_text_here
try doing it better next time?
"#
.trim_start();
assert_eq!(out, expected);
}