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 | 50 |
1 files changed, 28 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..b7e30a7f2 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,35 @@ 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", AssistKind::RefactorRewrite), |
53 | let then_arm = { | 53 | "Replace with match", |
54 | let then_block = then_block.reset_indent().indent(IndentLevel(1)); | 54 | target, |
55 | let then_expr = unwrap_trivial_block(then_block); | 55 | move |edit| { |
56 | make::match_arm(vec![pat.clone()], then_expr) | 56 | let match_expr = { |
57 | let then_arm = { | ||
58 | let then_block = then_block.reset_indent().indent(IndentLevel(1)); | ||
59 | let then_expr = unwrap_trivial_block(then_block); | ||
60 | make::match_arm(vec![pat.clone()], then_expr) | ||
61 | }; | ||
62 | let else_arm = { | ||
63 | let pattern = ctx | ||
64 | .sema | ||
65 | .type_of_pat(&pat) | ||
66 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) | ||
67 | .map(|it| it.sad_pattern()) | ||
68 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
69 | let else_expr = unwrap_trivial_block(else_block); | ||
70 | make::match_arm(vec![pattern], else_expr) | ||
71 | }; | ||
72 | let match_expr = | ||
73 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])); | ||
74 | match_expr.indent(IndentLevel::from_node(if_expr.syntax())) | ||
57 | }; | 75 | }; |
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 | 76 | ||
72 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | 77 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); |
73 | }) | 78 | }, |
79 | ) | ||
74 | } | 80 | } |
75 | 81 | ||
76 | #[cfg(test)] | 82 | #[cfg(test)] |