aboutsummaryrefslogtreecommitdiff
path: root/crates
diff options
context:
space:
mode:
Diffstat (limited to 'crates')
-rw-r--r--crates/ra_fmt/src/lib.rs14
1 files changed, 7 insertions, 7 deletions
diff --git a/crates/ra_fmt/src/lib.rs b/crates/ra_fmt/src/lib.rs
index 1c2c04ad2..d6e895729 100644
--- a/crates/ra_fmt/src/lib.rs
+++ b/crates/ra_fmt/src/lib.rs
@@ -3,7 +3,7 @@
3use itertools::Itertools; 3use itertools::Itertools;
4use ra_syntax::{ 4use ra_syntax::{
5 ast::{self, AstNode, AstToken}, 5 ast::{self, AstNode, AstToken},
6 SyntaxKind, 6 SmolStr, SyntaxKind,
7 SyntaxKind::*, 7 SyntaxKind::*,
8 SyntaxNode, SyntaxToken, T, 8 SyntaxNode, SyntaxToken, T,
9}; 9};
@@ -15,12 +15,12 @@ pub fn reindent(text: &str, indent: &str) -> String {
15} 15}
16 16
17/// If the node is on the beginning of the line, calculate indent. 17/// If the node is on the beginning of the line, calculate indent.
18pub fn leading_indent(node: &SyntaxNode) -> Option<&str> { 18pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
19 for token in prev_tokens(node.first_token()?) { 19 for token in prev_tokens(node.first_token()?) {
20 if let Some(ws) = ast::Whitespace::cast(token) { 20 if let Some(ws) = ast::Whitespace::cast(token.clone()) {
21 let ws_text = ws.text(); 21 let ws_text = ws.text();
22 if let Some(pos) = ws_text.rfind('\n') { 22 if let Some(pos) = ws_text.rfind('\n') {
23 return Some(&ws_text[pos + 1..]); 23 return Some(ws_text[pos + 1..].into());
24 } 24 }
25 } 25 }
26 if token.text().contains('\n') { 26 if token.text().contains('\n') {
@@ -31,17 +31,17 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
31} 31}
32 32
33fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> { 33fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
34 successors(token.prev_token(), |&token| token.prev_token()) 34 successors(token.prev_token(), |token| token.prev_token())
35} 35}
36 36
37pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { 37pub fn extract_trivial_expression(block: &ast::Block) -> Option<ast::Expr> {
38 let expr = block.expr()?; 38 let expr = block.expr()?;
39 if expr.syntax().text().contains('\n') { 39 if expr.syntax().text().contains('\n') {
40 return None; 40 return None;
41 } 41 }
42 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { 42 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
43 WHITESPACE | T!['{'] | T!['}'] => false, 43 WHITESPACE | T!['{'] | T!['}'] => false,
44 _ => it != &expr.syntax(), 44 _ => it != expr.syntax(),
45 }); 45 });
46 if non_trivial_children.count() > 0 { 46 if non_trivial_children.count() > 0 {
47 return None; 47 return None;