diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-01 10:30:25 +0100 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-04-01 10:30:25 +0100 |
commit | 42a883f06c28ddeab22e5703a578f19110dde7f3 (patch) | |
tree | fe57697b54ccfb791fe96c13cb553a8570516270 /crates/ra_fmt | |
parent | dec9bde10868b5e459535449476d17a6a0987b3e (diff) | |
parent | 9e213385c9d06db3c8ca20812779e2b8f8ad2c71 (diff) |
Merge #1078
1078: rewrite syntax trees r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_fmt')
-rw-r--r-- | crates/ra_fmt/src/lib.rs | 28 |
1 files changed, 12 insertions, 16 deletions
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs index 62e6fb9c1..ea90dc2b8 100644 --- a/crates/ra_fmt/src/lib.rs +++ b/crates/ra_fmt/src/lib.rs | |||
@@ -3,8 +3,8 @@ | |||
3 | use itertools::Itertools; | 3 | use itertools::Itertools; |
4 | use ra_syntax::{ | 4 | use ra_syntax::{ |
5 | AstNode, | 5 | AstNode, |
6 | SyntaxNode, SyntaxKind::*, | 6 | SyntaxNode, SyntaxKind::*, SyntaxToken, SyntaxKind, |
7 | ast::{self, AstToken}, | 7 | ast, |
8 | algo::generate, | 8 | algo::generate, |
9 | }; | 9 | }; |
10 | 10 | ||
@@ -15,26 +15,22 @@ pub fn reindent(text: &str, indent: &str) -> String { | |||
15 | 15 | ||
16 | /// If the node is on the beginning of the line, calculate indent. | 16 | /// If the node is on the beginning of the line, calculate indent. |
17 | pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { | 17 | pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { |
18 | for leaf in prev_leaves(node) { | 18 | for token in prev_tokens(node.first_token()?) { |
19 | if let Some(ws) = ast::Whitespace::cast(leaf) { | 19 | if let Some(ws) = ast::Whitespace::cast(token) { |
20 | let ws_text = ws.text(); | 20 | let ws_text = ws.text(); |
21 | if let Some(pos) = ws_text.rfind('\n') { | 21 | if let Some(pos) = ws_text.rfind('\n') { |
22 | return Some(&ws_text[pos + 1..]); | 22 | return Some(&ws_text[pos + 1..]); |
23 | } | 23 | } |
24 | } | 24 | } |
25 | if leaf.leaf_text().unwrap().contains('\n') { | 25 | if token.text().contains('\n') { |
26 | break; | 26 | break; |
27 | } | 27 | } |
28 | } | 28 | } |
29 | None | 29 | None |
30 | } | 30 | } |
31 | 31 | ||
32 | fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> { | 32 | fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { |
33 | generate(prev_leaf(node), |&node| prev_leaf(node)) | 33 | generate(token.prev_token(), |&token| token.prev_token()) |
34 | } | ||
35 | |||
36 | fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { | ||
37 | generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last() | ||
38 | } | 34 | } |
39 | 35 | ||
40 | pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { | 36 | pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { |
@@ -52,20 +48,20 @@ pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { | |||
52 | Some(expr) | 48 | Some(expr) |
53 | } | 49 | } |
54 | 50 | ||
55 | pub fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { | 51 | pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str { |
56 | match left.kind() { | 52 | match left { |
57 | L_PAREN | L_BRACK => return "", | 53 | L_PAREN | L_BRACK => return "", |
58 | L_CURLY => { | 54 | L_CURLY => { |
59 | if let USE_TREE = right.kind() { | 55 | if let USE_TREE = right { |
60 | return ""; | 56 | return ""; |
61 | } | 57 | } |
62 | } | 58 | } |
63 | _ => (), | 59 | _ => (), |
64 | } | 60 | } |
65 | match right.kind() { | 61 | match right { |
66 | R_PAREN | R_BRACK => return "", | 62 | R_PAREN | R_BRACK => return "", |
67 | R_CURLY => { | 63 | R_CURLY => { |
68 | if let USE_TREE = left.kind() { | 64 | if let USE_TREE = left { |
69 | return ""; | 65 | return ""; |
70 | } | 66 | } |
71 | } | 67 | } |