File tree Expand file tree Collapse file tree 1 file changed +54
-2
lines changed Expand file tree Collapse file tree 1 file changed +54
-2
lines changed Original file line number Diff line number Diff line change @@ -8,13 +8,39 @@ use ra_syntax::ast::{self, AstNode};
8
8
9
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
27
pub ( crate ) fn fill_match_arms ( mut ctx : AssistCtx < impl HirDatabase > ) -> Option < Assist > {
12
28
let match_expr = ctx. node_at_offset :: < ast:: MatchExpr > ( ) ?;
13
29
14
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
33
match match_expr. match_arm_list ( ) {
16
- Some ( arm_list) if arm_list. arms ( ) . count ( ) > 0 => {
17
- return None ;
34
+ Some ( arm_list) => {
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
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
}
You can’t perform that action at this time.
0 commit comments