From afc6ee251d4ee898bfd3f57a8a139750ab86a373 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Wed, 23 Oct 2019 17:41:15 +0300 Subject: minor cleanup --- crates/ra_assists/src/assists/early_return.rs | 54 ++++++++++++++++++--------- 1 file changed, 36 insertions(+), 18 deletions(-) (limited to 'crates/ra_assists/src') diff --git a/crates/ra_assists/src/assists/early_return.rs b/crates/ra_assists/src/assists/early_return.rs index 8c975714c..9c95adc53 100644 --- a/crates/ra_assists/src/assists/early_return.rs +++ b/crates/ra_assists/src/assists/early_return.rs @@ -1,32 +1,50 @@ -//! FIXME: write short doc here +//! Assist: `convert_to_guarded_return` +//! +//! Replace a large conditional with a guarded return. +//! +//! ```notrust +//! fn <|>main() { +//! if cond { +//! foo(); +//! bar(); +//! } +//! } +//! ``` +//! -> +//! ```notrust +//! fn main() { +//! if !cond { +//! return; +//! } +//! foo(); +//! bar(); +//! } +//! ``` + +use std::ops::RangeInclusive; -use crate::{ - assist_ctx::{Assist, AssistCtx}, - AssistId, -}; use hir::db::HirDatabase; use ra_syntax::{ algo::replace_children, - ast::edit::IndentLevel, - ast::make, - ast::Block, - ast::ContinueExpr, - ast::IfExpr, - ast::ReturnExpr, + ast::{self, edit::IndentLevel, make}, AstNode, SyntaxKind::{FN_DEF, LOOP_EXPR, L_CURLY, R_CURLY, WHILE_EXPR, WHITESPACE}, }; -use std::ops::RangeInclusive; + +use crate::{ + assist_ctx::{Assist, AssistCtx}, + AssistId, +}; pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> Option { - let if_expr: IfExpr = ctx.node_at_offset()?; + let if_expr: ast::IfExpr = ctx.node_at_offset()?; let expr = if_expr.condition()?.expr()?; let then_block = if_expr.then_branch()?.block()?; if if_expr.else_branch().is_some() { return None; } - let parent_block = if_expr.syntax().parent()?.ancestors().find_map(Block::cast)?; + let parent_block = if_expr.syntax().parent()?.ancestors().find_map(ast::Block::cast)?; if parent_block.expr()? != if_expr.clone().into() { return None; @@ -34,11 +52,11 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> // check for early return and continue let first_in_then_block = then_block.syntax().first_child()?.clone(); - if ReturnExpr::can_cast(first_in_then_block.kind()) - || ContinueExpr::can_cast(first_in_then_block.kind()) + if ast::ReturnExpr::can_cast(first_in_then_block.kind()) + || ast::ContinueExpr::can_cast(first_in_then_block.kind()) || first_in_then_block .children() - .any(|x| ReturnExpr::can_cast(x.kind()) || ContinueExpr::can_cast(x.kind())) + .any(|x| ast::ReturnExpr::can_cast(x.kind()) || ast::ContinueExpr::can_cast(x.kind())) { return None; } @@ -86,7 +104,7 @@ pub(crate) fn convert_to_guarded_return(mut ctx: AssistCtx) -> &mut new_if_and_then_statements, ); edit.target(if_expr.syntax().text_range()); - edit.replace_ast(parent_block, Block::cast(new_block).unwrap()); + edit.replace_ast(parent_block, ast::Block::cast(new_block).unwrap()); edit.set_cursor(cursor_position); }); ctx.build() -- cgit v1.2.3