diff options
author | Andrea Pretto <[email protected]> | 2019-02-08 17:58:27 +0000 |
---|---|---|
committer | Andrea Pretto <[email protected]> | 2019-02-08 17:58:27 +0000 |
commit | 02dd0cfd8c7bf50cfb26c3c5178be5af4f3fdd25 (patch) | |
tree | 7fe910ed99307268a06e5668bb4401b6c6916fdb /crates/ra_ide_api_light/src/formatting.rs | |
parent | 12e3b4c70b5ef23b2fdfc197296d483680e125f9 (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.rs | 74 |
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 @@ | |||
1 | use itertools::Itertools; | ||
2 | use ra_syntax::{ | ||
3 | AstNode, | ||
4 | SyntaxNode, SyntaxKind::*, | ||
5 | ast::{self, AstToken}, | ||
6 | algo::generate, | ||
7 | }; | ||
8 | |||
9 | pub 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. | ||
15 | pub 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 | |||
30 | fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> { | ||
31 | generate(prev_leaf(node), |&node| prev_leaf(node)) | ||
32 | } | ||
33 | |||
34 | fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> { | ||
35 | generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last() | ||
36 | } | ||
37 | |||
38 | pub 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 | |||
53 | pub(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 | } | ||