diff options
-rw-r--r-- | crates/ra_assists/src/fill_match_arms.rs | 56 |
1 files changed, 54 insertions, 2 deletions
diff --git a/crates/ra_assists/src/fill_match_arms.rs b/crates/ra_assists/src/fill_match_arms.rs index c7a8bce20..a0e0f110f 100644 --- a/crates/ra_assists/src/fill_match_arms.rs +++ b/crates/ra_assists/src/fill_match_arms.rs | |||
@@ -8,13 +8,39 @@ use ra_syntax::ast::{self, AstNode}; | |||
8 | 8 | ||
9 | use crate::{AssistCtx, Assist, AssistId}; | 9 | use crate::{AssistCtx, Assist, AssistId}; |
10 | 10 | ||
11 | fn is_trivial_arm(arm: &ast::MatchArm) -> bool { | ||
12 | for (i, p) in arm.pats().enumerate() { | ||
13 | if i > 0 { | ||
14 | return false; | ||
15 | } | ||
16 | |||
17 | match p.kind() { | ||
18 | ast::PatKind::PlaceholderPat(_) => {} | ||
19 | _ => { | ||
20 | return false; | ||
21 | } | ||
22 | }; | ||
23 | } | ||
24 | return true; | ||
25 | } | ||
26 | |||
11 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { | 27 | pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> { |
12 | let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; | 28 | let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?; |
13 | 29 | ||
14 | // We already have some match arms, so we don't provide any assists. | 30 | // We already have some match arms, so we don't provide any assists. |
31 | // Unless if there is only one trivial match arm possibly created | ||
32 | // by match postfix complete. Trivial match arm is the catch all arm. | ||
15 | match match_expr.match_arm_list() { | 33 | match match_expr.match_arm_list() { |
16 | Some(arm_list) if arm_list.arms().count() > 0 => { | 34 | Some(arm_list) => { |
17 | return None; | 35 | for (i, a) in arm_list.arms().enumerate() { |
36 | if i > 0 { | ||
37 | return None; | ||
38 | } | ||
39 | |||
40 | if !is_trivial_arm(a) { | ||
41 | return None; | ||
42 | } | ||
43 | } | ||
18 | } | 44 | } |
19 | _ => {} | 45 | _ => {} |
20 | } | 46 | } |
@@ -228,4 +254,30 @@ mod tests { | |||
228 | "match E::X {}", | 254 | "match E::X {}", |
229 | ); | 255 | ); |
230 | } | 256 | } |
257 | |||
258 | #[test] | ||
259 | fn fill_match_arms_trivial_arm() { | ||
260 | check_assist( | ||
261 | fill_match_arms, | ||
262 | r#" | ||
263 | enum E { X, Y } | ||
264 | |||
265 | fn main() { | ||
266 | match E::X { | ||
267 | <|>_ => {}, | ||
268 | } | ||
269 | } | ||
270 | "#, | ||
271 | r#" | ||
272 | enum E { X, Y } | ||
273 | |||
274 | fn main() { | ||
275 | match <|>E::X { | ||
276 | E::X => (), | ||
277 | E::Y => (), | ||
278 | } | ||
279 | } | ||
280 | "#, | ||
281 | ); | ||
282 | } | ||
231 | } | 283 | } |