Update annotate-snippets to 0.11

This commit is contained in:
Xiretza 2024-08-19 20:20:48 +00:00
parent 4fe1e2bd5b
commit c864238baf
5 changed files with 42 additions and 75 deletions

View File

@ -94,16 +94,6 @@ dependencies = [
"yansi-term", "yansi-term",
] ]
[[package]]
name = "annotate-snippets"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d9b665789884a7e8fb06c84b295e923b03ca51edbb7d08f91a6a50322ecbfe6"
dependencies = [
"anstyle",
"unicode-width",
]
[[package]] [[package]]
name = "annotate-snippets" name = "annotate-snippets"
version = "0.11.4" version = "0.11.4"
@ -3642,7 +3632,7 @@ dependencies = [
name = "rustc_errors" name = "rustc_errors"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"annotate-snippets 0.10.2", "annotate-snippets 0.11.4",
"derive_setters", "derive_setters",
"rustc_ast", "rustc_ast",
"rustc_ast_pretty", "rustc_ast_pretty",
@ -3702,7 +3692,7 @@ dependencies = [
name = "rustc_fluent_macro" name = "rustc_fluent_macro"
version = "0.0.0" version = "0.0.0"
dependencies = [ dependencies = [
"annotate-snippets 0.10.2", "annotate-snippets 0.11.4",
"fluent-bundle", "fluent-bundle",
"fluent-syntax", "fluent-syntax",
"proc-macro2", "proc-macro2",

View File

@ -5,7 +5,7 @@ edition = "2021"
[dependencies] [dependencies]
# tidy-alphabetical-start # tidy-alphabetical-start
annotate-snippets = "0.10" annotate-snippets = "0.11"
derive_setters = "0.1.6" derive_setters = "0.1.6"
rustc_ast = { path = "../rustc_ast" } rustc_ast = { path = "../rustc_ast" }
rustc_ast_pretty = { path = "../rustc_ast_pretty" } rustc_ast_pretty = { path = "../rustc_ast_pretty" }

View File

@ -5,7 +5,7 @@
//! //!
//! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/ //! [annotate_snippets]: https://docs.rs/crate/annotate-snippets/
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation}; use annotate_snippets::{Renderer, Snippet};
use rustc_data_structures::sync::Lrc; use rustc_data_structures::sync::Lrc;
use rustc_error_messages::FluentArgs; use rustc_error_messages::FluentArgs;
use rustc_span::source_map::SourceMap; use rustc_span::source_map::SourceMap;
@ -83,15 +83,17 @@ fn source_string(file: Lrc<SourceFile>, line: &Line) -> String {
file.get_line(line.line_index - 1).map(|a| a.to_string()).unwrap_or_default() file.get_line(line.line_index - 1).map(|a| a.to_string()).unwrap_or_default()
} }
/// Maps `diagnostic::Level` to `snippet::AnnotationType` /// Maps [`crate::Level`] to [`annotate_snippets::Level`]
fn annotation_type_for_level(level: Level) -> AnnotationType { fn annotation_level_for_level(level: Level) -> annotate_snippets::Level {
match level { match level {
Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => AnnotationType::Error, Level::Bug | Level::Fatal | Level::Error | Level::DelayedBug => {
Level::ForceWarning(_) | Level::Warning => AnnotationType::Warning, annotate_snippets::Level::Error
Level::Note | Level::OnceNote => AnnotationType::Note, }
Level::Help | Level::OnceHelp => AnnotationType::Help, Level::ForceWarning(_) | Level::Warning => annotate_snippets::Level::Warning,
Level::Note | Level::OnceNote => annotate_snippets::Level::Note,
Level::Help | Level::OnceHelp => annotate_snippets::Level::Help,
// FIXME(#59346): Not sure how to map this level // FIXME(#59346): Not sure how to map this level
Level::FailureNote => AnnotationType::Error, Level::FailureNote => annotate_snippets::Level::Error,
Level::Allow => panic!("Should not call with Allow"), Level::Allow => panic!("Should not call with Allow"),
Level::Expect(_) => panic!("Should not call with Expect"), Level::Expect(_) => panic!("Should not call with Expect"),
} }
@ -180,42 +182,29 @@ impl AnnotateSnippetEmitter {
}) })
.collect(); .collect();
let code = code.map(|code| code.to_string()); let code = code.map(|code| code.to_string());
let snippet = Snippet {
title: Some(Annotation { let snippets =
label: Some(&message), annotated_files.iter().map(|(file_name, source, line_index, annotations)| {
id: code.as_deref(), Snippet::source(source)
annotation_type: annotation_type_for_level(*level), .line_start(*line_index)
}), .origin(file_name)
footer: vec![], // FIXME(#59346): Not really sure when `fold` should be true or false
slices: annotated_files .fold(false)
.iter() .annotations(annotations.iter().map(|annotation| {
.map(|(file_name, source, line_index, annotations)| { annotation_level_for_level(*level)
Slice { .span(annotation.start_col.display..annotation.end_col.display)
source, .label(annotation.label.as_deref().unwrap_or_default())
line_start: *line_index, }))
origin: Some(file_name), });
// FIXME(#59346): Not really sure when `fold` should be true or false let mut message = annotation_level_for_level(*level).title(&message).snippets(snippets);
fold: false, if let Some(code) = code.as_deref() {
annotations: annotations message = message.id(code)
.iter() }
.map(|annotation| SourceAnnotation {
range: (
annotation.start_col.display,
annotation.end_col.display,
),
label: annotation.label.as_deref().unwrap_or_default(),
annotation_type: annotation_type_for_level(*level),
})
.collect(),
}
})
.collect(),
};
// FIXME(#59346): Figure out if we can _always_ print to stderr or not. // FIXME(#59346): Figure out if we can _always_ print to stderr or not.
// `emitter.rs` has the `Destination` enum that lists various possible output // `emitter.rs` has the `Destination` enum that lists various possible output
// destinations. // destinations.
let renderer = Renderer::plain().anonymized_line_numbers(self.ui_testing); let renderer = Renderer::plain().anonymized_line_numbers(self.ui_testing);
eprintln!("{}", renderer.render(snippet)) eprintln!("{}", renderer.render(message))
} }
// FIXME(#59346): Is it ok to return None if there's no source_map? // FIXME(#59346): Is it ok to return None if there's no source_map?
} }

View File

@ -8,7 +8,7 @@ proc-macro = true
[dependencies] [dependencies]
# tidy-alphabetical-start # tidy-alphabetical-start
annotate-snippets = "0.10" annotate-snippets = "0.11"
fluent-bundle = "0.15.2" fluent-bundle = "0.15.2"
fluent-syntax = "0.11" fluent-syntax = "0.11"
proc-macro2 = "1" proc-macro2 = "1"

View File

@ -2,7 +2,7 @@ use std::collections::{HashMap, HashSet};
use std::fs::read_to_string; use std::fs::read_to_string;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
use annotate_snippets::{Annotation, AnnotationType, Renderer, Slice, Snippet, SourceAnnotation}; use annotate_snippets::{Renderer, Snippet};
use fluent_bundle::{FluentBundle, FluentError, FluentResource}; use fluent_bundle::{FluentBundle, FluentError, FluentResource};
use fluent_syntax::ast::{ use fluent_syntax::ast::{
Attribute, Entry, Expression, Identifier, InlineExpression, Message, Pattern, PatternElement, Attribute, Entry, Expression, Identifier, InlineExpression, Message, Pattern, PatternElement,
@ -154,27 +154,15 @@ pub(crate) fn fluent_messages(input: proc_macro::TokenStream) -> proc_macro::Tok
.unwrap() .unwrap()
.0; .0;
let snippet = Snippet { let message = annotate_snippets::Level::Error.title(&err).snippet(
title: Some(Annotation { Snippet::source(this.source())
label: Some(&err), .line_start(line_start)
id: None, .origin(&relative_ftl_path)
annotation_type: AnnotationType::Error, .fold(true)
}), .annotation(annotate_snippets::Level::Error.span(pos.start..pos.end - 1)),
footer: vec![], );
slices: vec![Slice {
source: this.source(),
line_start,
origin: Some(&relative_ftl_path),
fold: true,
annotations: vec![SourceAnnotation {
label: "",
annotation_type: AnnotationType::Error,
range: (pos.start, pos.end - 1),
}],
}],
};
let renderer = Renderer::plain(); let renderer = Renderer::plain();
eprintln!("{}\n", renderer.render(snippet)); eprintln!("{}\n", renderer.render(message));
} }
return failed(&crate_name); return failed(&crate_name);