1
Fork 0

Use more slice patterns inside the compiler

This commit is contained in:
León Orell Valerian Liehr 2024-08-07 12:41:49 +02:00
parent 60d146580c
commit c4c518d2d4
No known key found for this signature in database
GPG key ID: D17A07215F68E713
40 changed files with 191 additions and 221 deletions

View file

@ -231,17 +231,17 @@ pub trait Emitter: Translate {
) {
if let Some((sugg, rest)) = suggestions.split_first() {
let msg = self.translate_message(&sugg.msg, fluent_args).map_err(Report::new).unwrap();
if rest.is_empty() &&
if rest.is_empty()
// ^ if there is only one suggestion
// don't display multi-suggestions as labels
sugg.substitutions.len() == 1 &&
&& let [substitution] = sugg.substitutions.as_slice()
// don't display multipart suggestions as labels
sugg.substitutions[0].parts.len() == 1 &&
&& let [part] = substitution.parts.as_slice()
// don't display long messages as labels
msg.split_whitespace().count() < 10 &&
&& msg.split_whitespace().count() < 10
// don't display multiline suggestions as labels
!sugg.substitutions[0].parts[0].snippet.contains('\n') &&
![
&& !part.snippet.contains('\n')
&& ![
// when this style is set we want the suggestion to be a message, not inline
SuggestionStyle::HideCodeAlways,
// trivial suggestion for tooling's sake, never shown
@ -250,8 +250,8 @@ pub trait Emitter: Translate {
SuggestionStyle::ShowAlways,
].contains(&sugg.style)
{
let substitution = &sugg.substitutions[0].parts[0].snippet.trim();
let msg = if substitution.is_empty() || sugg.style.hide_inline() {
let snippet = part.snippet.trim();
let msg = if snippet.is_empty() || sugg.style.hide_inline() {
// This substitution is only removal OR we explicitly don't want to show the
// code inline (`hide_inline`). Therefore, we don't show the substitution.
format!("help: {msg}")
@ -260,19 +260,18 @@ pub trait Emitter: Translate {
format!(
"help: {}{}: `{}`",
msg,
if self.source_map().is_some_and(|sm| is_case_difference(
sm,
substitution,
sugg.substitutions[0].parts[0].span,
)) {
if self
.source_map()
.is_some_and(|sm| is_case_difference(sm, snippet, part.span,))
{
" (notice the capitalization)"
} else {
""
},
substitution,
snippet,
)
};
primary_span.push_span_label(sugg.substitutions[0].parts[0].span, msg);
primary_span.push_span_label(part.span, msg);
// We return only the modified primary_span
suggestions.clear();

View file

@ -2024,11 +2024,11 @@ pub fn a_or_an(s: &str) -> &'static str {
///
/// Take a list ["a", "b", "c"] and output a display friendly version "a, b and c"
pub fn display_list_with_comma_and<T: std::fmt::Display>(v: &[T]) -> String {
match v.len() {
0 => "".to_string(),
1 => v[0].to_string(),
2 => format!("{} and {}", v[0], v[1]),
_ => format!("{}, {}", v[0], display_list_with_comma_and(&v[1..])),
match v {
[] => "".to_string(),
[a] => a.to_string(),
[a, b] => format!("{a} and {b}"),
[a, v @ ..] => format!("{a}, {}", display_list_with_comma_and(v)),
}
}