mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-09-30 05:45:12 +00:00
migrate ra_fmt to new rowan
This commit is contained in:
parent
08fd402ef2
commit
7e02aa0eff
1 changed files with 7 additions and 7 deletions
|
@ -3,7 +3,7 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use ra_syntax::{
|
use ra_syntax::{
|
||||||
ast::{self, AstNode, AstToken},
|
ast::{self, AstNode, AstToken},
|
||||||
SyntaxKind,
|
SmolStr, SyntaxKind,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
SyntaxNode, SyntaxToken, T,
|
SyntaxNode, SyntaxToken, T,
|
||||||
};
|
};
|
||||||
|
@ -15,12 +15,12 @@ pub fn reindent(text: &str, indent: &str) -> String {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// If the node is on the beginning of the line, calculate indent.
|
/// If the node is on the beginning of the line, calculate indent.
|
||||||
pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
pub fn leading_indent(node: &SyntaxNode) -> Option<SmolStr> {
|
||||||
for token in prev_tokens(node.first_token()?) {
|
for token in prev_tokens(node.first_token()?) {
|
||||||
if let Some(ws) = ast::Whitespace::cast(token) {
|
if let Some(ws) = ast::Whitespace::cast(token.clone()) {
|
||||||
let ws_text = ws.text();
|
let ws_text = ws.text();
|
||||||
if let Some(pos) = ws_text.rfind('\n') {
|
if let Some(pos) = ws_text.rfind('\n') {
|
||||||
return Some(&ws_text[pos + 1..]);
|
return Some(ws_text[pos + 1..].into());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if token.text().contains('\n') {
|
if token.text().contains('\n') {
|
||||||
|
@ -31,17 +31,17 @@ pub fn leading_indent(node: &SyntaxNode) -> Option<&str> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
|
fn prev_tokens(token: SyntaxToken) -> impl Iterator<Item = SyntaxToken> {
|
||||||
successors(token.prev_token(), |&token| token.prev_token())
|
successors(token.prev_token(), |token| token.prev_token())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn extract_trivial_expression(block: &ast::Block) -> Option<&ast::Expr> {
|
pub fn extract_trivial_expression(block: &ast::Block) -> Option<ast::Expr> {
|
||||||
let expr = block.expr()?;
|
let expr = block.expr()?;
|
||||||
if expr.syntax().text().contains('\n') {
|
if expr.syntax().text().contains('\n') {
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
|
let non_trivial_children = block.syntax().children().filter(|it| match it.kind() {
|
||||||
WHITESPACE | T!['{'] | T!['}'] => false,
|
WHITESPACE | T!['{'] | T!['}'] => false,
|
||||||
_ => it != &expr.syntax(),
|
_ => it != expr.syntax(),
|
||||||
});
|
});
|
||||||
if non_trivial_children.count() > 0 {
|
if non_trivial_children.count() > 0 {
|
||||||
return None;
|
return None;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue