aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_fmt
diff options
context:
space:
mode:
authorAleksey Kladov <[email protected]>2019-03-30 10:25:53 +0000
committerAleksey Kladov <[email protected]>2019-04-01 10:06:24 +0100
commit9e213385c9d06db3c8ca20812779e2b8f8ad2c71 (patch)
treefe57697b54ccfb791fe96c13cb553a8570516270 /crates/ra_fmt
parentdec9bde10868b5e459535449476d17a6a0987b3e (diff)
switch to new rowan
Diffstat (limited to 'crates/ra_fmt')
-rw-r--r--crates/ra_fmt/src/lib.rs28
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 @@
3use itertools::Itertools; 3use itertools::Itertools;
4use ra_syntax::{ 4use 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.
17pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { 17pub 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
32fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> { 32fn 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
36fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
37 generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last()
38} 34}
39 35
40pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { 36pub 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
55pub fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { 51pub 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 }