This commit is contained in:
Changyu Li 2019-06-25 13:14:54 -07:00
parent 888157b52e
commit 3a2a13756f

View file

@ -1,4 +1,5 @@
use std::fmt::Write; use std::fmt::Write;
use itertools::Itertools;
use hir::{ use hir::{
AdtDef, FieldSource, HasSource, AdtDef, FieldSource, HasSource,
@ -9,19 +10,14 @@ use ra_syntax::ast::{self, AstNode};
use crate::{AssistCtx, Assist, AssistId}; use crate::{AssistCtx, Assist, AssistId};
fn is_trivial_arm(arm: &ast::MatchArm) -> bool { fn is_trivial_arm(arm: &ast::MatchArm) -> bool {
for (i, p) in arm.pats().enumerate() { fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> {
if i > 0 { let (pat,) = arm.pats().collect_tuple()?;
return false; Some(pat.kind())
} }
match single_pattern(arm) {
match p.kind() { Some(ast::PatKind::PlaceholderPat(..)) => true,
ast::PatKind::PlaceholderPat(_) => {} _ => false,
_ => {
return false;
}
};
} }
return true;
} }
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
@ -32,12 +28,19 @@ pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<As
// by match postfix complete. Trivial match arm is the catch all arm. // by match postfix complete. Trivial match arm is the catch all arm.
match match_expr.match_arm_list() { match match_expr.match_arm_list() {
Some(arm_list) => { Some(arm_list) => {
for (i, a) in arm_list.arms().enumerate() { let mut arm_iter = arm_list.arms();
if i > 0 { let first = arm_iter.next();
return None;
}
if !is_trivial_arm(a) { match first {
// If there arm list is empty or there is only one trivial arm, then proceed.
Some(arm) if is_trivial_arm(arm) => {
if arm_iter.next() != None {
return None;
}
}
None => {}
_ => {
return None; return None;
} }
} }