Skip to content

Commit f319bc0

Browse files
committed
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 rust-lang#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 rust-lang#167
1 parent 7132bf3 commit f319bc0

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

src/renderer/display_list.rs

+1
Original file line numberDiff line numberDiff line change
@@ -909,6 +909,7 @@ pub(crate) enum DisplayAnnotationType {
909909
impl From<snippet::Level> for DisplayAnnotationType {
910910
fn from(at: snippet::Level) -> Self {
911911
match at {
912+
snippet::Level::None => DisplayAnnotationType::None,
912913
snippet::Level::Error => DisplayAnnotationType::Error,
913914
snippet::Level::Warning => DisplayAnnotationType::Warning,
914915
snippet::Level::Info => DisplayAnnotationType::Info,

src/snippet.rs

+2
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ impl<'a> Annotation<'a> {
126126
/// Types of annotations.
127127
#[derive(Debug, Clone, Copy, PartialEq)]
128128
pub enum Level {
129+
/// Do not attach any annotation.
130+
None,
129131
/// Error annotations are displayed using red color and "^" character.
130132
Error,
131133
/// Warning annotations are displayed using blue color and "-" character.

tests/formatter.rs

+29
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,35 @@ fn test_format_title() {
121121
assert_data_eq!(renderer.render(input).to_string(), expected);
122122
}
123123

124+
/// Tests that we can format a message *without* a header.
125+
///
126+
/// This uses `Level::None`, which is somewhat of a hacky API addition I made
127+
/// to our vendored copy of `annotate-snippets` in order to do exactly what
128+
/// this test asserts: skip the header.
129+
#[test]
130+
fn test_format_skip_title() {
131+
let source =
132+
"# Docstring followed by a newline\n\ndef foobar(foot, bar={}):\n \"\"\"\n \"\"\"\n";
133+
let src_annotation = Level::Error.span(56..58).label("B006");
134+
let snippet = Snippet::source(source)
135+
.line_start(1)
136+
.annotation(src_annotation)
137+
.fold(false);
138+
let message = Level::None.title("").snippet(snippet);
139+
140+
let expected = str![[r#"
141+
|
142+
1 | # Docstring followed by a newline
143+
2 |
144+
3 | def foobar(foot, bar={}):
145+
| ^^ B006
146+
4 | """
147+
5 | """
148+
|
149+
"#]];
150+
assert_data_eq!(Renderer::plain().render(message).to_string(), expected);
151+
}
152+
124153
#[test]
125154
fn test_format_snippet_only() {
126155
let source = "This is line 1\nThis is line 2";

0 commit comments

Comments
 (0)