Skip to content

Commit 0129790

Browse files
bors[bot]ironymanyourmsftacct
committed
Merge #1432
1432: Make fill_match_arm work with trivial arm r=matklad a=ironyman Addresses this issue #1399 One minor issue I noticed is that complete_postfix creates an arm like this ``` match E::X { <|>_ => {}, } ``` but fill_match_arms creates arms like this ``` E::X => (), ``` Co-authored-by: ironyman <[email protected]> Co-authored-by: Changyu Li <[email protected]>
2 parents 4b0c37b + 3a2a137 commit 0129790

File tree

1 file changed

+57
-2
lines changed

1 file changed

+57
-2
lines changed

crates/ra_assists/src/fill_match_arms.rs

Lines changed: 57 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use std::fmt::Write;
2+
use itertools::Itertools;
23

34
use hir::{
45
AdtDef, FieldSource, HasSource,
@@ -8,13 +9,41 @@ use ra_syntax::ast::{self, AstNode};
89

910
use crate::{AssistCtx, Assist, AssistId};
1011

12+
fn is_trivial_arm(arm: &ast::MatchArm) -> bool {
13+
fn single_pattern(arm: &ast::MatchArm) -> Option<ast::PatKind> {
14+
let (pat,) = arm.pats().collect_tuple()?;
15+
Some(pat.kind())
16+
}
17+
match single_pattern(arm) {
18+
Some(ast::PatKind::PlaceholderPat(..)) => true,
19+
_ => false,
20+
}
21+
}
22+
1123
pub(crate) fn fill_match_arms(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
1224
let match_expr = ctx.node_at_offset::<ast::MatchExpr>()?;
1325

1426
// We already have some match arms, so we don't provide any assists.
27+
// Unless if there is only one trivial match arm possibly created
28+
// by match postfix complete. Trivial match arm is the catch all arm.
1529
match match_expr.match_arm_list() {
16-
Some(arm_list) if arm_list.arms().count() > 0 => {
17-
return None;
30+
Some(arm_list) => {
31+
let mut arm_iter = arm_list.arms();
32+
let first = arm_iter.next();
33+
34+
match first {
35+
// If there arm list is empty or there is only one trivial arm, then proceed.
36+
Some(arm) if is_trivial_arm(arm) => {
37+
if arm_iter.next() != None {
38+
return None;
39+
}
40+
}
41+
None => {}
42+
43+
_ => {
44+
return None;
45+
}
46+
}
1847
}
1948
_ => {}
2049
}
@@ -228,4 +257,30 @@ mod tests {
228257
"match E::X {}",
229258
);
230259
}
260+
261+
#[test]
262+
fn fill_match_arms_trivial_arm() {
263+
check_assist(
264+
fill_match_arms,
265+
r#"
266+
enum E { X, Y }
267+
268+
fn main() {
269+
match E::X {
270+
<|>_ => {},
271+
}
272+
}
273+
"#,
274+
r#"
275+
enum E { X, Y }
276+
277+
fn main() {
278+
match <|>E::X {
279+
E::X => (),
280+
E::Y => (),
281+
}
282+
}
283+
"#,
284+
);
285+
}
231286
}

0 commit comments

Comments
 (0)