diff options
-rw-r--r-- | crates/assists/src/handlers/invert_if.rs | 9 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 4 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 3 |
3 files changed, 16 insertions, 0 deletions
diff --git a/crates/assists/src/handlers/invert_if.rs b/crates/assists/src/handlers/invert_if.rs index ea722b91b..91e2f5c8c 100644 --- a/crates/assists/src/handlers/invert_if.rs +++ b/crates/assists/src/handlers/invert_if.rs | |||
@@ -69,6 +69,15 @@ mod tests { | |||
69 | use crate::tests::{check_assist, check_assist_not_applicable}; | 69 | use crate::tests::{check_assist, check_assist_not_applicable}; |
70 | 70 | ||
71 | #[test] | 71 | #[test] |
72 | fn invert_if_composite_condition() { | ||
73 | check_assist( | ||
74 | invert_if, | ||
75 | "fn f() { i<|>f x == 3 || x == 4 || x == 5 { 1 } else { 3 * 2 } }", | ||
76 | "fn f() { if !(x == 3 || x == 4 || x == 5) { 3 * 2 } else { 1 } }", | ||
77 | ) | ||
78 | } | ||
79 | |||
80 | #[test] | ||
72 | fn invert_if_remove_inequality() { | 81 | fn invert_if_remove_inequality() { |
73 | check_assist( | 82 | check_assist( |
74 | invert_if, | 83 | invert_if, |
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 01f5c291f..f2cacf7c8 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -212,6 +212,10 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | |||
212 | ast::Expr::BinExpr(bin) => match bin.op_kind()? { | 212 | ast::Expr::BinExpr(bin) => match bin.op_kind()? { |
213 | ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), | 213 | ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), |
214 | ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), | 214 | ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), |
215 | // Parenthesize composite boolean expressions before prefixing `!` | ||
216 | ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => { | ||
217 | Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))) | ||
218 | } | ||
215 | _ => None, | 219 | _ => None, |
216 | }, | 220 | }, |
217 | ast::Expr::MethodCallExpr(mce) => { | 221 | ast::Expr::MethodCallExpr(mce) => { |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index cc09b77a5..16b079c42 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -196,6 +196,9 @@ pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgLis | |||
196 | pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { | 196 | pub fn expr_ref(expr: ast::Expr, exclusive: bool) -> ast::Expr { |
197 | expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) | 197 | expr_from_text(&if exclusive { format!("&mut {}", expr) } else { format!("&{}", expr) }) |
198 | } | 198 | } |
199 | pub fn expr_paren(expr: ast::Expr) -> ast::Expr { | ||
200 | expr_from_text(&format!("({})", expr)) | ||
201 | } | ||
199 | fn expr_from_text(text: &str) -> ast::Expr { | 202 | fn expr_from_text(text: &str) -> ast::Expr { |
200 | ast_from_text(&format!("const C: () = {};", text)) | 203 | ast_from_text(&format!("const C: () = {};", text)) |
201 | } | 204 | } |