Skip to content

Commit c4e7011

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 b733c70 commit c4e7011

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
@@ -127,6 +127,35 @@ fn test_format_title() {
127127
assert_data_eq!(renderer.render(input).to_string(), expected);
128128
}
129129

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

0 commit comments

Comments
 (0)