From 9fd6101d0982d1cc7e548775f0eed3e2c32f4346 Mon Sep 17 00:00:00 2001 From: Yuki Okushi Date: Thu, 18 Mar 2021 03:45:15 +0900 Subject: [PATCH] Upgrade `annotate-snippets` to 0.8 --- Cargo.lock | 25 +++--- Cargo.toml | 2 +- src/format_report_formatter.rs | 146 ++++++++++++++------------------- 3 files changed, 76 insertions(+), 97 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6e65d5626a2..709a21804c5 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -9,20 +9,14 @@ dependencies = [ "memchr", ] -[[package]] -name = "annotate-snippets" -version = "0.6.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c7021ce4924a3f25f802b2cccd1af585e39ea1a363a1aa2e72afe54b67a3a7a7" -dependencies = [ - "ansi_term", -] - [[package]] name = "annotate-snippets" version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "d78ea013094e5ea606b1c05fe35f1dd7ea1eb1ea259908d040b25bd5ec677ee5" +dependencies = [ + "yansi-term", +] [[package]] name = "ansi_term" @@ -987,7 +981,7 @@ version = "706.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "988c7a3ecba53728d383c6bd8268c7a4c957244944c05a3f0e448795747d9db9" dependencies = [ - "annotate-snippets 0.8.0", + "annotate-snippets", "atty", "rustc-ap-rustc_data_structures", "rustc-ap-rustc_lint_defs", @@ -1245,7 +1239,7 @@ dependencies = [ name = "rustfmt-nightly" version = "1.4.36" dependencies = [ - "annotate-snippets 0.6.1", + "annotate-snippets", "anyhow", "bytecount", "cargo_metadata", @@ -1708,3 +1702,12 @@ dependencies = [ "winapi", "winapi-util", ] + +[[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 89aa311c52c..40c1135aed6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -52,7 +52,7 @@ unicode-width = "0.1.5" unicode_categories = "0.1.1" dirs = "2.0.1" ignore = "0.4.11" -annotate-snippets = { version = "0.6", features = ["ansi_term"] } +annotate-snippets = { version = "0.8", features = ["color"] } structopt = "0.3" rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" } lazy_static = "1.0.0" diff --git a/src/format_report_formatter.rs b/src/format_report_formatter.rs index 2fc6c4e8955..c820259256c 100644 --- a/src/format_report_formatter.rs +++ b/src/format_report_formatter.rs @@ -1,8 +1,6 @@ -use crate::config::FileName; use crate::formatting::FormattingError; use crate::{ErrorKind, FormatReport}; -use annotate_snippets::display_list::DisplayList; -use annotate_snippets::formatter::DisplayListFormatter; +use annotate_snippets::display_list::{DisplayList, FormatOptions}; use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation}; use std::fmt::{self, Display}; @@ -48,91 +46,79 @@ pub struct FormatReportFormatter<'a> { impl<'a> Display for FormatReportFormatter<'a> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let formatter = DisplayListFormatter::new(self.enable_colors, false); let errors_by_file = &self.report.internal.borrow().0; + let opt = FormatOptions { + color: self.enable_colors, + ..Default::default() + }; + for (file, errors) in errors_by_file { for error in errors { - let snippet = formatting_error_to_snippet(file, error); - writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?; + 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 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 + }; + + 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))?; } } if !errors_by_file.is_empty() { - let snippet = formatting_failure_snippet(self.report.warning_count()); - writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?; + let label = format!( + "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))?; } Ok(()) } } -fn formatting_failure_snippet(warning_count: usize) -> Snippet { - Snippet { - title: Some(Annotation { - id: None, - label: Some(format!( - "rustfmt has failed to format. See previous {} errors.", - warning_count - )), - annotation_type: AnnotationType::Warning, - }), - footer: Vec::new(), - slices: Vec::new(), - } -} - -fn formatting_error_to_snippet(file: &FileName, error: &FormattingError) -> Snippet { - let slices = vec![snippet_code_slice(file, error)]; - let title = Some(snippet_title(error)); - let footer = snippet_footer(error).into_iter().collect(); - - Snippet { - title, - footer, - slices, - } -} - -fn snippet_title(error: &FormattingError) -> Annotation { - let annotation_type = error_kind_to_snippet_annotation_type(&error.kind); - - Annotation { - id: title_annotation_id(error), - label: Some(error.kind.to_string()), - annotation_type, - } -} - -fn snippet_footer(error: &FormattingError) -> Option { - let message_suffix = error.msg_suffix(); - - if !message_suffix.is_empty() { - Some(Annotation { - id: None, - label: Some(message_suffix.to_string()), - annotation_type: AnnotationType::Note, - }) - } else { - None - } -} - -fn snippet_code_slice(file: &FileName, error: &FormattingError) -> Slice { - let annotations = slice_annotation(error).into_iter().collect(); - let origin = Some(format!("{}:{}", file, error.line)); - let source = error.line_buffer.clone(); - - Slice { - source, - line_start: error.line, - origin, - fold: false, - annotations, - } -} - -fn slice_annotation(error: &FormattingError) -> Option { +fn slice_annotation(error: &FormattingError) -> Option> { let (range_start, range_length) = error.format_len(); let range_end = range_start + range_length; @@ -140,23 +126,13 @@ fn slice_annotation(error: &FormattingError) -> Option { Some(SourceAnnotation { annotation_type: AnnotationType::Error, range: (range_start, range_end), - label: String::new(), + label: "", }) } else { None } } -fn title_annotation_id(error: &FormattingError) -> Option { - const INTERNAL_ERROR_ID: &str = "internal"; - - if error.is_internal() { - Some(INTERNAL_ERROR_ID.to_string()) - } else { - None - } -} - fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationType { match error_kind { ErrorKind::LineOverflow(..)