From f0a9128761e8dd42f0dd40610a1d8734a52f3d9c Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 13 Aug 2020 11:41:20 +0200 Subject: Minor --- .../src/handlers/replace_if_let_with_match.rs | 6 ++-- crates/ra_assists/src/handlers/unwrap_block.rs | 3 +- crates/ra_assists/src/utils.rs | 38 ++++++++++++++++++++ crates/ra_fmt/src/lib.rs | 40 +--------------------- crates/ra_ide/src/join_lines.rs | 3 +- 5 files changed, 46 insertions(+), 44 deletions(-) diff --git a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs index 2442f049b..79097621e 100644 --- a/crates/ra_assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/ra_assists/src/handlers/replace_if_let_with_match.rs @@ -1,4 +1,3 @@ -use ra_fmt::unwrap_trivial_block; use syntax::{ ast::{ self, @@ -8,7 +7,10 @@ use syntax::{ AstNode, }; -use crate::{utils::TryEnum, AssistContext, AssistId, AssistKind, Assists}; +use crate::{ + utils::{unwrap_trivial_block, TryEnum}, + AssistContext, AssistId, AssistKind, Assists, +}; // Assist: replace_if_let_with_match // diff --git a/crates/ra_assists/src/handlers/unwrap_block.rs b/crates/ra_assists/src/handlers/unwrap_block.rs index 2879090b8..3851aeb3e 100644 --- a/crates/ra_assists/src/handlers/unwrap_block.rs +++ b/crates/ra_assists/src/handlers/unwrap_block.rs @@ -1,4 +1,3 @@ -use ra_fmt::unwrap_trivial_block; use syntax::{ ast::{ self, @@ -7,7 +6,7 @@ use syntax::{ AstNode, TextRange, T, }; -use crate::{AssistContext, AssistId, AssistKind, Assists}; +use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; // Assist: unwrap_block // diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs index 6d85661c4..a20453dd8 100644 --- a/crates/ra_assists/src/utils.rs +++ b/crates/ra_assists/src/utils.rs @@ -4,6 +4,7 @@ pub(crate) mod insert_use; use std::{iter, ops}; use hir::{Adt, Crate, Enum, ScopeDef, Semantics, Trait, Type}; +use itertools::Itertools; use ra_ide_db::RootDatabase; use rustc_hash::FxHashSet; use syntax::{ @@ -17,6 +18,43 @@ use crate::assist_config::SnippetCap; pub(crate) use insert_use::{find_insert_use_container, insert_use_statement}; +pub(crate) fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { + extract_trivial_expression(&block) + .filter(|expr| !expr.syntax().text().contains_char('\n')) + .unwrap_or_else(|| block.into()) +} + +pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option { + let has_anything_else = |thing: &SyntaxNode| -> bool { + let mut non_trivial_children = + block.syntax().children_with_tokens().filter(|it| match it.kind() { + WHITESPACE | T!['{'] | T!['}'] => false, + _ => it.as_node() != Some(thing), + }); + non_trivial_children.next().is_some() + }; + + if let Some(expr) = block.expr() { + if has_anything_else(expr.syntax()) { + return None; + } + return Some(expr); + } + // Unwrap `{ continue; }` + let (stmt,) = block.statements().next_tuple()?; + if let ast::Stmt::ExprStmt(expr_stmt) = stmt { + if has_anything_else(expr_stmt.syntax()) { + return None; + } + let expr = expr_stmt.expr()?; + match expr.syntax().kind() { + CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), + _ => (), + } + } + None +} + #[derive(Clone, Copy, Debug)] pub(crate) enum Cursor<'a> { Replace(&'a SyntaxNode), diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs index 397a3c3ae..d0004654b 100644 --- a/crates/ra_fmt/src/lib.rs +++ b/crates/ra_fmt/src/lib.rs @@ -2,9 +2,8 @@ use std::iter::successors; -use itertools::Itertools; use syntax::{ - ast::{self, AstNode, AstToken}, + ast::{self, AstToken}, SmolStr, SyntaxKind, SyntaxKind::*, SyntaxNode, SyntaxToken, T, @@ -30,43 +29,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator { successors(token.prev_token(), |token| token.prev_token()) } -pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr { - extract_trivial_expression(&block) - .filter(|expr| !expr.syntax().text().contains_char('\n')) - .unwrap_or_else(|| block.into()) -} - -pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option { - let has_anything_else = |thing: &SyntaxNode| -> bool { - let mut non_trivial_children = - block.syntax().children_with_tokens().filter(|it| match it.kind() { - WHITESPACE | T!['{'] | T!['}'] => false, - _ => it.as_node() != Some(thing), - }); - non_trivial_children.next().is_some() - }; - - if let Some(expr) = block.expr() { - if has_anything_else(expr.syntax()) { - return None; - } - return Some(expr); - } - // Unwrap `{ continue; }` - let (stmt,) = block.statements().next_tuple()?; - if let ast::Stmt::ExprStmt(expr_stmt) = stmt { - if has_anything_else(expr_stmt.syntax()) { - return None; - } - let expr = expr_stmt.expr()?; - match expr.syntax().kind() { - CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr), - _ => (), - } - } - None -} - pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { match left { T!['('] | T!['['] => return "", diff --git a/crates/ra_ide/src/join_lines.rs b/crates/ra_ide/src/join_lines.rs index 35cec87f6..574ce219d 100644 --- a/crates/ra_ide/src/join_lines.rs +++ b/crates/ra_ide/src/join_lines.rs @@ -1,5 +1,6 @@ use itertools::Itertools; -use ra_fmt::{compute_ws, extract_trivial_expression}; +use ra_assists::utils::extract_trivial_expression; +use ra_fmt::compute_ws; use syntax::{ algo::{find_covering_element, non_trivia_sibling}, ast::{self, AstNode, AstToken}, -- cgit v1.2.3