From 5f8f8a38a251197c8b07e349d9782c3ec5fde383 Mon Sep 17 00:00:00 2001 From: Steffen Lyngbaek Date: Tue, 17 Mar 2020 12:26:55 -0700 Subject: Don't show assist if all arms are present --- crates/ra_assists/src/handlers/fill_match_arms.rs | 70 ++++++++++++----------- 1 file changed, 37 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/fill_match_arms.rs b/crates/ra_assists/src/handlers/fill_match_arms.rs index 6e6c2d5cc..f8859ff6d 100644 --- a/crates/ra_assists/src/handlers/fill_match_arms.rs +++ b/crates/ra_assists/src/handlers/fill_match_arms.rs @@ -50,45 +50,49 @@ pub(crate) fn fill_match_arms(ctx: AssistCtx) -> Option { return None; } - let db = ctx.db; - ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { - let mut arms: Vec = match_arm_list.arms().collect(); - if arms.len() == 1 { - if let Some(Pat::PlaceholderPat(..)) = arms[0].pat() { - arms.clear(); - } + let mut arms: Vec = match_arm_list.arms().collect(); + if arms.len() == 1 { + if let Some(Pat::PlaceholderPat(..)) = arms[0].pat() { + arms.clear(); } + } - let mut has_partial_match = false; - let variants: Vec = variants - .into_iter() - .filter_map(|variant| build_pat(db, module, variant)) - .filter(|variant_pat| { - !arms.iter().filter_map(|arm| arm.pat().map(|_| arm)).any(|arm| { - let pat = arm.pat().unwrap(); - - // Special casee OrPat as separate top-level pats - let pats: Vec = match Pat::from(pat.clone()) { - Pat::OrPat(pats) => pats.pats().collect::>(), - _ => vec![pat], - }; - - pats.iter().any(|pat| { - match does_arm_pat_match_variant(pat, arm.guard(), variant_pat) { - ArmMatch::Yes => true, - ArmMatch::No => false, - ArmMatch::Partial => { - has_partial_match = true; - true - } + let mut has_partial_match = false; + let db = ctx.db; + let missing_arms: Vec = variants + .into_iter() + .filter_map(|variant| build_pat(db, module, variant)) + .filter(|variant_pat| { + !arms.iter().filter_map(|arm| arm.pat().map(|_| arm)).any(|arm| { + let pat = arm.pat().unwrap(); + + // Special casee OrPat as separate top-level pats + let pats: Vec = match Pat::from(pat.clone()) { + Pat::OrPat(pats) => pats.pats().collect::>(), + _ => vec![pat], + }; + + pats.iter().any(|pat| { + match does_arm_pat_match_variant(pat, arm.guard(), variant_pat) { + ArmMatch::Yes => true, + ArmMatch::No => false, + ArmMatch::Partial => { + has_partial_match = true; + true } - }) + } }) }) - .map(|pat| make::match_arm(iter::once(pat), make::expr_unit())) - .collect(); + }) + .map(|pat| make::match_arm(iter::once(pat), make::expr_unit())) + .collect(); - arms.extend(variants); + if missing_arms.is_empty() && !has_partial_match { + return None; + } + + ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { + arms.extend(missing_arms); if has_partial_match { arms.push(make::match_arm( iter::once(make::placeholder_pat().into()), -- cgit v1.2.3