diff options
Diffstat (limited to 'crates/ra_fmt/src/lib.rs')
-rw-r--r-- | crates/ra_fmt/src/lib.rs | 67 |
1 files changed, 2 insertions, 65 deletions
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs index 397a3c3ae..b92477f9e 100644 --- a/crates/ra_fmt/src/lib.rs +++ b/crates/ra_fmt/src/lib.rs | |||
@@ -2,12 +2,9 @@ | |||
2 | 2 | ||
3 | use std::iter::successors; | 3 | use std::iter::successors; |
4 | 4 | ||
5 | use itertools::Itertools; | ||
6 | use syntax::{ | 5 | use syntax::{ |
7 | ast::{self, AstNode, AstToken}, | 6 | ast::{self, AstToken}, |
8 | SmolStr, SyntaxKind, | 7 | SmolStr, SyntaxNode, SyntaxToken, |
9 | SyntaxKind::*, | ||
10 | SyntaxNode, SyntaxToken, T, | ||
11 | }; | 8 | }; |
12 | 9 | ||
13 | /// If the node is on the beginning of the line, calculate indent. | 10 | /// If the node is on the beginning of the line, calculate indent. |
@@ -29,63 +26,3 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> { | |||
29 | fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { | 26 | fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { |
30 | successors(token.prev_token(), |token| token.prev_token()) | 27 | successors(token.prev_token(), |token| token.prev_token()) |
31 | } | 28 | } |
32 | |||
33 | pub 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 | |||
39 | pub 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 | |||
70 | pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { | ||
71 | match left { | ||
72 | T!['('] | T!['['] => return "", | ||
73 | T!['{'] => { | ||
74 | if let USE_TREE = right { | ||
75 | return ""; | ||
76 | } | ||
77 | } | ||
78 | _ => (), | ||
79 | } | ||
80 | match right { | ||
81 | T![')'] | T![']'] => return "", | ||
82 | T!['}'] => { | ||
83 | if let USE_TREE = left { | ||
84 | return ""; | ||
85 | } | ||
86 | } | ||
87 | T![.] => return "", | ||
88 | _ => (), | ||
89 | } | ||
90 | " " | ||
91 | } | ||