diff options
author | Aleksey Kladov <[email protected]> | 2020-05-04 13:16:10 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-05-04 13:16:10 +0100 |
commit | 802617ccc25aeea82eb417b9329812c795311a73 (patch) | |
tree | 08b3d3bbfde6cf76edfe9d77ad13f67ee71652c7 /crates/ra_assists/src/handlers | |
parent | 96f581079ff1e2adf5758e77edc0a4c039e52df8 (diff) |
Simplify
Diffstat (limited to 'crates/ra_assists/src/handlers')
-rw-r--r-- | crates/ra_assists/src/handlers/unwrap_block.rs | 26 |
1 files changed, 13 insertions, 13 deletions
diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 58649c47e..b407eb870 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs | |||
@@ -24,38 +24,38 @@ use ra_syntax::{ast, AstNode, TextRange, T}; | |||
24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { | 24 | pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option<Assist> { |
25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; | 25 | let l_curly_token = ctx.find_token_at_offset(T!['{'])?; |
26 | 26 | ||
27 | let res = if let Some(if_expr) = l_curly_token.ancestors().find_map(IfExpr::cast) { | 27 | let (expr, expr_to_unwrap) = if let Some(if_expr) = |
28 | l_curly_token.ancestors().find_map(IfExpr::cast) | ||
29 | { | ||
28 | // if expression | 30 | // if expression |
29 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); | 31 | let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); |
30 | let expr_to_unwrap = expr_to_unwrap?; | 32 | let expr_to_unwrap = expr_to_unwrap?; |
31 | // Find if we are in a else if block | 33 | // Find if we are in a else if block |
32 | let ancestor = if_expr.syntax().ancestors().skip(1).find_map(ast::IfExpr::cast); | 34 | let ancestor = if_expr.syntax().ancestors().skip(1).find_map(ast::IfExpr::cast); |
33 | 35 | ||
34 | if let Some(ancestor) = ancestor { | 36 | match ancestor { |
35 | Some((ast::Expr::IfExpr(ancestor), expr_to_unwrap)) | 37 | None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), |
36 | } else { | 38 | Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), |
37 | Some((ast::Expr::IfExpr(if_expr), expr_to_unwrap)) | ||
38 | } | 39 | } |
39 | } else if let Some(for_expr) = l_curly_token.ancestors().find_map(ForExpr::cast) { | 40 | } else if let Some(for_expr) = l_curly_token.ancestors().find_map(ForExpr::cast) { |
40 | // for expression | 41 | // for expression |
41 | let block_expr = for_expr.loop_body()?; | 42 | let block_expr = for_expr.loop_body()?; |
42 | extract_expr(ctx.frange.range, block_expr) | 43 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; |
43 | .map(|expr_to_unwrap| (ast::Expr::ForExpr(for_expr), expr_to_unwrap)) | 44 | (ast::Expr::ForExpr(for_expr), expr_to_unwrap) |
44 | } else if let Some(while_expr) = l_curly_token.ancestors().find_map(WhileExpr::cast) { | 45 | } else if let Some(while_expr) = l_curly_token.ancestors().find_map(WhileExpr::cast) { |
45 | // while expression | 46 | // while expression |
46 | let block_expr = while_expr.loop_body()?; | 47 | let block_expr = while_expr.loop_body()?; |
47 | extract_expr(ctx.frange.range, block_expr) | 48 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; |
48 | .map(|expr_to_unwrap| (ast::Expr::WhileExpr(while_expr), expr_to_unwrap)) | 49 | (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) |
49 | } else if let Some(loop_expr) = l_curly_token.ancestors().find_map(LoopExpr::cast) { | 50 | } else if let Some(loop_expr) = l_curly_token.ancestors().find_map(LoopExpr::cast) { |
50 | // loop expression | 51 | // loop expression |
51 | let block_expr = loop_expr.loop_body()?; | 52 | let block_expr = loop_expr.loop_body()?; |
52 | extract_expr(ctx.frange.range, block_expr) | 53 | let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; |
53 | .map(|expr_to_unwrap| (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap)) | 54 | (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) |
54 | } else { | 55 | } else { |
55 | None | 56 | return None; |
56 | }; | 57 | }; |
57 | 58 | ||
58 | let (expr, expr_to_unwrap) = res?; | ||
59 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { | 59 | ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { |
60 | edit.set_cursor(expr.syntax().text_range().start()); | 60 | edit.set_cursor(expr.syntax().text_range().start()); |
61 | edit.target(expr_to_unwrap.syntax().text_range()); | 61 | edit.target(expr_to_unwrap.syntax().text_range()); |