mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-24 17:16:25 +00:00
internal: Migrate remove_parentheses assist to SyntaxEditor
This commit is contained in:
parent
daca6b88c4
commit
6dda2e81c6
2 changed files with 14 additions and 5 deletions
|
|
@ -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};
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
||||||
|
|
||||||
|
|
@ -40,6 +44,7 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||||
"Remove redundant parentheses",
|
"Remove redundant parentheses",
|
||||||
target,
|
target,
|
||||||
|builder| {
|
|builder| {
|
||||||
|
let mut editor = builder.make_editor(parens.syntax());
|
||||||
let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token());
|
let prev_token = parens.syntax().first_token().and_then(|it| it.prev_token());
|
||||||
let need_to_add_ws = match prev_token {
|
let need_to_add_ws = match prev_token {
|
||||||
Some(it) => {
|
Some(it) => {
|
||||||
|
|
@ -48,9 +53,13 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
|
||||||
}
|
}
|
||||||
None => false,
|
None => false,
|
||||||
};
|
};
|
||||||
let expr = if need_to_add_ws { format!(" {expr}") } else { expr.to_string() };
|
if need_to_add_ws {
|
||||||
|
let make = SyntaxFactory::new();
|
||||||
builder.replace(parens.syntax().text_range(), expr)
|
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);
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2974,7 +2974,7 @@ impl Walrus {
|
||||||
|
|
||||||
|
|
||||||
### `remove_parentheses`
|
### `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.
|
Removes redundant parentheses.
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue