2019-01-10 15:32:02 +00:00
|
|
|
use ra_syntax::{
|
2019-01-11 11:57:19 +00:00
|
|
|
AstNode,
|
2019-01-10 15:32:02 +00:00
|
|
|
SyntaxNode, SyntaxKind::*,
|
2019-01-11 11:57:19 +00:00
|
|
|
ast::{self, AstToken},
|
2019-01-13 15:21:23 +00:00
|
|
|
algo::generate,
|
2019-01-10 15:32:02 +00:00
|
|
|
};
|
|
|
|
|
2019-01-13 18:54:28 +00:00
|
|
|
/// If the node is on the beginning of the line, calculate indent.
|
2019-01-11 11:57:19 +00:00
|
|
|
pub(crate) fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
2019-01-13 15:21:23 +00:00
|
|
|
let prev = prev_leaf(node)?;
|
2019-01-11 11:57:19 +00:00
|
|
|
let ws_text = ast::Whitespace::cast(prev)?.text();
|
|
|
|
ws_text.rfind('\n').map(|pos| &ws_text[pos + 1..])
|
|
|
|
}
|
|
|
|
|
2019-01-13 15:21:23 +00:00
|
|
|
fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
|
|
|
|
generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| {
|
|
|
|
it.last_child()
|
|
|
|
})
|
|
|
|
.last()
|
|
|
|
}
|
|
|
|
|
2019-01-10 15:32:02 +00:00
|
|
|
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 "",
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
" "
|
|
|
|
}
|