|
5 | 5 | //! |
6 | 6 | //! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/ |
7 | 7 |
|
8 | | -use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation}; |
| 8 | +use annotate_snippets::{Renderer, Snippet}; |
9 | 9 | use rustc_data_structures::sync::Lrc; |
10 | 10 | use rustc_error_messages::FluentArgs; |
11 | 11 | use rustc_span::source_map::SourceMap; |
@@ -83,15 +83,17 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String { |
83 | 83 | file.get_line(line.line_index - 1).map(|a| a.to_string()).unwrap_or_default() |
84 | 84 | } |
85 | 85 |
|
86 | | -/// Maps `diagnostic::Level` to `snippet::AnnotationType` |
87 | | -fn annotation_type_for_level(level: Level) -> AnnotationType { |
| 86 | +/// Maps [`crate::Level`] to [`annotate_snippets::Level`] |
| 87 | +fn annotation_level_for_level(level: Level) -> annotate_snippets::Level { |
88 | 88 | match level { |
89 | | - Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => AnnotationType::Error, |
90 | | - Level::ForceWarning(_) | Level::Warning => AnnotationType::Warning, |
91 | | - Level::Note | Level::OnceNote => AnnotationType::Note, |
92 | | - Level::Help | Level::OnceHelp => AnnotationType::Help, |
| 89 | + Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => { |
| 90 | + annotate_snippets::Level::Error |
| 91 | + } |
| 92 | + Level::ForceWarning(_) | Level::Warning => annotate_snippets::Level::Warning, |
| 93 | + Level::Note | Level::OnceNote => annotate_snippets::Level::Note, |
| 94 | + Level::Help | Level::OnceHelp => annotate_snippets::Level::Help, |
93 | 95 | // FIXME(#59346): Not sure how to map this level |
94 | | - Level::FailureNote => AnnotationType::Error, |
| 96 | + Level::FailureNote => annotate_snippets::Level::Error, |
95 | 97 | Level::Allow => panic!("Should not call with Allow"), |
96 | 98 | Level::Expect(_) => panic!("Should not call with Expect"), |
97 | 99 | } |
@@ -180,42 +182,29 @@ impl AnnotateSnippetEmitter { |
180 | 182 | }) |
181 | 183 | .collect(); |
182 | 184 | let code = code.map(|code| code.to_string()); |
183 | | - let snippet = Snippet { |
184 | | - title: Some(Annotation { |
185 | | - label: Some(&message), |
186 | | - id: code.as_deref(), |
187 | | - annotation_type: annotation_type_for_level(*level), |
188 | | - }), |
189 | | - footer: vec![], |
190 | | - slices: annotated_files |
191 | | - .iter() |
192 | | - .map(|(file_name, source, line_index, annotations)| { |
193 | | - Slice { |
194 | | - source, |
195 | | - line_start: *line_index, |
196 | | - origin: Some(file_name), |
197 | | - // FIXME(#59346): Not really sure when `fold` should be true or false |
198 | | - fold: false, |
199 | | - annotations: annotations |
200 | | - .iter() |
201 | | - .map(|annotation| SourceAnnotation { |
202 | | - range: ( |
203 | | - annotation.start_col.display, |
204 | | - annotation.end_col.display, |
205 | | - ), |
206 | | - label: annotation.label.as_deref().unwrap_or_default(), |
207 | | - annotation_type: annotation_type_for_level(*level), |
208 | | - }) |
209 | | - .collect(), |
210 | | - } |
211 | | - }) |
212 | | - .collect(), |
213 | | - }; |
| 185 | + |
| 186 | + let snippets = |
| 187 | + annotated_files.iter().map(|(file_name, source, line_index, annotations)| { |
| 188 | + Snippet::source(source) |
| 189 | + .line_start(*line_index) |
| 190 | + .origin(file_name) |
| 191 | + // FIXME(#59346): Not really sure when `fold` should be true or false |
| 192 | + .fold(false) |
| 193 | + .annotations(annotations.iter().map(|annotation| { |
| 194 | + annotation_level_for_level(*level) |
| 195 | + .span(annotation.start_col.display..annotation.end_col.display) |
| 196 | + .label(annotation.label.as_deref().unwrap_or_default()) |
| 197 | + })) |
| 198 | + }); |
| 199 | + let mut message = annotation_level_for_level(*level).title(&message).snippets(snippets); |
| 200 | + if let Some(code) = code.as_deref() { |
| 201 | + message = message.id(code) |
| 202 | + } |
214 | 203 | // FIXME(#59346): Figure out if we can _always_ print to stderr or not. |
215 | 204 | // `emitter.rs` has the `Destination` enum that lists various possible output |
216 | 205 | // destinations. |
217 | 206 | let renderer = Renderer::plain().anonymized_line_numbers(self.ui_testing); |
218 | | - eprintln!("{}", renderer.render(snippet)) |
| 207 | + eprintln!("{}", renderer.render(message)) |
219 | 208 | } |
220 | 209 | // FIXME(#59346): Is it ok to return None if there's no source_map? |
221 | 210 | } |
|
0 commit comments