Skip to content

Commit 7aba175

Browse files
committed
ruff_annotate_snippets: make small change to enable omitting header
This is a tiny change that, perhaps slightly shady, permits us to use the `annotate-snippets` renderer without its mandatory header (which wasn't there in `annotate-snippets 0.9`). Specifically, we can now do this: Level::None.title("") The combination of a "none" level and an empty label results in the `annotate-snippets` header being skipped entirely. (Not even an empty line is written.) This is maybe not the right API for upstream `annotate-snippets`, but it's very easy for us to do and unblocks the upgrade (albeit relying on a vendored copy). Ref rust-lang/annotate-snippets-rs#167
1 parent 3e19c38 commit 7aba175

File tree

3 files changed

+32
-0
lines changed

3 files changed

+32
-0
lines changed

crates/ruff_annotate_snippets/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,

crates/ruff_annotate_snippets/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.

crates/ruff_annotate_snippets/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(foor, 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(foor, 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)