rust-analyzer/crates/ra_fmt/src/lib.rs

97 lines
2.7 KiB
Rust
Raw Normal View History

//! This crate provides some utilities for indenting rust code.
2019-11-12 12:41:02 +00:00
use std::iter::successors;
2019-02-03 18:26:35 +00:00
use itertools::Itertools;
2019-01-10 15:32:02 +00:00
use ra_syntax::{
2019-04-02 07:23:18 +00:00
ast::{self, AstNode, AstToken},
2019-07-18 21:35:27 +00:00
SmolStr, SyntaxKind,
SyntaxKind::*,
SyntaxNode, SyntaxToken, T,
2019-01-10 15:32:02 +00:00
};
2019-02-03 18:26:35 +00:00
pub fn reindent(text: &str, indent: &str) -> String {
let indent = format!("\n{}", indent);
text.lines().intersperse(&indent).collect()
}
2019-01-13 18:54:28 +00:00
/// If the node is on the beginning of the line, calculate indent.
2019-07-18 21:35:27 +00:00
pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
2019-03-30 10:25:53 +00:00
for token in prev_tokens(node.first_token()?) {
2019-07-18 21:35:27 +00:00
if let Some(ws) = ast::Whitespace::cast(token.clone()) {
2019-01-25 08:23:15 +00:00
let ws_text = ws.text();
if let Some(pos) = ws_text.rfind('\n') {
2019-07-18 21:35:27 +00:00
return Some(ws_text[pos + 1..].into());
2019-01-25 08:23:15 +00:00
}
}
2019-03-30 10:25:53 +00:00
if token.text().contains('\n') {
2019-01-25 08:23:15 +00:00
break;
}
}
None
}
2019-03-30 10:25:53 +00:00
fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
2019-07-18 21:35:27 +00:00
successors(token.prev_token(), |token| token.prev_token())
2019-01-13 15:21:23 +00:00
}
pub fn unwrap_trivial_block(block: ast::BlockExpr) -> ast::Expr {
extract_trivial_expression(&block)
.filter(|expr| !expr.syntax().text().contains_char('\n'))
.unwrap_or_else(|| block.into())
}
pub fn extract_trivial_expression(block: &ast::BlockExpr) -> Option<ast::Expr> {
let has_anything_else = |thing: &SyntaxNode| -> bool {
let mut non_trivial_children =
block.syntax().children_with_tokens().filter(|it| match it.kind() {
WHITESPACE | T!['{'] | T!['}'] => false,
_ => it.as_node() != Some(thing),
});
non_trivial_children.next().is_some()
};
if let Some(expr) = block.expr() {
if has_anything_else(expr.syntax()) {
return None;
}
return Some(expr);
}
// Unwrap `{ continue; }`
let (stmt,) = block.statements().next_tuple()?;
if let ast::Stmt::ExprStmt(expr_stmt) = stmt {
if has_anything_else(expr_stmt.syntax()) {
return None;
}
let expr = expr_stmt.expr()?;
match expr.syntax().kind() {
CONTINUE_EXPR | BREAK_EXPR | RETURN_EXPR => return Some(expr),
_ => (),
}
2019-01-10 15:32:02 +00:00
}
None
2019-01-10 15:32:02 +00:00
}
2019-03-30 10:25:53 +00:00
pub fn compute_ws(left: SyntaxKind, right: SyntaxKind) -> &'static str {
match left {
2019-05-15 12:35:47 +00:00
T!['('] | T!['['] => return "",
T!['{'] => {
2019-03-30 10:25:53 +00:00
if let USE_TREE = right {
2019-01-10 15:32:02 +00:00
return "";
}
}
_ => (),
}
2019-03-30 10:25:53 +00:00
match right {
2019-05-15 12:35:47 +00:00
T![')'] | T![']'] => return "",
T!['}'] => {
2019-03-30 10:25:53 +00:00
if let USE_TREE = left {
2019-01-10 15:32:02 +00:00
return "";
}
}
2019-05-15 12:35:47 +00:00
T![.] => return "",
2019-01-10 15:32:02 +00:00
_ => (),
}
" "
}