file_level -> hide_snippet

This commit is contained in:
Brent Westbrook 2025-09-26 17:40:48 -04:00
parent 0668704fcc
commit 519b6fa850
6 changed files with 23 additions and 22 deletions

View file

@ -840,7 +840,7 @@ impl<'a> FormatResults<'a> {
let source_file = SourceFileBuilder::new(path, unformatted.source_code()).finish(); let source_file = SourceFileBuilder::new(path, unformatted.source_code()).finish();
let span = Span::from(source_file).with_range(range); let span = Span::from(source_file).with_range(range);
let mut annotation = Annotation::primary(span); let mut annotation = Annotation::primary(span);
annotation.set_file_level(true); annotation.hide_snippet(true);
diagnostic.annotate(annotation); diagnostic.annotate(annotation);
diagnostic.set_fix(fix); diagnostic.set_fix(fix);
@ -900,7 +900,7 @@ impl From<&FormatCommandError> for Diagnostic {
let file = SourceFileBuilder::new(path.to_string_lossy(), "").finish(); let file = SourceFileBuilder::new(path.to_string_lossy(), "").finish();
let span = Span::from(file); let span = Span::from(file);
let mut annotation = Annotation::primary(span); let mut annotation = Annotation::primary(span);
annotation.set_file_level(true); annotation.hide_snippet(true);
annotation annotation
}); });

View file

@ -155,7 +155,7 @@ impl<'a> Annotation<'a> {
self 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.is_file_level = yes;
self self
} }

View file

@ -758,11 +758,11 @@ pub struct Annotation {
is_primary: bool, is_primary: bool,
/// The diagnostic tags associated with this annotation. /// The diagnostic tags associated with this annotation.
tags: Vec<DiagnosticTag>, tags: Vec<DiagnosticTag>,
/// 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 /// When set, rendering will only include the file's name and (optional) range. Everything else
/// is omitted, including any file snippet or message. /// is omitted, including any file snippet or message.
is_file_level: bool, hide_snippet: bool,
} }
impl Annotation { impl Annotation {
@ -781,7 +781,7 @@ impl Annotation {
message: None, message: None,
is_primary: true, is_primary: true,
tags: Vec::new(), tags: Vec::new(),
is_file_level: false, hide_snippet: false,
} }
} }
@ -798,7 +798,7 @@ impl Annotation {
message: None, message: None,
is_primary: false, is_primary: false,
tags: Vec::new(), tags: Vec::new(),
is_file_level: false, hide_snippet: false,
} }
} }
@ -865,19 +865,20 @@ impl Annotation {
self.tags.push(tag); 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 /// Such annotations are only rendered with their file name and range, if available. This is
/// is intended for backwards compatibility with Ruff diagnostics, which historically used /// intended for backwards compatibility with Ruff diagnostics, which historically used
/// `TextRange::default` to indicate a file-level diagnostic. In the new diagnostic model, a /// `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` /// [`Span`] with a range of `None` should be used instead, as mentioned in the `Span`
/// documentation. /// documentation.
/// ///
/// TODO(brent) update this usage in Ruff and remove `is_file_level` entirely. See /// TODO(brent) update this usage in Ruff and remove `is_file_level` entirely. See
/// <https://github.com/astral-sh/ruff/issues/19688>, especially my first comment, for more /// <https://github.com/astral-sh/ruff/issues/19688>, especially my first comment, for more
/// details. /// details. As of 2025-09-26 we also use this to suppress snippet rendering for formatter
pub fn set_file_level(&mut self, yes: bool) { /// diagnostics, which also need to have a range, so we probably can't eliminate this entirely.
self.is_file_level = yes; pub fn hide_snippet(&mut self, yes: bool) {
self.hide_snippet = yes;
} }
} }

View file

@ -408,7 +408,7 @@ struct ResolvedAnnotation<'a> {
line_end: OneIndexed, line_end: OneIndexed,
message: Option<&'a str>, message: Option<&'a str>,
is_primary: bool, is_primary: bool,
is_file_level: bool, hide_snippet: bool,
notebook_index: Option<NotebookIndex>, notebook_index: Option<NotebookIndex>,
} }
@ -456,7 +456,7 @@ impl<'a> ResolvedAnnotation<'a> {
line_end, line_end,
message: ann.get_message(), message: ann.get_message(),
is_primary: ann.is_primary, 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), notebook_index: resolver.notebook_index(&ann.span.file),
}) })
} }
@ -722,8 +722,8 @@ struct RenderableAnnotation<'r> {
message: Option<&'r str>, message: Option<&'r str>,
/// Whether this annotation is considered "primary" or not. /// Whether this annotation is considered "primary" or not.
is_primary: bool, is_primary: bool,
/// Whether this annotation applies to an entire file, rather than a snippet within it. /// Whether the snippet for this annotation should be hidden instead of rendered.
is_file_level: bool, hide_snippet: bool,
} }
impl<'r> RenderableAnnotation<'r> { impl<'r> RenderableAnnotation<'r> {
@ -745,7 +745,7 @@ impl<'r> RenderableAnnotation<'r> {
range, range,
message: ann.message, message: ann.message,
is_primary: ann.is_primary, 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 { if let Some(message) = self.message {
ann = ann.label(message); ann = ann.label(message);
} }
ann.is_file_level(self.is_file_level) ann.hide_snippet(self.hide_snippet)
} }
} }

View file

@ -573,7 +573,7 @@ print()
let mut diagnostic = env.err().build(); let mut diagnostic = env.err().build();
let span = env.path("example.py").with_range(TextRange::default()); let span = env.path("example.py").with_range(TextRange::default());
let mut annotation = Annotation::primary(span); let mut annotation = Annotation::primary(span);
annotation.set_file_level(true); annotation.hide_snippet(true);
diagnostic.annotate(annotation); diagnostic.annotate(annotation);
insta::assert_snapshot!(env.render(&diagnostic), @r" insta::assert_snapshot!(env.render(&diagnostic), @r"

View file

@ -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 file = SourceFileBuilder::new(path.to_string_lossy(), "").finish();
let span = Span::from(file); let span = Span::from(file);
let mut annotation = Annotation::primary(span); let mut annotation = Annotation::primary(span);
annotation.set_file_level(true); annotation.hide_snippet(true);
diagnostic.annotate(annotation); 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 // 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. // https://github.com/astral-sh/ruff/issues/19688.
if range == TextRange::default() { if range == TextRange::default() {
annotation.set_file_level(true); annotation.hide_snippet(true);
} }
diagnostic.annotate(annotation); diagnostic.annotate(annotation);