aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--crates/ra_assists/src/handlers/fill_match_arms.rs70
1 files changed, 37 insertions, 33 deletions
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<Assist> {
50 return None; 50 return None;
51 } 51 }
52 52
53 let db = ctx.db; 53 let mut arms: Vec<MatchArm> = match_arm_list.arms().collect();
54 ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| { 54 if arms.len() == 1 {
55 let mut arms: Vec<MatchArm> = match_arm_list.arms().collect(); 55 if let Some(Pat::PlaceholderPat(..)) = arms[0].pat() {
56 if arms.len() == 1 { 56 arms.clear();
57 if let Some(Pat::PlaceholderPat(..)) = arms[0].pat() {
58 arms.clear();
59 }
60 } 57 }
58 }
61 59
62 let mut has_partial_match = false; 60 let mut has_partial_match = false;
63 let variants: Vec<MatchArm> = variants 61 let db = ctx.db;
64 .into_iter() 62 let missing_arms: Vec<MatchArm> = variants
65 .filter_map(|variant| build_pat(db, module, variant)) 63 .into_iter()
66 .filter(|variant_pat| { 64 .filter_map(|variant| build_pat(db, module, variant))
67 !arms.iter().filter_map(|arm| arm.pat().map(|_| arm)).any(|arm| { 65 .filter(|variant_pat| {
68 let pat = arm.pat().unwrap(); 66 !arms.iter().filter_map(|arm| arm.pat().map(|_| arm)).any(|arm| {
69 67 let pat = arm.pat().unwrap();
70 // Special casee OrPat as separate top-level pats 68
71 let pats: Vec<Pat> = match Pat::from(pat.clone()) { 69 // Special casee OrPat as separate top-level pats
72 Pat::OrPat(pats) => pats.pats().collect::<Vec<_>>(), 70 let pats: Vec<Pat> = match Pat::from(pat.clone()) {
73 _ => vec![pat], 71 Pat::OrPat(pats) => pats.pats().collect::<Vec<_>>(),
74 }; 72 _ => vec![pat],
75 73 };
76 pats.iter().any(|pat| { 74
77 match does_arm_pat_match_variant(pat, arm.guard(), variant_pat) { 75 pats.iter().any(|pat| {
78 ArmMatch::Yes => true, 76 match does_arm_pat_match_variant(pat, arm.guard(), variant_pat) {
79 ArmMatch::No => false, 77 ArmMatch::Yes => true,
80 ArmMatch::Partial => { 78 ArmMatch::No => false,
81 has_partial_match = true; 79 ArmMatch::Partial => {
82 true 80 has_partial_match = true;
83 } 81 true
84 } 82 }
85 }) 83 }
86 }) 84 })
87 }) 85 })
88 .map(|pat| make::match_arm(iter::once(pat), make::expr_unit())) 86 })
89 .collect(); 87 .map(|pat| make::match_arm(iter::once(pat), make::expr_unit()))
88 .collect();
90 89
91 arms.extend(variants); 90 if missing_arms.is_empty() && !has_partial_match {
91 return None;
92 }
93
94 ctx.add_assist(AssistId("fill_match_arms"), "Fill match arms", |edit| {
95 arms.extend(missing_arms);
92 if has_partial_match { 96 if has_partial_match {
93 arms.push(make::match_arm( 97 arms.push(make::match_arm(
94 iter::once(make::placeholder_pat().into()), 98 iter::once(make::placeholder_pat().into()),