aboutsummaryrefslogtreecommitdiff
path: root/crates/ra_ide_api_light/src/formatting.rs
diff options
context:
space:
mode:
authorAndrea Pretto <[email protected]>2019-02-08 17:58:27 +0000
committerAndrea Pretto <[email protected]>2019-02-08 17:58:27 +0000
commit02dd0cfd8c7bf50cfb26c3c5178be5af4f3fdd25 (patch)
tree7fe910ed99307268a06e5668bb4401b6c6916fdb /crates/ra_ide_api_light/src/formatting.rs
parent12e3b4c70b5ef23b2fdfc197296d483680e125f9 (diff)
Refactor formatting code out of ra_ida_api_light into ra_fmt.
Diffstat (limited to 'crates/ra_ide_api_light/src/formatting.rs')
-rw-r--r--crates/ra_ide_api_light/src/formatting.rs74
1 files changed, 0 insertions, 74 deletions
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs
deleted file mode 100644
index 8bc03f974..000000000
--- a/crates/ra_ide_api_light/src/formatting.rs
+++ /dev/null
@@ -1,74 +0,0 @@
1use itertools::Itertools;
2use ra_syntax::{
3 AstNode,
4 SyntaxNode, SyntaxKind::*,
5 ast::{self, AstToken},
6 algo::generate,
7};
8
9pub fn reindent(text: &str, indent: &str) -> String {
10 let indent = format!("\n{}", indent);
11 text.lines().intersperse(&indent).collect()
12}
13
14/// If the node is on the beginning of the line, calculate indent.
15pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
16 for leaf in prev_leaves(node) {
17 if let Some(ws) = ast::Whitespace::cast(leaf) {
18 let ws_text = ws.text();
19 if let Some(pos) = ws_text.rfind('\n') {
20 return Some(&ws_text[pos + 1..]);
21 }
22 }
23 if leaf.leaf_text().unwrap().contains('\n') {
24 break;
25 }
26 }
27 None
28}
29
30fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> {
31 generate(prev_leaf(node), |&node| prev_leaf(node))
32}
33
34fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
35 generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last()
36}
37
38pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
39 let expr = block.expr()?;
40 if expr.syntax().text().contains('\n') {
41 return None;
42 }
43 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
44 WHITESPACE | L_CURLY | R_CURLY => false,
45 _ => it != &expr.syntax(),
46 });
47 if non_trivial_children.count() > 0 {
48 return None;
49 }
50 Some(expr)
51}
52
53pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
54 match left.kind() {
55 L_PAREN | L_BRACK => return "",
56 L_CURLY => {
57 if let USE_TREE = right.kind() {
58 return "";
59 }
60 }
61 _ => (),
62 }
63 match right.kind() {
64 R_PAREN | R_BRACK => return "",
65 R_CURLY => {
66 if let USE_TREE = left.kind() {
67 return "";
68 }
69 }
70 DOT => return "",
71 _ => (),
72 }
73 " "
74}