From a0c978cd0ce95446f6d3e6a13047474670d9ee55 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Thu, 10 Jan 2019 18:32:02 +0300 Subject: fix code duplication --- crates/ra_ide_api_light/src/formatting.rs | 42 +++++++++++++++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 crates/ra_ide_api_light/src/formatting.rs (limited to 'crates/ra_ide_api_light/src/formatting.rs') 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 @@ +use ra_syntax::{ + ast, AstNode, + SyntaxNode, SyntaxKind::*, +}; + +pub(crate) fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> { + let expr = block.expr()?; + if expr.syntax().text().contains('\n') { + return None; + } + let non_trivial_children = block.syntax().children().filter(|it| match it.kind() { + WHITESPACE | L_CURLY | R_CURLY => false, + _ => it != &expr.syntax(), + }); + if non_trivial_children.count() > 0 { + return None; + } + Some(expr) +} + +pub(crate) fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str { + match left.kind() { + L_PAREN | L_BRACK => return "", + L_CURLY => { + if let USE_TREE = right.kind() { + return ""; + } + } + _ => (), + } + match right.kind() { + R_PAREN | R_BRACK => return "", + R_CURLY => { + if let USE_TREE = left.kind() { + return ""; + } + } + DOT => return "", + _ => (), + } + " " +} -- cgit v1.2.3