mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-28 12:55:11 +00:00
move parentheses judge logic into builder
This commit is contained in:
parent
be62e0bf08
commit
bd5a63b208
1 changed files with 13 additions and 16 deletions
|
@ -34,27 +34,24 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// we should use `find_node_at_offset` at `SourceFile` level to get expectant `Between`
|
|
||||||
let token_at_offset = ctx
|
|
||||||
.find_node_at_offset::<ast::SourceFile>()?
|
|
||||||
.syntax()
|
|
||||||
.token_at_offset(parens.syntax().text_range().start());
|
|
||||||
let need_to_add_ws = match token_at_offset {
|
|
||||||
syntax::TokenAtOffset::Between(before, _after) => {
|
|
||||||
// anyother `SyntaxKind` we missing here?
|
|
||||||
let tokens = vec![T![&], T![!], T!['('], T!['['], T!['{']];
|
|
||||||
before.kind() != SyntaxKind::WHITESPACE && !tokens.contains(&before.kind())
|
|
||||||
}
|
|
||||||
_ => false,
|
|
||||||
};
|
|
||||||
let expr = if need_to_add_ws { format!(" {}", expr) } else { expr.to_string() };
|
|
||||||
|
|
||||||
let target = parens.syntax().text_range();
|
let target = parens.syntax().text_range();
|
||||||
acc.add(
|
acc.add(
|
||||||
AssistId("remove_parentheses", AssistKind::Refactor),
|
AssistId("remove_parentheses", AssistKind::Refactor),
|
||||||
"Remove redundant parentheses",
|
"Remove redundant parentheses",
|
||||||
target,
|
target,
|
||||||
|builder| builder.replace(parens.syntax().text_range(), expr),
|
|builder| {
|
||||||
|
let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token());
|
||||||
|
let need_to_add_ws = match prev_token {
|
||||||
|
Some(it) => {
|
||||||
|
let tokens = vec![T![&], T![!], T!['('], T!['['], T!['{']];
|
||||||
|
it.kind() != SyntaxKind::WHITESPACE && !tokens.contains(&it.kind())
|
||||||
|
}
|
||||||
|
None => false,
|
||||||
|
};
|
||||||
|
let expr = if need_to_add_ws { format!(" {}", expr) } else { expr.to_string() };
|
||||||
|
|
||||||
|
builder.replace(parens.syntax().text_range(), expr)
|
||||||
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue