diff options
author | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
---|---|---|
committer | bors[bot] <bors[bot]@users.noreply.github.com> | 2019-01-10 15:32:34 +0000 |
commit | acd90bc33b6111b47af68d51ec668bf027afb31d (patch) | |
tree | 42e0151888d94e8f9e33d52e44771252d9e151d5 /crates/ra_ide_api_light/src/formatting.rs | |
parent | 3990f93921537c4ab4db3a89b76ecfd20504d93f (diff) | |
parent | a0c978cd0ce95446f6d3e6a13047474670d9ee55 (diff) |
Merge #482
482: fix code duplication r=matklad a=matklad
Co-authored-by: Aleksey Kladov <[email protected]>
Diffstat (limited to 'crates/ra_ide_api_light/src/formatting.rs')
-rw-r--r-- | crates/ra_ide_api_light/src/formatting.rs | 42 |
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 @@ | |||
1 | use ra_syntax::{ | ||
2 | ast, AstNode, | ||
3 | SyntaxNode, SyntaxKind::*, | ||
4 | }; | ||
5 | |||
6 | pub(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 | |||
21 | pub(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 | } | ||