mirror of https://github.com/zkat/miette.git
fix(printer): Show snippet message for unnamed sources (#39)
Fixes: https://github.com/zkat/miette/issues/38
This commit is contained in:
parent
50c7a88360
commit
84219f6c80
|
|
@ -189,23 +189,29 @@ impl GraphicalReportPrinter {
|
||||||
.len();
|
.len();
|
||||||
|
|
||||||
// Header
|
// 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() {
|
if let Some(source_name) = snippet.source.name() {
|
||||||
let source_name = source_name.style(self.theme.styles.filename);
|
let source_name = source_name.style(self.theme.styles.filename);
|
||||||
write!(
|
write!(
|
||||||
f,
|
f,
|
||||||
"{}{}{}[{}:{}:{}]",
|
"[{}:{}:{}]",
|
||||||
" ".repeat(linum_width + 2),
|
|
||||||
self.theme.characters.ltop,
|
|
||||||
self.theme.characters.hbar.to_string().repeat(3),
|
|
||||||
source_name,
|
source_name,
|
||||||
contents.line() + 1,
|
contents.line() + 1,
|
||||||
contents.column() + 1
|
contents.column() + 1
|
||||||
)?;
|
)?;
|
||||||
if let Some(msg) = &snippet.message {
|
} else {
|
||||||
write!(f, " {}:", msg)?;
|
write!(f, "[{}:{}]", contents.line() + 1, contents.column() + 1)?;
|
||||||
}
|
|
||||||
writeln!(f)?;
|
|
||||||
}
|
}
|
||||||
|
if let Some(msg) = &snippet.message {
|
||||||
|
write!(f, " {}:", msg)?;
|
||||||
|
}
|
||||||
|
writeln!(f)?;
|
||||||
|
|
||||||
// Now it's time for the fun part--actually rendering everything!
|
// Now it's time for the fun part--actually rendering everything!
|
||||||
for line in &lines {
|
for line in &lines {
|
||||||
|
|
|
||||||
|
|
@ -93,12 +93,16 @@ impl NarratableReportPrinter {
|
||||||
if let Some(filename) = snippet.source.name() {
|
if let Some(filename) = snippet.source.name() {
|
||||||
write!(f, " for {}", filename,)?;
|
write!(f, " for {}", filename,)?;
|
||||||
}
|
}
|
||||||
writeln!(
|
write!(
|
||||||
f,
|
f,
|
||||||
" starting at line {}, column {}",
|
" starting at line {}, column {}",
|
||||||
contents.line() + 1,
|
contents.line() + 1,
|
||||||
contents.column() + 1
|
contents.column() + 1
|
||||||
)?;
|
)?;
|
||||||
|
if let Some(message) = snippet.message.as_deref() {
|
||||||
|
write!(f, ": {}", message)?;
|
||||||
|
}
|
||||||
|
writeln!(f)?;
|
||||||
writeln!(f)?;
|
writeln!(f)?;
|
||||||
|
|
||||||
// Highlights are the bits we're going to underline in our overall
|
// Highlights are the bits we're going to underline in our overall
|
||||||
|
|
|
||||||
|
|
@ -45,7 +45,7 @@ fn single_line_highlight() -> Result<(), MietteError> {
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
snippet line 2: text
|
snippet line 2: text
|
||||||
|
|
@ -87,7 +87,7 @@ fn single_line_highlight_no_label() -> Result<(), MietteError> {
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
snippet line 2: text
|
snippet line 2: text
|
||||||
|
|
@ -132,7 +132,7 @@ fn multiple_same_line_highlights() -> Result<(), MietteError> {
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
snippet line 2: text text text text text
|
snippet line 2: text text text text text
|
||||||
|
|
@ -175,7 +175,7 @@ fn multiline_highlight_adjacent() -> Result<(), MietteError> {
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
snippet line 2: text
|
snippet line 2: text
|
||||||
|
|
@ -226,7 +226,7 @@ line5
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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
|
snippet line 1: line1
|
||||||
highlight starting at line 1, column 1: block 1
|
highlight starting at line 1, column 1: block 1
|
||||||
|
|
@ -280,7 +280,7 @@ line5
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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
|
snippet line 1: line1
|
||||||
highlight starting at line 1, column 1: block 1
|
highlight starting at line 1, column 1: block 1
|
||||||
|
|
@ -328,7 +328,7 @@ fn multiple_multiline_highlights_adjacent() -> Result<(), MietteError> {
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
highlight starting at line 1, column 1: this bit here
|
highlight starting at line 1, column 1: this bit here
|
||||||
|
|
@ -377,7 +377,7 @@ fn multiple_multiline_highlights_overlapping_lines() -> Result<(), MietteError>
|
||||||
oops!
|
oops!
|
||||||
Diagnostic severity: error
|
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 1: source
|
||||||
highlight starting at line 1, column 1: this bit here
|
highlight starting at line 1, column 1: this bit here
|
||||||
|
|
@ -441,3 +441,36 @@ diagnostic error code: oops::my::bad
|
||||||
assert_eq!(expected, out);
|
assert_eq!(expected, out);
|
||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -491,3 +491,35 @@ fn disable_url_links() -> Result<(), MietteError> {
|
||||||
assert!(out.contains("oops::my::bad"));
|
assert!(out.contains("oops::my::bad"));
|
||||||
Ok(())
|
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);
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue