aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_assists/src/handlers/replace_let_with_if_let.rs
diff options
context:
space:
mode:
authorBenjamin Coenen <[email protected]>2020-05-10 11:31:55 +0100
committerBenjamin Coenen <[email protected]>2020-05-10 11:31:55 +0100
commite80903a96564c2239489a8c630a4748bf21a3659 (patch)
tree12b31a1fd12deb2120065cea5a558425c8c1984f /crates/ra_assists/src/handlers/replace_let_with_if_let.rs
parent6203e9c4faee288f16d93dbb7dd0f1f8df487d83 (diff)
parent4578154b608fa075595103d0c933da60d55b25c8 (diff)
Merge branch 'master' of github.com:rust-analyzer/rust-analyzer into feat_4348
Diffstat (limited to 'crates/ra_assists/src/handlers/replace_let_with_if_let.rs')
-rw-r--r--crates/ra_assists/src/handlers/replace_let_with_if_let.rs15
1 files changed, 5 insertions, 10 deletions
diff --git a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs
index a5509a567..482957dc6 100644
--- a/crates/ra_assists/src/handlers/replace_let_with_if_let.rs
+++ b/crates/ra_assists/src/handlers/replace_let_with_if_let.rs
@@ -9,11 +9,7 @@ use ra_syntax::{
9 AstNode, T, 9 AstNode, T,
10}; 10};
11 11
12use crate::{ 12use crate::{utils::TryEnum, AssistContext, AssistId, Assists};
13 assist_ctx::{Assist, AssistCtx},
14 utils::TryEnum,
15 AssistId,
16};
17 13
18// Assist: replace_let_with_if_let 14// Assist: replace_let_with_if_let
19// 15//
@@ -39,16 +35,16 @@ use crate::{
39// 35//
40// fn compute() -> Option<i32> { None } 36// fn compute() -> Option<i32> { None }
41// ``` 37// ```
42pub(crate) fn replace_let_with_if_let(ctx: AssistCtx) -> Option<Assist> { 38pub(crate) fn replace_let_with_if_let(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
43 let let_kw = ctx.find_token_at_offset(T![let])?; 39 let let_kw = ctx.find_token_at_offset(T![let])?;
44 let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?; 40 let let_stmt = let_kw.ancestors().find_map(ast::LetStmt::cast)?;
45 let init = let_stmt.initializer()?; 41 let init = let_stmt.initializer()?;
46 let original_pat = let_stmt.pat()?; 42 let original_pat = let_stmt.pat()?;
47 let ty = ctx.sema.type_of_expr(&init)?; 43 let ty = ctx.sema.type_of_expr(&init)?;
48 let happy_variant = TryEnum::from_ty(ctx.sema, &ty).map(|it| it.happy_case()); 44 let happy_variant = TryEnum::from_ty(&ctx.sema, &ty).map(|it| it.happy_case());
49 45
50 let target = let_kw.text_range(); 46 let target = let_kw.text_range();
51 ctx.add_assist(AssistId("replace_let_with_if_let"), "Replace with if-let", target, |edit| { 47 acc.add(AssistId("replace_let_with_if_let"), "Replace with if-let", target, |edit| {
52 let with_placeholder: ast::Pat = match happy_variant { 48 let with_placeholder: ast::Pat = match happy_variant {
53 None => make::placeholder_pat().into(), 49 None => make::placeholder_pat().into(),
54 Some(var_name) => make::tuple_struct_pat( 50 Some(var_name) => make::tuple_struct_pat(
@@ -57,8 +53,7 @@ pub(crate) fn replace_let_with_if_let(ctx: AssistCtx) -> Option<Assist> {
57 ) 53 )
58 .into(), 54 .into(),
59 }; 55 };
60 let block = 56 let block = make::block_expr(None, None).indent(IndentLevel::from_node(let_stmt.syntax()));
61 IndentLevel::from_node(let_stmt.syntax()).increase_indent(make::block_expr(None, None));
62 let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block); 57 let if_ = make::expr_if(make::condition(init, Some(with_placeholder)), block);
63 let stmt = make::expr_stmt(if_); 58 let stmt = make::expr_stmt(if_);
64 59