Skip to content

Commit

Permalink
feat: Add Level::None for omitting the header
Browse files Browse the repository at this point in the history
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
  • Loading branch information
BurntSushi committed Jan 8, 2025
1 parent 7132bf3 commit f319bc0
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/renderer/display_list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -909,6 +909,7 @@ pub(crate) enum DisplayAnnotationType {
impl From<snippet::Level> 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,
Expand Down
2 changes: 2 additions & 0 deletions src/snippet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
29 changes: 29 additions & 0 deletions tests/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down

0 comments on commit f319bc0

Please sign in to comment.