aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_assists/src/handlers/replace_unwrap_with_match.rs')
-rw-r--r--crates/ra_assists/src/handlers/replace_unwrap_with_match.rs41
1 files changed, 17 insertions, 24 deletions
diff --git a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs
index c6b73da67..a46998b8e 100644
--- a/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs
+++ b/crates/ra_assists/src/handlers/replace_unwrap_with_match.rs
@@ -5,7 +5,7 @@ use ra_syntax::{
5 AstNode, 5 AstNode,
6}; 6};
7 7
8use crate::{utils::TryEnum, Assist, AssistCtx, AssistId}; 8use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
9 9
10// Assist: replace_unwrap_with_match 10// Assist: replace_unwrap_with_match
11// 11//
@@ -29,7 +29,7 @@ use crate::{utils::TryEnum, Assist, AssistCtx, AssistId};
29// }; 29// };
30// } 30// }
31// ``` 31// ```
32pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option<Assist> { 32pub(crate) fn replace_unwrap_with_match(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
33 let method_call: ast::MethodCallExpr = ctx.find_node_at_offset()?; 33 let method_call: ast::MethodCallExpr = ctx.find_node_at_offset()?;
34 let name = method_call.name_ref()?; 34 let name = method_call.name_ref()?;
35 if name.text() != "unwrap" { 35 if name.text() != "unwrap" {
@@ -37,33 +37,26 @@ pub(crate) fn replace_unwrap_with_match(ctx: AssistCtx) -> Option<Assist> {
37 } 37 }
38 let caller = method_call.expr()?; 38 let caller = method_call.expr()?;
39 let ty = ctx.sema.type_of_expr(&caller)?; 39 let ty = ctx.sema.type_of_expr(&caller)?;
40 let happy_variant = TryEnum::from_ty(ctx.sema, &ty)?.happy_case(); 40 let happy_variant = TryEnum::from_ty(&ctx.sema, &ty)?.happy_case();
41 let target = method_call.syntax().text_range(); 41 let target = method_call.syntax().text_range();
42 ctx.add_assist( 42 acc.add(AssistId("replace_unwrap_with_match"), "Replace unwrap with match", target, |edit| {
43 AssistId("replace_unwrap_with_match"), 43 let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
44 "Replace unwrap with match", 44 let it = make::bind_pat(make::name("a")).into();
45 target, 45 let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
46 |edit| {
47 let ok_path = make::path_unqualified(make::path_segment(make::name_ref(happy_variant)));
48 let it = make::bind_pat(make::name("a")).into();
49 let ok_tuple = make::tuple_struct_pat(ok_path, iter::once(it)).into();
50 46
51 let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a"))); 47 let bind_path = make::path_unqualified(make::path_segment(make::name_ref("a")));
52 let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path)); 48 let ok_arm = make::match_arm(iter::once(ok_tuple), make::expr_path(bind_path));
53 49
54 let unreachable_call = make::unreachable_macro_call().into(); 50 let unreachable_call = make::unreachable_macro_call().into();
55 let err_arm = 51 let err_arm = make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call);
56 make::match_arm(iter::once(make::placeholder_pat().into()), unreachable_call);
57 52
58 let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]); 53 let match_arm_list = make::match_arm_list(vec![ok_arm, err_arm]);
59 let match_expr = make::expr_match(caller.clone(), match_arm_list); 54 let match_expr = make::expr_match(caller.clone(), match_arm_list);
60 let match_expr = 55 let match_expr = IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr);
61 IndentLevel::from_node(method_call.syntax()).increase_indent(match_expr);
62 56
63 edit.set_cursor(caller.syntax().text_range().start()); 57 edit.set_cursor(caller.syntax().text_range().start());
64 edit.replace_ast::<ast::Expr>(method_call.into(), match_expr); 58 edit.replace_ast::<ast::Expr>(method_call.into(), match_expr);
65 }, 59 })
66 )
67} 60}
68 61
69#[cfg(test)] 62#[cfg(test)]