aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/formatting.rs
diff options
context:
space:
mode:
Diffstat (limited to 'crates/ra_ide_api_light/src/formatting.rs')
-rw-r--r--crates/ra_ide_api_light/src/formatting.rs10
1 files changed, 8 insertions, 2 deletions
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs
index 1f34b85d6..46ffa7d96 100644
--- a/crates/ra_ide_api_light/src/formatting.rs
+++ b/crates/ra_ide_api_light/src/formatting.rs
@@ -1,3 +1,4 @@
1use itertools::Itertools;
1use ra_syntax::{ 2use ra_syntax::{
2 AstNode, 3 AstNode,
3 SyntaxNode, SyntaxKind::*, 4 SyntaxNode, SyntaxKind::*,
@@ -5,8 +6,13 @@ use ra_syntax::{
5 algo::generate, 6 algo::generate,
6}; 7};
7 8
9pub fn reindent(text: &str, indent: &str) -> String {
10 let indent = format!("\n{}", indent);
11 text.lines().intersperse(&indent).collect()
12}
13
8/// If the node is on the beginning of the line, calculate indent. 14/// If the node is on the beginning of the line, calculate indent.
9pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> { 15pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
10 for leaf in prev_leaves(node) { 16 for leaf in prev_leaves(node) {
11 if let Some(ws) = ast::Whitespace::cast(leaf) { 17 if let Some(ws) = ast::Whitespace::cast(leaf) {
12 let ws_text = ws.text(); 18 let ws_text = ws.text();
@@ -32,7 +38,7 @@ fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
32 .last() 38 .last()
33} 39}
34 40
35pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { 41pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
36 let expr = block.expr()?; 42 let expr = block.expr()?;
37 if expr.syntax().text().contains('\n') { 43 if expr.syntax().text().contains('\n') {
38 return None; 44 return None;