diff options
Diffstat (limited to 'crates/ra_assists/src/handlers/replace_if_let_with_match.rs')
-rw-r--r-- | crates/ra_assists/src/handlers/replace_if_let_with_match.rs | 51 |
1 files changed, 29 insertions, 22 deletions
diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index dfcd787de..9ef121a4c 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs | |||
@@ -8,7 +8,7 @@ use ra_syntax::{ | |||
8 | AstNode, | 8 | AstNode, |
9 | }; | 9 | }; |
10 | 10 | ||
11 | use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; | 11 | use crate::{utils::TryEnum, AssistContext, AssistId, AssistKind, Assists}; |
12 | 12 | ||
13 | // Assist: replace_if_let_with_match | 13 | // Assist: replace_if_let_with_match |
14 | // | 14 | // |
@@ -48,29 +48,36 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
48 | }; | 48 | }; |
49 | 49 | ||
50 | let target = if_expr.syntax().text_range(); | 50 | let target = if_expr.syntax().text_range(); |
51 | acc.add(AssistId("replace_if_let_with_match"), "Replace with match", target, move |edit| { | 51 | acc.add( |
52 | let match_expr = { | 52 | AssistId("replace_if_let_with_match"), |
53 | let then_arm = { | 53 | AssistKind::RefactorRewrite, |
54 | let then_block = then_block.reset_indent().indent(IndentLevel(1)); | 54 | "Replace with match", |
55 | let then_expr = unwrap_trivial_block(then_block); | 55 | target, |
56 | make::match_arm(vec![pat.clone()], then_expr) | 56 | move |edit| { |
57 | let match_expr = { | ||
58 | let then_arm = { | ||
59 | let then_block = then_block.reset_indent().indent(IndentLevel(1)); | ||
60 | let then_expr = unwrap_trivial_block(then_block); | ||
61 | make::match_arm(vec![pat.clone()], then_expr) | ||
62 | }; | ||
63 | let else_arm = { | ||
64 | let pattern = ctx | ||
65 | .sema | ||
66 | .type_of_pat(&pat) | ||
67 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) | ||
68 | .map(|it| it.sad_pattern()) | ||
69 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
70 | let else_expr = unwrap_trivial_block(else_block); | ||
71 | make::match_arm(vec![pattern], else_expr) | ||
72 | }; | ||
73 | let match_expr = | ||
74 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])); | ||
75 | match_expr.indent(IndentLevel::from_node(if_expr.syntax())) | ||
57 | }; | 76 | }; |
58 | let else_arm = { | ||
59 | let pattern = ctx | ||
60 | .sema | ||
61 | .type_of_pat(&pat) | ||
62 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) | ||
63 | .map(|it| it.sad_pattern()) | ||
64 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
65 | let else_expr = unwrap_trivial_block(else_block); | ||
66 | make::match_arm(vec![pattern], else_expr) | ||
67 | }; | ||
68 | let match_expr = make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])); | ||
69 | match_expr.indent(IndentLevel::from_node(if_expr.syntax())) | ||
70 | }; | ||
71 | 77 | ||
72 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | 78 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); |
73 | }) | 79 | }, |
80 | ) | ||
74 | } | 81 | } |
75 | 82 | ||
76 | #[cfg(test)] | 83 | #[cfg(test)] |