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 | 81 |
1 files changed, 60 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 e016f51c3..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,28 +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_expr = unwrap_trivial_block(then_block); | 54 | target, |
55 | make::match_arm(vec![pat.clone()], then_expr) | 55 | move |edit| { |
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())) | ||
56 | }; | 75 | }; |
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 | }; | ||
70 | 76 | ||
71 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); | 77 | edit.replace_ast::<ast::Expr>(if_expr.into(), match_expr); |
72 | }) | 78 | }, |
79 | ) | ||
73 | } | 80 | } |
74 | 81 | ||
75 | #[cfg(test)] | 82 | #[cfg(test)] |
@@ -213,4 +220,36 @@ fn foo(x: Result<i32, ()>) { | |||
213 | "#, | 220 | "#, |
214 | ); | 221 | ); |
215 | } | 222 | } |
223 | |||
224 | #[test] | ||
225 | fn nested_indent() { | ||
226 | check_assist( | ||
227 | replace_if_let_with_match, | ||
228 | r#" | ||
229 | fn main() { | ||
230 | if true { | ||
231 | <|>if let Ok(rel_path) = path.strip_prefix(root_path) { | ||
232 | let rel_path = RelativePathBuf::from_path(rel_path).ok()?; | ||
233 | Some((*id, rel_path)) | ||
234 | } else { | ||
235 | None | ||
236 | } | ||
237 | } | ||
238 | } | ||
239 | "#, | ||
240 | r#" | ||
241 | fn main() { | ||
242 | if true { | ||
243 | match path.strip_prefix(root_path) { | ||
244 | Ok(rel_path) => { | ||
245 | let rel_path = RelativePathBuf::from_path(rel_path).ok()?; | ||
246 | Some((*id, rel_path)) | ||
247 | } | ||
248 | _ => None, | ||
249 | } | ||
250 | } | ||
251 | } | ||
252 | "#, | ||
253 | ) | ||
254 | } | ||
216 | } | 255 | } |