From d7450222a97cb9abefc4fd843ae9d6f4d0d0f93f Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Mon, 4 May 2020 14:55:24 +0200 Subject: Simplify --- crates/ra_assists/src/handlers/unwrap_block.rs | 67 +++++++++++++------------- 1 file changed, 34 insertions(+), 33 deletions(-) (limited to 'crates') diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index b407eb870..859c70ad8 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -1,8 +1,8 @@ use crate::{Assist, AssistCtx, AssistId}; -use ast::{BlockExpr, Expr, ForExpr, IfExpr, LoopBodyOwner, LoopExpr, WhileExpr}; +use ast::LoopBodyOwner; use ra_fmt::unwrap_trivial_block; -use ra_syntax::{ast, AstNode, TextRange, T}; +use ra_syntax::{ast, match_ast, AstNode, TextRange, T}; // Assist: unwrap_block // @@ -23,37 +23,38 @@ use ra_syntax::{ast, AstNode, TextRange, T}; // ``` pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option { let l_curly_token = ctx.find_token_at_offset(T!['{'])?; - - let (expr, expr_to_unwrap) = if let Some(if_expr) = - l_curly_token.ancestors().find_map(IfExpr::cast) - { - // if expression - let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); - let expr_to_unwrap = expr_to_unwrap?; - // Find if we are in a else if block - let ancestor = if_expr.syntax().ancestors().skip(1).find_map(ast::IfExpr::cast); - - match ancestor { - None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), - Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), + let block = ast::BlockExpr::cast(l_curly_token.parent())?; + let parent = block.syntax().parent()?; + let (expr, expr_to_unwrap) = match_ast! { + match parent { + ast::IfExpr(if_expr) => { + let expr_to_unwrap = if_expr.blocks().find_map(|expr| extract_expr(ctx.frange.range, expr)); + let expr_to_unwrap = expr_to_unwrap?; + // Find if we are in a else if block + let ancestor = if_expr.syntax().parent().and_then(ast::IfExpr::cast); + + match ancestor { + None => (ast::Expr::IfExpr(if_expr), expr_to_unwrap), + Some(ancestor) => (ast::Expr::IfExpr(ancestor), expr_to_unwrap), + } + }, + ast::ForExpr(for_expr) => { + let block_expr = for_expr.loop_body()?; + let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; + (ast::Expr::ForExpr(for_expr), expr_to_unwrap) + }, + ast::WhileExpr(while_expr) => { + let block_expr = while_expr.loop_body()?; + let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; + (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) + }, + ast::LoopExpr(loop_expr) => { + let block_expr = loop_expr.loop_body()?; + let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; + (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) + }, + _ => return None, } - } else if let Some(for_expr) = l_curly_token.ancestors().find_map(ForExpr::cast) { - // for expression - let block_expr = for_expr.loop_body()?; - let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; - (ast::Expr::ForExpr(for_expr), expr_to_unwrap) - } else if let Some(while_expr) = l_curly_token.ancestors().find_map(WhileExpr::cast) { - // while expression - let block_expr = while_expr.loop_body()?; - let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; - (ast::Expr::WhileExpr(while_expr), expr_to_unwrap) - } else if let Some(loop_expr) = l_curly_token.ancestors().find_map(LoopExpr::cast) { - // loop expression - let block_expr = loop_expr.loop_body()?; - let expr_to_unwrap = extract_expr(ctx.frange.range, block_expr)?; - (ast::Expr::LoopExpr(loop_expr), expr_to_unwrap) - } else { - return None; }; ctx.add_assist(AssistId("unwrap_block"), "Unwrap block", |edit| { @@ -76,7 +77,7 @@ pub(crate) fn unwrap_block(ctx: AssistCtx) -> Option { }) } -fn extract_expr(cursor_range: TextRange, block: BlockExpr) -> Option { +fn extract_expr(cursor_range: TextRange, block: ast::BlockExpr) -> Option { let cursor_in_range = block.l_curly_token()?.text_range().contains_range(cursor_range); if cursor_in_range { -- cgit v1.2.3