This commit is contained in:
Lukas Wirth 2021-07-01 21:10:45 +02:00
parent 1b9b2d1f40
commit 3d2490ca97

View File

@ -10,7 +10,7 @@ use crate::{AssistContext, AssistId, AssistKind, Assists, TextRange};
// Assist: merge_match_arms
//
// Merges identical match arms.
// Merges the current match arm with the following if their bodies are identical.
//
// ```
// enum Action { Move { distance: u32 }, Stop }
@ -44,14 +44,11 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
// We check if the following match arms match this one. We could, but don't,
// compare to the previous match arm as well.
let arms_to_merge = successors(Some(current_arm), |it| neighbor(it, Direction::Next))
.take_while(|arm| {
if arm.guard().is_some() {
return false;
}
match arm.expr() {
Some(expr) => expr.syntax().text() == current_expr.syntax().text(),
None => false,
.take_while(|arm| match arm.expr() {
Some(expr) if arm.guard().is_none() => {
expr.syntax().text() == current_expr.syntax().text()
}
_ => false,
})
.collect::<Vec<_>>();
@ -77,10 +74,12 @@ pub(crate) fn merge_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option
let arm = format!("{} => {}", pats, current_expr.syntax().text());
let start = arms_to_merge.first().unwrap().syntax().text_range().start();
let end = arms_to_merge.last().unwrap().syntax().text_range().end();
if let [first, .., last] = &*arms_to_merge {
let start = first.syntax().text_range().start();
let end = last.syntax().text_range().end();
edit.replace(TextRange::new(start, end), arm);
}
},
)
}