mirror of
https://github.com/astral-sh/ruff.git
synced 2025-08-04 10:49:50 +00:00
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 https://github.com/rust-lang/annotate-snippets-rs/issues/167
This commit is contained in:
parent
9c27c57b5b
commit
1b97677779
3 changed files with 32 additions and 0 deletions
|
@ -909,6 +909,7 @@ pub(crate) enum DisplayAnnotationType {
|
||||||
impl From<snippet::Level> for DisplayAnnotationType {
|
impl From<snippet::Level> for DisplayAnnotationType {
|
||||||
fn from(at: snippet::Level) -> Self {
|
fn from(at: snippet::Level) -> Self {
|
||||||
match at {
|
match at {
|
||||||
|
snippet::Level::None => DisplayAnnotationType::None,
|
||||||
snippet::Level::Error => DisplayAnnotationType::Error,
|
snippet::Level::Error => DisplayAnnotationType::Error,
|
||||||
snippet::Level::Warning => DisplayAnnotationType::Warning,
|
snippet::Level::Warning => DisplayAnnotationType::Warning,
|
||||||
snippet::Level::Info => DisplayAnnotationType::Info,
|
snippet::Level::Info => DisplayAnnotationType::Info,
|
||||||
|
|
|
@ -126,6 +126,8 @@ impl<'a> Annotation<'a> {
|
||||||
/// Types of annotations.
|
/// Types of annotations.
|
||||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||||
pub enum Level {
|
pub enum Level {
|
||||||
|
/// Do not attach any annotation.
|
||||||
|
None,
|
||||||
/// Error annotations are displayed using red color and "^" character.
|
/// Error annotations are displayed using red color and "^" character.
|
||||||
Error,
|
Error,
|
||||||
/// Warning annotations are displayed using blue color and "-" character.
|
/// Warning annotations are displayed using blue color and "-" character.
|
||||||
|
|
|
@ -127,6 +127,35 @@ fn test_format_title() {
|
||||||
assert_data_eq!(renderer.render(input).to_string(), expected);
|
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]
|
#[test]
|
||||||
fn test_format_snippet_only() {
|
fn test_format_snippet_only() {
|
||||||
let source = "This is line 1\nThis is line 2";
|
let source = "This is line 1\nThis is line 2";
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue