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.rs42
1 files changed, 42 insertions, 0 deletions
diff --git a/crates/ra_ide_api_light/src/formatting.rs b/crates/ra_ide_api_light/src/formatting.rs
new file mode 100644
index 000000000..1f3769209
--- /dev/null
+++ b/crates/ra_ide_api_light/src/formatting.rs
@@ -0,0 +1,42 @@
1use ra_syntax::{
2 ast, AstNode,
3 SyntaxNode, SyntaxKind::*,
4};
5
6pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
7 let expr = block.expr()?;
8 if expr.syntax().text().contains('\n') {
9 return None;
10 }
11 let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
12 WHITESPACE | L_CURLY | R_CURLY => false,
13 _ => it != &expr.syntax(),
14 });
15 if non_trivial_children.count() > 0 {
16 return None;
17 }
18 Some(expr)
19}
20
21pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
22 match left.kind() {
23 L_PAREN | L_BRACK => return "",
24 L_CURLY => {
25 if let USE_TREE = right.kind() {
26 return "";
27 }
28 }
29 _ => (),
30 }
31 match right.kind() {
32 R_PAREN | R_BRACK => return "",
33 R_CURLY => {
34 if let USE_TREE = left.kind() {
35 return "";
36 }
37 }
38 DOT => return "",
39 _ => (),
40 }
41 " "
42}