Skip to content

Commit

Permalink
Upgrade annotate-snippets to 0.8 (#4762)
Browse files Browse the repository at this point in the history
  • Loading branch information
JohnTitor authored Apr 22, 2021
1 parent 84ff001 commit ddb224a
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 89 deletions.
15 changes: 12 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
146 changes: 61 additions & 85 deletions src/format_report_formatter.rs
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -48,115 +46,93 @@ 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<Annotation> {
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<SourceAnnotation> {
fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation<'_>> {
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: String::new(),
label: "",
})
} else {
None
}
}

fn title_annotation_id(error: &FormattingError) -> Option<String> {
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(..)
Expand Down

0 comments on commit ddb224a

Please sign in to comment.