diff options
author | Aleksey Kladov <[email protected]> | 2019-01-13 15:21:23 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2019-01-13 15:21:23 +0000 |
commit | 884ce4a4207ca68a5299b3a2e4e33b8f1f158001 (patch) | |
tree | ed8a3c14e19a67426c9b08cebb8e34ae91c8f8ab /crates/ra_ide_api_light/src/formatting.rs | |
parent | c46e0300e6cebf78c78c28ca91e45b57e4e40954 (diff) |
fix indent caclulation
Diffstat (limited to 'crates/ra_ide_api_light/src/formatting.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/formatting.rs | 10 |
1 files changed, 9 insertions, 1 deletions
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs index 4635fbd60..599e3cdcb 100644 --- a/crates/ra_ide_api_light/src/formatting.rs +++ b/crates/ra_ide_api_light/src/formatting.rs | |||
@@ -2,15 +2,23 @@ use ra_syntax::{ | |||
2 | AstNode, | 2 | AstNode, |
3 | SyntaxNode, SyntaxKind::*, | 3 | SyntaxNode, SyntaxKind::*, |
4 | ast::{self, AstToken}, | 4 | ast::{self, AstToken}, |
5 | algo::generate, | ||
5 | }; | 6 | }; |
6 | 7 | ||
7 | /// If the node is on the begining of the line, calculate indent. | 8 | /// If the node is on the begining of the line, calculate indent. |
8 | pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { | 9 | pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { |
9 | let prev = node.prev_sibling()?; | 10 | let prev = prev_leaf(node)?; |
10 | let ws_text = ast::Whitespace::cast(prev)?.text(); | 11 | let ws_text = ast::Whitespace::cast(prev)?.text(); |
11 | ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..]) | 12 | ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..]) |
12 | } | 13 | } |
13 | 14 | ||
15 | fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { | ||
16 | generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| { | ||
17 | it.last_child() | ||
18 | }) | ||
19 | .last() | ||
20 | } | ||
21 | |||
14 | pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { | 22 | pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { |
15 | let expr = block.expr()?; | 23 | let expr = block.expr()?; |
16 | if expr.syntax().text().contains('\n') { | 24 | if expr.syntax().text().contains('\n') { |