2022-12-08 12:54:08 -06:00
|
|
|
use syntax::{ast, AstNode};
|
2022-12-06 10:18:25 -06:00
|
|
|
|
|
|
|
use crate::{AssistContext, AssistId, AssistKind, Assists};
|
|
|
|
|
|
|
|
// Assist: remove_parentheses
|
|
|
|
//
|
2022-12-08 12:22:57 -06:00
|
|
|
// Removes redundant parentheses.
|
2022-12-06 10:18:25 -06:00
|
|
|
//
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// _ = $0(2) + 2;
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// _ = 2 + 2;
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
pub(crate) fn remove_parentheses(acc: &mut Assists, ctx: &AssistContext<'_>) -> Option<()> {
|
|
|
|
let parens = ctx.find_node_at_offset::<ast::ParenExpr>()?;
|
|
|
|
|
2022-12-08 12:54:08 -06:00
|
|
|
let cursor_in_range =
|
|
|
|
parens.l_paren_token()?.text_range().contains_range(ctx.selection_trimmed())
|
|
|
|
|| parens.r_paren_token()?.text_range().contains_range(ctx.selection_trimmed());
|
2022-12-06 10:18:25 -06:00
|
|
|
if !cursor_in_range {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2022-12-06 13:11:24 -06:00
|
|
|
let expr = parens.expr()?;
|
2022-12-08 12:54:08 -06:00
|
|
|
|
2022-12-06 13:11:24 -06:00
|
|
|
let parent = ast::Expr::cast(parens.syntax().parent()?);
|
2022-12-08 12:44:03 -06:00
|
|
|
let is_ok_to_remove = expr.precedence() >= parent.as_ref().and_then(ast::Expr::precedence);
|
2022-12-06 13:11:24 -06:00
|
|
|
if !is_ok_to_remove {
|
|
|
|
return None;
|
|
|
|
}
|
2022-12-06 10:18:25 -06:00
|
|
|
|
|
|
|
let target = parens.syntax().text_range();
|
|
|
|
acc.add(
|
|
|
|
AssistId("remove_parentheses", AssistKind::Refactor),
|
2022-12-08 12:22:57 -06:00
|
|
|
"Remove redundant parentheses",
|
2022-12-06 10:18:25 -06:00
|
|
|
target,
|
2022-12-08 12:54:08 -06:00
|
|
|
|builder| builder.replace_ast(parens.into(), expr),
|
2022-12-06 10:18:25 -06:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_parens_simple() {
|
|
|
|
check_assist(remove_parentheses, r#"fn f() { $0(2) + 2; }"#, r#"fn f() { 2 + 2; }"#);
|
|
|
|
check_assist(remove_parentheses, r#"fn f() { ($02) + 2; }"#, r#"fn f() { 2 + 2; }"#);
|
|
|
|
check_assist(remove_parentheses, r#"fn f() { (2)$0 + 2; }"#, r#"fn f() { 2 + 2; }"#);
|
|
|
|
check_assist(remove_parentheses, r#"fn f() { (2$0) + 2; }"#, r#"fn f() { 2 + 2; }"#);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2022-12-06 13:11:24 -06:00
|
|
|
fn remove_parens_precedence() {
|
2022-12-06 10:18:25 -06:00
|
|
|
check_assist(
|
|
|
|
remove_parentheses,
|
2022-12-06 13:11:24 -06:00
|
|
|
r#"fn f() { $0(2 * 3) + 1; }"#,
|
|
|
|
r#"fn f() { 2 * 3 + 1; }"#,
|
2022-12-06 10:18:25 -06:00
|
|
|
);
|
2022-12-06 13:11:24 -06:00
|
|
|
check_assist(remove_parentheses, r#"fn f() { ( $0(2) ); }"#, r#"fn f() { ( 2 ); }"#);
|
|
|
|
check_assist(remove_parentheses, r#"fn f() { $0(2?)?; }"#, r#"fn f() { 2??; }"#);
|
|
|
|
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; }"#,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_parens_doesnt_apply_precedence() {
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() { $0(2 + 2) * 8; }"#);
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() { $0(2 + 2).f(); }"#);
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() { $0(2 + 2).await; }"#);
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() { $0!(2..2); }"#);
|
2022-12-06 10:18:25 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn remove_parens_doesnt_apply_with_cursor_not_on_paren() {
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() { (2 +$0 2) }"#);
|
|
|
|
check_assist_not_applicable(remove_parentheses, r#"fn f() {$0 (2 + 2) }"#);
|
|
|
|
}
|
|
|
|
}
|