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 | 58 |
1 files changed, 28 insertions, 30 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 2eb8348f8..65f5fc6ab 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 | |||
@@ -1,10 +1,14 @@ | |||
1 | use ra_fmt::unwrap_trivial_block; | 1 | use ra_fmt::unwrap_trivial_block; |
2 | use ra_syntax::{ | 2 | use ra_syntax::{ |
3 | ast::{self, edit::IndentLevel, make}, | 3 | ast::{ |
4 | self, | ||
5 | edit::{AstNodeEdit, IndentLevel}, | ||
6 | make, | ||
7 | }, | ||
4 | AstNode, | 8 | AstNode, |
5 | }; | 9 | }; |
6 | 10 | ||
7 | use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; | 11 | use crate::{utils::TryEnum, AssistContext, AssistId, Assists}; |
8 | 12 | ||
9 | // Assist: replace_if_let_with_match | 13 | // Assist: replace_if_let_with_match |
10 | // | 14 | // |
@@ -32,7 +36,7 @@ use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; | |||
32 | // } | 36 | // } |
33 | // } | 37 | // } |
34 | // ``` | 38 | // ``` |
35 | pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | 39 | pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { |
36 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; | 40 | let if_expr: ast::IfExpr = ctx.find_node_at_offset()?; |
37 | let cond = if_expr.condition()?; | 41 | let cond = if_expr.condition()?; |
38 | let pat = cond.pat()?; | 42 | let pat = cond.pat()?; |
@@ -43,36 +47,30 @@ pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | |||
43 | ast::ElseBranch::IfExpr(_) => return None, | 47 | ast::ElseBranch::IfExpr(_) => return None, |
44 | }; | 48 | }; |
45 | 49 | ||
46 | let sema = ctx.sema; | ||
47 | let target = if_expr.syntax().text_range(); | 50 | let target = if_expr.syntax().text_range(); |
48 | ctx.add_assist( | 51 | acc.add(AssistId("replace_if_let_with_match"), "Replace with match", target, move |edit| { |
49 | AssistId("replace_if_let_with_match"), | 52 | let match_expr = { |
50 | "Replace with match", | 53 | let then_arm = { |
51 | target, | 54 | let then_expr = unwrap_trivial_block(then_block); |
52 | move |edit| { | 55 | make::match_arm(vec![pat.clone()], then_expr) |
53 | let match_expr = { | ||
54 | let then_arm = { | ||
55 | let then_expr = unwrap_trivial_block(then_block); | ||
56 | make::match_arm(vec![pat.clone()], then_expr) | ||
57 | }; | ||
58 | let else_arm = { | ||
59 | let pattern = sema | ||
60 | .type_of_pat(&pat) | ||
61 | .and_then(|ty| TryEnum::from_ty(sema, &ty)) | ||
62 | .map(|it| it.sad_pattern()) | ||
63 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
64 | let else_expr = unwrap_trivial_block(else_block); | ||
65 | make::match_arm(vec![pattern], else_expr) | ||
66 | }; | ||
67 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) | ||
68 | }; | 56 | }; |
57 | let else_arm = { | ||
58 | let pattern = ctx | ||
59 | .sema | ||
60 | .type_of_pat(&pat) | ||
61 | .and_then(|ty| TryEnum::from_ty(&ctx.sema, &ty)) | ||
62 | .map(|it| it.sad_pattern()) | ||
63 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
64 | let else_expr = unwrap_trivial_block(else_block); | ||
65 | make::match_arm(vec![pattern], else_expr) | ||
66 | }; | ||
67 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) | ||
68 | .indent(IndentLevel::from_node(if_expr.syntax())) | ||
69 | }; | ||
69 | 70 | ||
70 | let match_expr = IndentLevel::from_node(if_expr.syntax()).increase_indent(match_expr); | 71 | edit.set_cursor(if_expr.syntax().text_range().start()); |
71 | 72 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | |
72 | edit.set_cursor(if_expr.syntax().text_range().start()); | 73 | }) |
73 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | ||
74 | }, | ||
75 | ) | ||
76 | } | 74 | } |
77 | 75 | ||
78 | #[cfg(test)] | 76 | #[cfg(test)] |