From f319bc0e18c0087e2594a7c938e5218c9b38619b Mon Sep 17 00:00:00 2001 From: Andrew Gallant Date: Wed, 8 Jan 2025 15:59:09 -0500 Subject: [PATCH] feat: Add `Level::None` for omitting the header When combined with an empty `title`, this results in the header being omitted entirely. This isn't what @epage suggested in #167 since changing our rendering to pass in a custom header is a bigger refactor than what I've been able to get to. The goal of this change was to be very small without requiring bigger code changes on our end. Unfortunately, this is a breaking change, so I'm not sure what the appetite is for this. Fixes #167 --- src/renderer/display_list.rs | 1 + src/snippet.rs | 2 ++ tests/formatter.rs | 29 +++++++++++++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/renderer/display_list.rs b/src/renderer/display_list.rs index c2cb82b..1df5a9f 100644 --- a/src/renderer/display_list.rs +++ b/src/renderer/display_list.rs @@ -909,6 +909,7 @@ pub(crate) enum DisplayAnnotationType { impl From for DisplayAnnotationType { fn from(at: snippet::Level) -> Self { match at { + snippet::Level::None => DisplayAnnotationType::None, snippet::Level::Error => DisplayAnnotationType::Error, snippet::Level::Warning => DisplayAnnotationType::Warning, snippet::Level::Info => DisplayAnnotationType::Info, diff --git a/src/snippet.rs b/src/snippet.rs index 8e9a3a8..e0c9edf 100644 --- a/src/snippet.rs +++ b/src/snippet.rs @@ -126,6 +126,8 @@ impl<'a> Annotation<'a> { /// Types of annotations. #[derive(Debug, Clone, Copy, PartialEq)] pub enum Level { + /// Do not attach any annotation. + None, /// Error annotations are displayed using red color and "^" character. Error, /// Warning annotations are displayed using blue color and "-" character. diff --git a/tests/formatter.rs b/tests/formatter.rs index 6faab76..d9582ea 100644 --- a/tests/formatter.rs +++ b/tests/formatter.rs @@ -121,6 +121,35 @@ fn test_format_title() { assert_data_eq!(renderer.render(input).to_string(), expected); } +/// Tests that we can format a message *without* a header. +/// +/// This uses `Level::None`, which is somewhat of a hacky API addition I made +/// to our vendored copy of `annotate-snippets` in order to do exactly what +/// this test asserts: skip the header. +#[test] +fn test_format_skip_title() { + let source = + "# Docstring followed by a newline\n\ndef foobar(foot, bar={}):\n \"\"\"\n \"\"\"\n"; + let src_annotation = Level::Error.span(56..58).label("B006"); + let snippet = Snippet::source(source) + .line_start(1) + .annotation(src_annotation) + .fold(false); + let message = Level::None.title("").snippet(snippet); + + let expected = str![[r#" + | +1 | # Docstring followed by a newline +2 | +3 | def foobar(foot, bar={}): + | ^^ B006 +4 | """ +5 | """ + | +"#]]; + assert_data_eq!(Renderer::plain().render(message).to_string(), expected); +} + #[test] fn test_format_snippet_only() { let source = "This is line 1\nThis is line 2";