Skip to content

Commit a01a4ba

Browse files
committed
fix sample + cosmetics + one more test
1 parent 4e16cfb commit a01a4ba

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

crates/ide_assists/src/handlers/add_missing_match_arms.rs

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ use crate::{
2121
// enum Action { Move { distance: u32 }, Stop }
2222
//
2323
// fn handle(action: Action) {
24-
// match action $0 {
24+
// match action {
25+
// $0
2526
// }
2627
// }
2728
// ```
@@ -30,7 +31,7 @@ use crate::{
3031
// enum Action { Move { distance: u32 }, Stop }
3132
//
3233
// fn handle(action: Action) {
33-
// match action {
34+
// match action {
3435
// $0Action::Move { distance } => todo!(),
3536
// Action::Stop => todo!(),
3637
// }
@@ -41,7 +42,7 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
4142
let match_arm_list = match_expr.match_arm_list()?;
4243
let target_range : TextRange;
4344

44-
if let None = cursor_inside_simple_match_arm_list(&ctx, &match_expr, &match_arm_list) {
45+
if let None = trivial_match_arm_list_at_cursor(&ctx, &match_expr, &match_arm_list) {
4546
target_range = TextRange::new(
4647
ctx.sema.original_range(match_expr.syntax()).range.start(),
4748
ctx.sema.original_range(match_arm_list.syntax()).range.start(),
@@ -192,12 +193,13 @@ pub(crate) fn add_missing_match_arms(acc: &mut Assists, ctx: &AssistContext) ->
192193
)
193194
}
194195

195-
fn cursor_inside_simple_match_arm_list(ctx: &AssistContext, match_expr : &MatchExpr, match_arm_list : &MatchArmList) -> Option<()> {
196-
println!("---\n{:#?}\n{:#?}\n---", match_expr, match_arm_list);
196+
fn trivial_match_arm_list_at_cursor(ctx: &AssistContext, match_expr : &MatchExpr, match_arm_list : &MatchArmList) -> Option<()> {
197+
// match x { $0 }
197198
if match_arm_list.arms().next() == None {
198199
return Some(());
199200
}
200201

202+
// match { _$0 => {...} }
201203
let wild_pat = ctx.find_node_at_offset_with_descend::<ast::WildcardPat>()?;
202204
let arm = wild_pat.syntax().parent().and_then(ast::MatchArm::cast)?;
203205
let arm_match_expr = arm.syntax().ancestors().nth(2).and_then(ast::MatchExpr::cast)?;
@@ -973,6 +975,26 @@ fn main() {
973975
);
974976
}
975977

978+
979+
#[test]
980+
fn wildcard_inside_expression_not_applicable() {
981+
check_assist_not_applicable(
982+
add_missing_match_arms,
983+
r#"
984+
enum E { X, Y }
985+
986+
fn foo(e : E) {
987+
match e {
988+
_ => {
989+
println!("1");$0
990+
println!("2");
991+
}
992+
}
993+
}
994+
"#,
995+
);
996+
}
997+
976998
#[test]
977999
fn add_missing_match_arms_qualifies_path() {
9781000
check_assist(

0 commit comments

Comments
 (0)