From 84219f6c80c2c432fbeb4c40a591380285de8767 Mon Sep 17 00:00:00 2001 From: Cormac Relf Date: Mon, 30 Aug 2021 12:28:17 +1000 Subject: [PATCH] fix(printer): Show snippet message for unnamed sources (#39) Fixes: https://github.com/zkat/miette/issues/38 --- src/printer/graphical_printer.rs | 22 +++++++++----- src/printer/narratable_printer.rs | 6 +++- tests/narrated.rs | 49 ++++++++++++++++++++++++++----- tests/printer.rs | 32 ++++++++++++++++++++ 4 files changed, 92 insertions(+), 17 deletions(-) diff --git a/src/printer/graphical_printer.rs b/src/printer/graphical_printer.rs index 897c65f..3625996 100644 --- a/src/printer/graphical_printer.rs +++ b/src/printer/graphical_printer.rs @@ -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 { diff --git a/src/printer/narratable_printer.rs b/src/printer/narratable_printer.rs index 37c4c83..d7ea72c 100644 --- a/src/printer/narratable_printer.rs +++ b/src/printer/narratable_printer.rs @@ -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 diff --git a/tests/narrated.rs b/tests/narrated.rs index f6f143e..778d3fe 100644 --- a/tests/narrated.rs +++ b/tests/narrated.rs @@ -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); +} diff --git a/tests/printer.rs b/tests/printer.rs index 2591899..9465789 100644 --- a/tests/printer.rs +++ b/tests/printer.rs @@ -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); +}