Skip to content

Commit

Permalink
Auto merge of #15857 - Young-Flash:fix, r=Veykril
Browse files Browse the repository at this point in the history
fix: remove parenthesis should ensure space

close #15844
  • Loading branch information
bors committed Nov 27, 2023
2 parents 9aa867c + bd5a63b commit 4ab6729
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions crates/ide-assists/src/handlers/remove_parentheses.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use syntax::{ast, AstNode};
use syntax::{ast, AstNode, SyntaxKind, T};

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

Expand Down Expand Up @@ -39,7 +39,19 @@ pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) ->
AssistId("remove_parentheses", AssistKind::Refactor),
"Remove redundant parentheses",
target,
|builder| builder.replace_ast(parens.into(), 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)
},
)
}

Expand All @@ -49,6 +61,15 @@ mod tests {

use super::*;

#[test]
fn remove_parens_space() {
check_assist(
remove_parentheses,
r#"fn f() { match$0(true) {} }"#,
r#"fn f() { match true {} }"#,
);
}

#[test]
fn remove_parens_simple() {
check_assist(remove_parentheses, r#"fn f() { $0(2) + 2; }"#, r#"fn f() { 2 + 2; }"#);
Expand Down Expand Up @@ -94,8 +115,8 @@ mod tests {
check_assist(remove_parentheses, r#"fn f() { f(($02 + 2)); }"#, r#"fn f() { f(2 + 2); }"#);
check_assist(
remove_parentheses,
r#"fn f() { (1<2)&&$0(3>4); }"#,
r#"fn f() { (1<2)&&3>4; }"#,
r#"fn f() { (1<2) &&$0(3>4); }"#,
r#"fn f() { (1<2) && 3>4; }"#,
);
}

Expand Down Expand Up @@ -164,8 +185,8 @@ mod tests {
fn remove_parens_weird_places() {
check_assist(
remove_parentheses,
r#"fn f() { match () { _=>$0(()) } }"#,
r#"fn f() { match () { _=>() } }"#,
r#"fn f() { match () { _ =>$0(()) } }"#,
r#"fn f() { match () { _ => () } }"#,
);

check_assist(
Expand Down

0 comments on commit 4ab6729

Please sign in to comment.