Skip to content

Commit ddb224a

Browse files
authored
Upgrade annotate-snippets to 0.8 (#4762)
1 parent 84ff001 commit ddb224a

File tree

3 files changed

+74
-89
lines changed

3 files changed

+74
-89
lines changed

Diff for: Cargo.lock

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

Diff for: Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ unicode-width = "0.1.5"
5252
unicode_categories = "0.1.1"
5353
dirs = "2.0.1"
5454
ignore = "0.4.11"
55-
annotate-snippets = { version = "0.6", features = ["ansi_term"] }
55+
annotate-snippets = { version = "0.8", features = ["color"] }
5656
structopt = "0.3"
5757
rustfmt-config_proc_macro = { version = "0.2", path = "config_proc_macro" }
5858
lazy_static = "1.0.0"

Diff for: src/format_report_formatter.rs

+61-85
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
use crate::config::FileName;
21
use crate::formatting::FormattingError;
32
use crate::{ErrorKind, FormatReport};
4-
use annotate_snippets::display_list::DisplayList;
5-
use annotate_snippets::formatter::DisplayListFormatter;
3+
use annotate_snippets::display_list::{DisplayList, FormatOptions};
64
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};
75
use std::fmt::{self, Display};
86

@@ -48,115 +46,93 @@ pub struct FormatReportFormatter<'a> {
4846

4947
impl<'a> Display for FormatReportFormatter<'a> {
5048
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51-
let formatter = DisplayListFormatter::new(self.enable_colors, false);
5249
let errors_by_file = &self.report.internal.borrow().0;
5350

51+
let opt = FormatOptions {
52+
color: self.enable_colors,
53+
..Default::default()
54+
};
55+
5456
for (file, errors) in errors_by_file {
5557
for error in errors {
56-
let snippet = formatting_error_to_snippet(file, error);
57-
writeln!(f, "{}\n", formatter.format(&DisplayList::from(snippet)))?;
58+
let error_kind = error.kind.to_string();
59+
let title = Some(Annotation {
60+
id: if error.is_internal() {
61+
Some("internal")
62+
} else {
63+
None
64+
},
65+
label: Some(&error_kind),
66+
annotation_type: error_kind_to_snippet_annotation_type(&error.kind),
67+
});
68+
69+
let message_suffix = error.msg_suffix();
70+
let footer = if !message_suffix.is_empty() {
71+
Some(Annotation {
72+
id: None,
73+
label: Some(message_suffix),
74+
annotation_type: AnnotationType::Note,
75+
})
76+
} else {
77+
None
78+
};
79+
80+
let origin = format!("{}:{}", file, error.line);
81+
let slice = Slice {
82+
source: &error.line_buffer.clone(),
83+
line_start: error.line,
84+
origin: Some(origin.as_str()),
85+
fold: false,
86+
annotations: slice_annotation(error).into_iter().collect(),
87+
};
88+
89+
let snippet = Snippet {
90+
title,
91+
footer: footer.into_iter().collect(),
92+
slices: vec![slice],
93+
opt,
94+
};
95+
writeln!(f, "{}\n", DisplayList::from(snippet))?;
5896
}
5997
}
6098

6199
if !errors_by_file.is_empty() {
62-
let snippet = formatting_failure_snippet(self.report.warning_count());
63-
writeln!(f, "{}", formatter.format(&DisplayList::from(snippet)))?;
100+
let label = format!(
101+
"rustfmt has failed to format. See previous {} errors.",
102+
self.report.warning_count()
103+
);
104+
let snippet = Snippet {
105+
title: Some(Annotation {
106+
id: None,
107+
label: Some(&label),
108+
annotation_type: AnnotationType::Warning,
109+
}),
110+
footer: Vec::new(),
111+
slices: Vec::new(),
112+
opt,
113+
};
114+
writeln!(f, "{}", DisplayList::from(snippet))?;
64115
}
65116

66117
Ok(())
67118
}
68119
}
69120

70-
fn formatting_failure_snippet(warning_count: usize) -> Snippet {
71-
Snippet {
72-
title: Some(Annotation {
73-
id: None,
74-
label: Some(format!(
75-
"rustfmt has failed to format. See previous {} errors.",
76-
warning_count
77-
)),
78-
annotation_type: AnnotationType::Warning,
79-
}),
80-
footer: Vec::new(),
81-
slices: Vec::new(),
82-
}
83-
}
84-
85-
fn formatting_error_to_snippet(file: &FileName, error: &FormattingError) -> Snippet {
86-
let slices = vec![snippet_code_slice(file, error)];
87-
let title = Some(snippet_title(error));
88-
let footer = snippet_footer(error).into_iter().collect();
89-
90-
Snippet {
91-
title,
92-
footer,
93-
slices,
94-
}
95-
}
96-
97-
fn snippet_title(error: &FormattingError) -> Annotation {
98-
let annotation_type = error_kind_to_snippet_annotation_type(&error.kind);
99-
100-
Annotation {
101-
id: title_annotation_id(error),
102-
label: Some(error.kind.to_string()),
103-
annotation_type,
104-
}
105-
}
106-
107-
fn snippet_footer(error: &FormattingError) -> Option<Annotation> {
108-
let message_suffix = error.msg_suffix();
109-
110-
if !message_suffix.is_empty() {
111-
Some(Annotation {
112-
id: None,
113-
label: Some(message_suffix.to_string()),
114-
annotation_type: AnnotationType::Note,
115-
})
116-
} else {
117-
None
118-
}
119-
}
120-
121-
fn snippet_code_slice(file: &FileName, error: &FormattingError) -> Slice {
122-
let annotations = slice_annotation(error).into_iter().collect();
123-
let origin = Some(format!("{}:{}", file, error.line));
124-
let source = error.line_buffer.clone();
125-
126-
Slice {
127-
source,
128-
line_start: error.line,
129-
origin,
130-
fold: false,
131-
annotations,
132-
}
133-
}
134-
135-
fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation> {
121+
fn slice_annotation(error: &FormattingError) -> Option<SourceAnnotation<'_>> {
136122
let (range_start, range_length) = error.format_len();
137123
let range_end = range_start + range_length;
138124

139125
if range_length > 0 {
140126
Some(SourceAnnotation {
141127
annotation_type: AnnotationType::Error,
142128
range: (range_start, range_end),
143-
label: String::new(),
129+
label: "",
144130
})
145131
} else {
146132
None
147133
}
148134
}
149135

150-
fn title_annotation_id(error: &FormattingError) -> Option<String> {
151-
const INTERNAL_ERROR_ID: &str = "internal";
152-
153-
if error.is_internal() {
154-
Some(INTERNAL_ERROR_ID.to_string())
155-
} else {
156-
None
157-
}
158-
}
159-
160136
fn error_kind_to_snippet_annotation_type(error_kind: &ErrorKind) -> AnnotationType {
161137
match error_kind {
162138
ErrorKind::LineOverflow(..)

0 commit comments

Comments
 (0)