Minor
This commit is contained in:
parent
b0f03db51d
commit
f0a9128761
@ -1,4 +1,3 @@
|
||||
use ra_fmt::unwrap_trivial_block;
|
||||
use syntax::{
|
||||
ast::{
|
||||
self,
|
||||
@ -8,7 +7,10 @@
|
||||
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
|
||||
//
|
||||
|
@ -1,4 +1,3 @@
|
||||
use ra_fmt::unwrap_trivial_block;
|
||||
use syntax::{
|
||||
ast::{
|
||||
self,
|
||||
@ -7,7 +6,7 @@
|
||||
AstNode, TextRange, T,
|
||||
};
|
||||
|
||||
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
|
||||
|
||||
// Assist: unwrap_block
|
||||
//
|
||||
|
@ -4,6 +4,7 @@
|
||||
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 @@
|
||||
|
||||
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<ast::Expr> {
|
||||
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),
|
||||
|
@ -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<Item = SyntaxToken> {
|
||||
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<ast::Expr> {
|
||||
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 "",
|
||||
|
@ -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},
|
||||
|
Loading…
Reference in New Issue
Block a user