aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_fmt
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_fmt')
-rw-r--r--crates/ra_fmt/src/lib.rs40
1 files changed, 1 insertions, 39 deletions
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 @@
2 2
3use std::iter::successors; 3use std::iter::successors;
4 4
5use itertools::Itertools;
6use syntax::{ 5use syntax::{
7 ast::{self, AstNode, AstToken}, 6 ast::{self, AstToken},
8 SmolStr, SyntaxKind, 7 SmolStr, SyntaxKind,
9 SyntaxKind::*, 8 SyntaxKind::*,
10 SyntaxNode, SyntaxToken, T, 9 SyntaxNode, SyntaxToken, T,
@@ -30,43 +29,6 @@ fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
30 successors(token.prev_token(), |token| token.prev_token()) 29 successors(token.prev_token(), |token| token.prev_token())
31} 30}
32 31
33pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
34 extract_trivial_expression(&block)
35 .filter(|expr| !expr.syntax().text().contains_char('\n'))
36 .unwrap_or_else(|| block.into())
37}
38
39pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
40 let has_anything_else = |thing: &SyntaxNode| -> bool {
41 let mut non_trivial_children =
42 block.syntax().children_with_tokens().filter(|it| match it.kind() {
43 WHITESPACE | T!['{'] | T!['}'] => false,
44 _ => it.as_node() != Some(thing),
45 });
46 non_trivial_children.next().is_some()
47 };
48
49 if let Some(expr) = block.expr() {
50 if has_anything_else(expr.syntax()) {
51 return None;
52 }
53 return Some(expr);
54 }
55 // Unwrap `{ continue; }`
56 let (stmt,) = block.statements().next_tuple()?;
57 if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
58 if has_anything_else(expr_stmt.syntax()) {
59 return None;
60 }
61 let expr = expr_stmt.expr()?;
62 match expr.syntax().kind() {
63 CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
64 _ => (),
65 }
66 }
67 None
68}
69
70pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { 32pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
71 match left { 33 match left {
72 T!['('] | T!['['] => return "", 34 T!['('] | T!['['] => return "",