diff --git a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_parentheses.rs b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_parentheses.rs index f74fc2611282..143d5e542428 100644 --- a/src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_parentheses.rs +++ b/src/tools/rust-analyzer/crates/ide-assists/src/handlers/remove_parentheses.rs @@ -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); }, ) } diff --git a/src/tools/rust-analyzer/docs/book/src/assists_generated.md b/src/tools/rust-analyzer/docs/book/src/assists_generated.md index 52da98dbd21d..a05536e2d2d5 100644 --- a/src/tools/rust-analyzer/docs/book/src/assists_generated.md +++ b/src/tools/rust-analyzer/docs/book/src/assists_generated.md @@ -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.