Skip to content

Commit 888157b

Browse files
committed
fill_match_arm works with trivial arm
1 parent 7d79be3 commit 888157b

File tree

1 file changed

+54
-2
lines changed

1 file changed

+54
-2
lines changed

crates/ra_assists/src/fill_match_arms.rs

Lines changed: 54 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,39 @@ use ra_syntax::ast::{self, AstNode};
88

99
use crate::{AssistCtx, Assist, AssistId};
1010

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+
1127
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
1228
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
1329

1430
// 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.
1533
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+
}
1844
}
1945
_ => {}
2046
}
@@ -228,4 +254,30 @@ mod tests {
228254
"match E::X {}",
229255
);
230256
}
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+
}
231283
}

0 commit comments

Comments
 (0)