From 845edb930c5fb17553cb4dce0fc71c4f4f1fa56d Mon Sep 17 00:00:00 2001 From: clubby789 Date: Sun, 10 Nov 2024 14:06:49 +0000 Subject: [PATCH] Update annotate-snippets Co-authored-by: printfn --- Cargo.lock | 23 +++------ Cargo.toml | 2 +- src/format_report_formatter.rs | 86 +++++++++++----------------------- 3 files changed, 36 insertions(+), 75 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e2ceb668ebd..6486468a2fa 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,12 +13,12 @@ dependencies = [ [[package]] name = "annotate-snippets" -version = "0.9.1" +version = "0.11.4" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c3b9d411ecbaf79885c6df4d75fff75858d5995ff25385657a28af47e82f9c36" +checksum = "24e35ed54e5ea7997c14ed4c70ba043478db1112e98263b3b035907aa197d991" dependencies = [ + "anstyle", "unicode-width", - "yansi-term", ] [[package]] @@ -37,9 +37,9 @@ dependencies = [ [[package]] name = "anstyle" -version = "1.0.3" +version = "1.0.10" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b84bf0a05bbb2a83e5eb6fa36bb6e87baa08193c35ff52bbf6b38d8af2890e46" +checksum = "55cc3b69f167a1ef2e161439aa98aed94e6028e5f9a59be9a6ffb47aef1651f9" [[package]] name = "anstyle-parse" @@ -772,9 +772,9 @@ checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" [[package]] name = "unicode-width" -version = "0.1.10" +version = "0.1.14" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b" +checksum = "7dd6e30e90baa6f72411720665d41d89b9a3d039dc45b8faea1ddd07f617f6af" [[package]] name = "utf8parse" @@ -910,12 +910,3 @@ checksum = "ca0ace3845f0d96209f0375e6d367e3eb87eb65d27d445bdc9f1843a26f39448" dependencies = [ "memchr", ] - -[[package]] -name = "yansi-term" -version = "0.1.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fe5c30ade05e61656247b2e334a031dfd0cc466fadef865bdcdea8d537951bf1" -dependencies = [ - "winapi", -] diff --git a/Cargo.toml b/Cargo.toml index e497b792342..f119cde5257 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -33,7 +33,7 @@ rustfmt-format-diff = [] generic-simd = ["bytecount/generic-simd"] [dependencies] -annotate-snippets = { version = "0.9", features = ["color"] } +annotate-snippets = { version = "0.11" } anyhow = "1.0" bytecount = "0.6.8" cargo_metadata = "0.18" diff --git a/src/format_report_formatter.rs b/src/format_report_formatter.rs index fd536d4df41..08889c712a5 100644 --- a/src/format_report_formatter.rs +++ b/src/format_report_formatter.rs @@ -1,7 +1,6 @@ use crate::formatting::FormattingError; use crate::{ErrorKind, FormatReport}; -use annotate_snippets::display_list::{DisplayList, FormatOptions}; -use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation}; +use annotate_snippets::{Annotation, Level, Renderer, Snippet}; use std::fmt::{self, Display}; /// A builder for [`FormatReportFormatter`]. @@ -49,51 +48,35 @@ impl<'a> Display for FormatReportFormatter<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { let errors_by_file = &self.report.internal.borrow().0; - let opt = FormatOptions { - color: self.enable_colors, - ..Default::default() + let renderer = if self.enable_colors { + Renderer::styled() + } else { + Renderer::plain() }; for (file, errors) in errors_by_file { for error in errors { let error_kind = error.kind.to_string(); - let title = Some(Annotation { - id: if error.is_internal() { - Some("internal") - } else { - None - }, - label: Some(&error_kind), - annotation_type: error_kind_to_snippet_annotation_type(&error.kind), - }); + let mut message = + error_kind_to_snippet_annotation_level(&error.kind).title(&error_kind); + if error.is_internal() { + message = message.id("internal"); + } let message_suffix = error.msg_suffix(); - let footer = if !message_suffix.is_empty() { - Some(Annotation { - id: None, - label: Some(message_suffix), - annotation_type: AnnotationType::Note, - }) - } else { - None - }; + if !message_suffix.is_empty() { + message = message.footer(Level::Note.title(&message_suffix)); + } let origin = format!("{}:{}", file, error.line); - let slice = Slice { - source: &error.line_buffer.clone(), - line_start: error.line, - origin: Some(origin.as_str()), - fold: false, - annotations: slice_annotation(error).into_iter().collect(), - }; - - let snippet = Snippet { - title, - footer: footer.into_iter().collect(), - slices: vec![slice], - opt, - }; - writeln!(f, "{}\n", DisplayList::from(snippet))?; + let snippet = Snippet::source(&error.line_buffer) + .line_start(error.line) + .origin(&origin) + .fold(false) + .annotations(annotation(error)); + message = message.snippet(snippet); + + writeln!(f, "{}\n", renderer.render(message))?; } } @@ -102,39 +85,26 @@ impl<'a> Display for FormatReportFormatter<'a> { "rustfmt has failed to format. See previous {} errors.", self.report.warning_count() ); - let snippet = Snippet { - title: Some(Annotation { - id: None, - label: Some(&label), - annotation_type: AnnotationType::Warning, - }), - footer: Vec::new(), - slices: Vec::new(), - opt, - }; - writeln!(f, "{}", DisplayList::from(snippet))?; + let message = Level::Warning.title(&label); + writeln!(f, "{}", renderer.render(message))?; } Ok(()) } } -fn slice_annotation(error: &FormattingError) -> Option> { +fn annotation(error: &FormattingError) -> Option> { let (range_start, range_length) = error.format_len(); let range_end = range_start + range_length; if range_length > 0 { - Some(SourceAnnotation { - annotation_type: AnnotationType::Error, - range: (range_start, range_end), - label: "", - }) + Some(Level::Error.span(range_start..range_end)) } else { None } } -fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationType { +fn error_kind_to_snippet_annotation_level(error_kind: &ErrorKind) -> Level { match error_kind { ErrorKind::LineOverflow(..) | ErrorKind::TrailingWhitespace @@ -144,7 +114,7 @@ fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationTy | ErrorKind::LostComment | ErrorKind::BadAttr | ErrorKind::InvalidGlobPattern(_) - | ErrorKind::VersionMismatch => AnnotationType::Error, - ErrorKind::DeprecatedAttr => AnnotationType::Warning, + | ErrorKind::VersionMismatch => Level::Error, + ErrorKind::DeprecatedAttr => Level::Warning, } }