Skip to content

Commit 4633930

Browse files
authored
Upload annotate-snippets to 0.8 (#4161)
1 parent a4cd6ae commit 4633930

File tree

3 files changed

+67
-68
lines changed

3 files changed

+67
-68
lines changed

Cargo.lock

+18-3
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

rustfmt-core/rustfmt-lib/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ emitter = [
2929
]
3030

3131
[dependencies]
32-
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
32+
annotate-snippets = { version = "0.8", features = ["color"] }
3333
anyhow = "1.0"
3434
bytecount = "0.6"
3535
dunce = "1.0"

rustfmt-core/rustfmt-lib/src/format_report_formatter.rs

+48-64
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
1-
use crate::config::FileName;
21
use crate::{
32
formatting::report::FormatReport,
43
result::{ErrorKind, FormatError},
54
};
6-
use annotate_snippets::display_list::DisplayList;
7-
use annotate_snippets::formatter::DisplayListFormatter;
5+
use annotate_snippets::display_list::{DisplayList, FormatOptions};
86
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
97
use std::fmt::{self, Display};
108

@@ -50,85 +48,71 @@ pub struct FormatReportFormatter<'a> {
5048

5149
impl<'a> Display for FormatReportFormatter<'a> {
5250
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
53-
let formatter = DisplayListFormatter::new(self.enable_colors, false);
54-
51+
let opt = FormatOptions {
52+
color: self.enable_colors,
53+
..Default::default()
54+
};
5555
for (file, errors) in self.report.format_result_as_rc().borrow().iter() {
5656
for error in errors.errors_excluding_macro() {
57-
let snippet = formatting_error_to_snippet(file, error);
58-
writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?;
57+
let annotation_type = error_kind_to_snippet_annotation_type(&error.kind());
58+
let label = error.kind().to_string();
59+
let title = Annotation {
60+
id: None,
61+
label: Some(&label),
62+
annotation_type,
63+
};
64+
let origin = error
65+
.line_num()
66+
.as_ref()
67+
.map(|line_num| format!("{}:{}", file, line_num));
68+
let slice = Slice {
69+
source: error.line_str().unwrap_or(""),
70+
line_start: error.line_num().unwrap_or(0),
71+
origin: origin.as_ref().map(|s| s.as_str()),
72+
fold: false,
73+
annotations: slice_annotation(error).into_iter().collect(),
74+
};
75+
76+
let snippet = Snippet {
77+
title: Some(title),
78+
slices: vec![slice],
79+
footer: vec![],
80+
opt,
81+
};
82+
writeln!(f, "{}\n", DisplayList::from(snippet))?;
5983
}
6084
}
6185

6286
if self.report.has_errors() {
63-
let snippet = formatting_failure_snippet(self.report.warning_count());
64-
writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?;
87+
let label = format!(
88+
"rustfmt has failed to format. See previous {} errors.",
89+
self.report.warning_count()
90+
);
91+
let snippet = Snippet {
92+
title: Some(Annotation {
93+
id: None,
94+
label: Some(&label),
95+
annotation_type: AnnotationType::Warning,
96+
}),
97+
slices: Vec::new(),
98+
footer: vec![],
99+
opt,
100+
};
101+
writeln!(f, "{}", DisplayList::from(snippet))?;
65102
}
66103

67104
Ok(())
68105
}
69106
}
70107

71-
fn formatting_failure_snippet(warning_count: usize) -> Snippet {
72-
Snippet {
73-
title: Some(Annotation {
74-
id: None,
75-
label: Some(format!(
76-
"rustfmt has failed to format. See previous {} errors.",
77-
warning_count
78-
)),
79-
annotation_type: AnnotationType::Warning,
80-
}),
81-
slices: Vec::new(),
82-
footer: vec![],
83-
}
84-
}
85-
86-
fn formatting_error_to_snippet(file: &FileName, error: &FormatError) -> Snippet {
87-
let slices = vec![snippet_code_slice(file, error)];
88-
let title = Some(snippet_title(error));
89-
90-
Snippet {
91-
title,
92-
slices,
93-
footer: vec![],
94-
}
95-
}
96-
97-
fn snippet_title(error: &FormatError) -> Annotation {
98-
let annotation_type = error_kind_to_snippet_annotation_type(&error.kind());
99-
100-
Annotation {
101-
id: None,
102-
label: Some(error.kind().to_string()),
103-
annotation_type,
104-
}
105-
}
106-
107-
fn snippet_code_slice(file: &FileName, error: &FormatError) -> Slice {
108-
let annotations = slice_annotation(error).into_iter().collect();
109-
let origin = error
110-
.line_num()
111-
.as_ref()
112-
.map(|line_num| format!("{}:{}", file, line_num));
113-
let source = error.line_str().unwrap_or("").to_owned();
114-
115-
Slice {
116-
source,
117-
line_start: error.line_num().unwrap_or(0),
118-
origin,
119-
fold: false,
120-
annotations,
121-
}
122-
}
123-
124-
fn slice_annotation(error: &FormatError) -> Option<SourceAnnotation> {
108+
fn slice_annotation(error: &FormatError) -> Option<SourceAnnotation<'static>> {
125109
match error.format_len() {
126110
Some((range_start, range_length)) if range_length > 0 => {
127111
let range_end = range_start + range_length;
128112
Some(SourceAnnotation {
129113
annotation_type: AnnotationType::Error,
130114
range: (range_start, range_end),
131-
label: String::new(),
115+
label: "",
132116
})
133117
}
134118
_ => None,

0 commit comments

Comments
 (0)