internal: Migrate remove_parentheses assist to SyntaxEditor

This commit is contained in:
Giga Bowser 2024-12-04 19:51:15 -05:00
parent daca6b88c4
commit 6dda2e81c6
2 changed files with 14 additions and 5 deletions

View file

@ -1,4 +1,8 @@
use syntax::{ast, AstNode, SyntaxKind, T};
use syntax::{
ast::{self, syntax_factory::SyntaxFactory},
syntax_editor::Position,
AstNode, SyntaxKind, T,
};
use crate::{AssistContext, AssistId, AssistKind, Assists};
@ -40,6 +44,7 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
"Remove redundant parentheses",
target,
|builder| {
let mut editor = builder.make_editor(parens.syntax());
let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token());
let need_to_add_ws = match prev_token {
Some(it) => {
@ -48,9 +53,13 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
}
None => false,
};
let expr = if need_to_add_ws { format!(" {expr}") } else { expr.to_string() };
builder.replace(parens.syntax().text_range(), expr)
if need_to_add_ws {
let make = SyntaxFactory::new();
editor.insert(Position::before(parens.syntax()), make.whitespace(" "));
editor.add_mappings(make.finish_with_mappings());
}
editor.replace(parens.syntax(), expr.syntax());
builder.add_file_edits(ctx.file_id(), editor);
},
)
}

View file

@ -2974,7 +2974,7 @@ impl Walrus {
### `remove_parentheses`
**Source:** [remove_parentheses.rs](https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/remove_parentheses.rs#L5)
**Source:** [remove_parentheses.rs](https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/remove_parentheses.rs#L9)
Removes redundant parentheses.