diff --git a/crates/ruff/src/commands/format.rs b/crates/ruff/src/commands/format.rs index 8525e37656..554fe17788 100644 --- a/crates/ruff/src/commands/format.rs +++ b/crates/ruff/src/commands/format.rs @@ -840,7 +840,7 @@ impl<'a> FormatResults<'a> { let source_file = SourceFileBuilder::new(path, unformatted.source_code()).finish(); let span = Span::from(source_file).with_range(range); let mut annotation = Annotation::primary(span); - annotation.set_file_level(true); + annotation.hide_snippet(true); diagnostic.annotate(annotation); diagnostic.set_fix(fix); @@ -900,7 +900,7 @@ impl From<&FormatCommandError> for Diagnostic { let file = SourceFileBuilder::new(path.to_string_lossy(), "").finish(); let span = Span::from(file); let mut annotation = Annotation::primary(span); - annotation.set_file_level(true); + annotation.hide_snippet(true); annotation }); diff --git a/crates/ruff_annotate_snippets/src/snippet.rs b/crates/ruff_annotate_snippets/src/snippet.rs index 4fca75c571..76d615189d 100644 --- a/crates/ruff_annotate_snippets/src/snippet.rs +++ b/crates/ruff_annotate_snippets/src/snippet.rs @@ -155,7 +155,7 @@ impl<'a> Annotation<'a> { self } - pub fn is_file_level(mut self, yes: bool) -> Self { + pub fn hide_snippet(mut self, yes: bool) -> Self { self.is_file_level = yes; self } diff --git a/crates/ruff_db/src/diagnostic/mod.rs b/crates/ruff_db/src/diagnostic/mod.rs index 90564c84a9..dd32f61c7b 100644 --- a/crates/ruff_db/src/diagnostic/mod.rs +++ b/crates/ruff_db/src/diagnostic/mod.rs @@ -758,11 +758,11 @@ pub struct Annotation { is_primary: bool, /// The diagnostic tags associated with this annotation. tags: Vec, - /// Whether this annotation is a file-level or full-file annotation. + /// Whether the snippet for this annotation should be hidden. /// /// When set, rendering will only include the file's name and (optional) range. Everything else /// is omitted, including any file snippet or message. - is_file_level: bool, + hide_snippet: bool, } impl Annotation { @@ -781,7 +781,7 @@ impl Annotation { message: None, is_primary: true, tags: Vec::new(), - is_file_level: false, + hide_snippet: false, } } @@ -798,7 +798,7 @@ impl Annotation { message: None, is_primary: false, tags: Vec::new(), - is_file_level: false, + hide_snippet: false, } } @@ -865,19 +865,20 @@ impl Annotation { self.tags.push(tag); } - /// Set whether or not this annotation is file-level. + /// Set whether or not the snippet on this annotation should be suppressed when rendering. /// - /// File-level annotations are only rendered with their file name and range, if available. This - /// is intended for backwards compatibility with Ruff diagnostics, which historically used + /// Such annotations are only rendered with their file name and range, if available. This is + /// intended for backwards compatibility with Ruff diagnostics, which historically used /// `TextRange::default` to indicate a file-level diagnostic. In the new diagnostic model, a /// [`Span`] with a range of `None` should be used instead, as mentioned in the `Span` /// documentation. /// /// TODO(brent) update this usage in Ruff and remove `is_file_level` entirely. See /// , especially my first comment, for more - /// details. - pub fn set_file_level(&mut self, yes: bool) { - self.is_file_level = yes; + /// details. As of 2025-09-26 we also use this to suppress snippet rendering for formatter + /// diagnostics, which also need to have a range, so we probably can't eliminate this entirely. + pub fn hide_snippet(&mut self, yes: bool) { + self.hide_snippet = yes; } } diff --git a/crates/ruff_db/src/diagnostic/render.rs b/crates/ruff_db/src/diagnostic/render.rs index 785329af9c..51caed471b 100644 --- a/crates/ruff_db/src/diagnostic/render.rs +++ b/crates/ruff_db/src/diagnostic/render.rs @@ -408,7 +408,7 @@ struct ResolvedAnnotation<'a> { line_end: OneIndexed, message: Option<&'a str>, is_primary: bool, - is_file_level: bool, + hide_snippet: bool, notebook_index: Option, } @@ -456,7 +456,7 @@ impl<'a> ResolvedAnnotation<'a> { line_end, message: ann.get_message(), is_primary: ann.is_primary, - is_file_level: ann.is_file_level, + hide_snippet: ann.hide_snippet, notebook_index: resolver.notebook_index(&ann.span.file), }) } @@ -722,8 +722,8 @@ struct RenderableAnnotation<'r> { message: Option<&'r str>, /// Whether this annotation is considered "primary" or not. is_primary: bool, - /// Whether this annotation applies to an entire file, rather than a snippet within it. - is_file_level: bool, + /// Whether the snippet for this annotation should be hidden instead of rendered. + hide_snippet: bool, } impl<'r> RenderableAnnotation<'r> { @@ -745,7 +745,7 @@ impl<'r> RenderableAnnotation<'r> { range, message: ann.message, is_primary: ann.is_primary, - is_file_level: ann.is_file_level, + hide_snippet: ann.hide_snippet, } } @@ -771,7 +771,7 @@ impl<'r> RenderableAnnotation<'r> { if let Some(message) = self.message { ann = ann.label(message); } - ann.is_file_level(self.is_file_level) + ann.hide_snippet(self.hide_snippet) } } diff --git a/crates/ruff_db/src/diagnostic/render/full.rs b/crates/ruff_db/src/diagnostic/render/full.rs index eb782e72c3..c87413a84e 100644 --- a/crates/ruff_db/src/diagnostic/render/full.rs +++ b/crates/ruff_db/src/diagnostic/render/full.rs @@ -573,7 +573,7 @@ print() let mut diagnostic = env.err().build(); let span = env.path("example.py").with_range(TextRange::default()); let mut annotation = Annotation::primary(span); - annotation.set_file_level(true); + annotation.hide_snippet(true); diagnostic.annotate(annotation); insta::assert_snapshot!(env.render(&diagnostic), @r" diff --git a/crates/ruff_linter/src/message/mod.rs b/crates/ruff_linter/src/message/mod.rs index 68a8d6257f..66b6020332 100644 --- a/crates/ruff_linter/src/message/mod.rs +++ b/crates/ruff_linter/src/message/mod.rs @@ -86,7 +86,7 @@ pub fn create_panic_diagnostic(error: &PanicError, path: Option<&Path>) -> Diagn let file = SourceFileBuilder::new(path.to_string_lossy(), "").finish(); let span = Span::from(file); let mut annotation = Annotation::primary(span); - annotation.set_file_level(true); + annotation.hide_snippet(true); diagnostic.annotate(annotation); } @@ -122,7 +122,7 @@ where // actually need it, but we need to be able to cache the new diagnostic model first. See // https://github.com/astral-sh/ruff/issues/19688. if range == TextRange::default() { - annotation.set_file_level(true); + annotation.hide_snippet(true); } diagnostic.annotate(annotation);