Skip to content

Commit 8036e84

Browse files
committed
internal: Migrate remove_parentheses assist to SyntaxEditor
1 parent cac5153 commit 8036e84

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_parentheses.rs

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
use syntax::{ast, AstNode, SyntaxKind, T};
1+
use syntax::{
2+
ast::{self, syntax_factory::SyntaxFactory},
3+
syntax_editor::Position,
4+
AstNode, SyntaxKind, T,
5+
};
26

37
use crate::{AssistContext, AssistId, AssistKind, Assists};
48

@@ -40,6 +44,7 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4044
"Remove redundant parentheses",
4145
target,
4246
|builder| {
47+
let mut editor = builder.make_editor(parens.syntax());
4348
let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token());
4449
let need_to_add_ws = match prev_token {
4550
Some(it) => {
@@ -48,9 +53,13 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
4853
}
4954
None => false,
5055
};
51-
let expr = if need_to_add_ws { format!(" {expr}") } else { expr.to_string() };
52-
53-
builder.replace(parens.syntax().text_range(), expr)
56+
if need_to_add_ws {
57+
let make = SyntaxFactory::new();
58+
editor.insert(Position::before(parens.syntax()), make.whitespace(" "));
59+
editor.add_mappings(make.finish_with_mappings());
60+
}
61+
editor.replace(parens.syntax(), expr.syntax());
62+
builder.add_file_edits(ctx.file_id(), editor);
5463
},
5564
)
5665
}

src/tools/rust-analyzer/docs/book/src/assists_generated.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2974,7 +2974,7 @@ impl Walrus {
29742974

29752975

29762976
### `remove_parentheses`
2977-
**Source:** [remove_parentheses.rs](https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/remove_parentheses.rs#L5)
2977+
**Source:** [remove_parentheses.rs](https://github.com/rust-lang/rust-analyzer/blob/master/crates/ide-assists/src/handlers/remove_parentheses.rs#L9)
29782978

29792979
Removes redundant parentheses.
29802980

0 commit comments

Comments
 (0)