2019-02-08 11:58:27 -06:00
|
|
|
//! This crate provides some utilities for indenting rust code.
|
|
|
|
//!
|
2019-02-03 12:26:35 -06:00
|
|
|
use itertools::Itertools;
|
2019-01-10 09:32:02 -06:00
|
|
|
use ra_syntax::{
|
2019-01-11 05:57:19 -06:00
|
|
|
AstNode,
|
2019-01-10 09:32:02 -06:00
|
|
|
SyntaxNode, SyntaxKind::*,
|
2019-01-11 05:57:19 -06:00
|
|
|
ast::{self, AstToken},
|
2019-01-13 09:21:23 -06:00
|
|
|
algo::generate,
|
2019-01-10 09:32:02 -06:00
|
|
|
};
|
|
|
|
|
2019-02-03 12:26:35 -06:00
|
|
|
pub fn reindent(text: &str, indent: &str) -> String {
|
|
|
|
let indent = format!("\n{}", indent);
|
|
|
|
text.lines().intersperse(&indent).collect()
|
|
|
|
}
|
|
|
|
|
2019-01-13 12:54:28 -06:00
|
|
|
/// If the node is on the beginning of the line, calculate indent.
|
2019-02-03 12:26:35 -06:00
|
|
|
pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
2019-01-25 02:23:15 -06:00
|
|
|
for leaf in prev_leaves(node) {
|
|
|
|
if let Some(ws) = ast::Whitespace::cast(leaf) {
|
|
|
|
let ws_text = ws.text();
|
|
|
|
if let Some(pos) = ws_text.rfind('\n') {
|
|
|
|
return Some(&ws_text[pos + 1..]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if leaf.leaf_text().unwrap().contains('\n') {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
None
|
|
|
|
}
|
|
|
|
|
|
|
|
fn prev_leaves(node: &SyntaxNode) -> impl Iterator<Item = &SyntaxNode> {
|
|
|
|
generate(prev_leaf(node), |&node| prev_leaf(node))
|
2019-01-11 05:57:19 -06:00
|
|
|
}
|
|
|
|
|
2019-01-13 09:21:23 -06:00
|
|
|
fn prev_leaf(node: &SyntaxNode) -> Option<&SyntaxNode> {
|
2019-02-08 05:49:43 -06:00
|
|
|
generate(node.ancestors().find_map(SyntaxNode::prev_sibling), |it| it.last_child()).last()
|
2019-01-13 09:21:23 -06:00
|
|
|
}
|
|
|
|
|
2019-02-03 12:26:35 -06:00
|
|
|
pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
|
2019-01-10 09:32:02 -06:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-02-08 11:58:27 -06:00
|
|
|
pub fn compute_ws(left: &SyntaxNode, right: &SyntaxNode) -> &'static str {
|
2019-01-10 09:32:02 -06:00
|
|
|
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 "",
|
|
|
|
_ => (),
|
|
|
|
}
|
|
|
|
" "
|
|
|
|
}
|