diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2020-05-06 11:52:57 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2020-05-06 11:52:57 +0100 |
commit | 4de3c3eedc1f6576d59e00f0ddde3c70c97be066 (patch) | |
tree | 7922dca3f7c4133e2616257be537428337e479d6 /crates/ra_assists/src/handlers/replace_if_let_with_match.rs | |
parent | ede8906844e206f252810d58533538cf1fb326d4 (diff) | |
parent | 233f01c9ba555e5d06f336cb0ff64e7a83e4a23a (diff) |
Merge #4341
4341: Move target to AssistLabel r=matklad a=matklad
Target is used for assists sorting, so we need it before we compute
the action.
bors r+
🤖
Co-authored-by: Aleksey Kladov <[email protected]>
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 | 47 |
1 files changed, 26 insertions, 21 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 d0df3b84e..2eb8348f8 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 | |||
@@ -44,30 +44,35 @@ pub(crate) fn replace_if_let_with_match(ctx: AssistCtx) -> Option<Assist> { | |||
44 | }; | 44 | }; |
45 | 45 | ||
46 | let sema = ctx.sema; | 46 | let sema = ctx.sema; |
47 | ctx.add_assist(AssistId("replace_if_let_with_match"), "Replace with match", move |edit| { | 47 | let target = if_expr.syntax().text_range(); |
48 | let match_expr = { | 48 | ctx.add_assist( |
49 | let then_arm = { | 49 | AssistId("replace_if_let_with_match"), |
50 | let then_expr = unwrap_trivial_block(then_block); | 50 | "Replace with match", |
51 | make::match_arm(vec![pat.clone()], then_expr) | 51 | target, |
52 | move |edit| { | ||
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])) | ||
52 | }; | 68 | }; |
53 | let else_arm = { | ||
54 | let pattern = sema | ||
55 | .type_of_pat(&pat) | ||
56 | .and_then(|ty| TryEnum::from_ty(sema, &ty)) | ||
57 | .map(|it| it.sad_pattern()) | ||
58 | .unwrap_or_else(|| make::placeholder_pat().into()); | ||
59 | let else_expr = unwrap_trivial_block(else_block); | ||
60 | make::match_arm(vec![pattern], else_expr) | ||
61 | }; | ||
62 | make::expr_match(expr, make::match_arm_list(vec![then_arm, else_arm])) | ||
63 | }; | ||
64 | 69 | ||
65 | let match_expr = IndentLevel::from_node(if_expr.syntax()).increase_indent(match_expr); | 70 | let match_expr = IndentLevel::from_node(if_expr.syntax()).increase_indent(match_expr); |
66 | 71 | ||
67 | edit.target(if_expr.syntax().text_range()); | 72 | edit.set_cursor(if_expr.syntax().text_range().start()); |
68 | edit.set_cursor(if_expr.syntax().text_range().start()); | 73 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); |
69 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | 74 | }, |
70 | }) | 75 | ) |
71 | } | 76 | } |
72 | 77 | ||
73 | #[cfg(test)] | 78 | #[cfg(test)] |