aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorlbrande <[email protected]>2021-02-19 13:48:07 +0000
committerLukas Wirth <[email protected]>2021-02-24 10:58:37 +0000
commitf7a4a87de2840789e12722afc7df9f4db2db013c (patch)
treecbc35e6f34db1216977269d7077d5a1889a41b86
parentaa38fa1c72673cb2470651025782fb0fcfad738c (diff)
De Morgan's Law assist now correctly parenthesizes binary expressions.
-rw-r--r--crates/ide_assists/src/handlers/apply_demorgan.rs7
-rw-r--r--crates/ide_assists/src/tests/generated.rs4
-rw-r--r--crates/ide_assists/src/utils.rs7
3 files changed, 7 insertions, 11 deletions
diff --git a/crates/ide_assists/src/handlers/apply_demorgan.rs b/crates/ide_assists/src/handlers/apply_demorgan.rs
index ed4d11455..e478ff2ce 100644
--- a/crates/ide_assists/src/handlers/apply_demorgan.rs
+++ b/crates/ide_assists/src/handlers/apply_demorgan.rs
@@ -7,18 +7,17 @@ use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKin
7// Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law]. 7// Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law].
8// This transforms expressions of the form `!l || !r` into `!(l && r)`. 8// This transforms expressions of the form `!l || !r` into `!(l && r)`.
9// This also works with `&&`. This assist can only be applied with the cursor 9// This also works with `&&`. This assist can only be applied with the cursor
10// on either `||` or `&&`, with both operands being a negation of some kind. 10// on either `||` or `&&`.
11// This means something of the form `!x` or `x != y`.
12// 11//
13// ``` 12// ```
14// fn main() { 13// fn main() {
15// if x != 4 ||$0 !y {} 14// if x != 4 ||$0 y < 3 {}
16// } 15// }
17// ``` 16// ```
18// -> 17// ->
19// ``` 18// ```
20// fn main() { 19// fn main() {
21// if !(x == 4 && y) {} 20// if !(x == 4 && !(y < 3)) {}
22// } 21// }
23// ``` 22// ```
24pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> { 23pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
diff --git a/crates/ide_assists/src/tests/generated.rs b/crates/ide_assists/src/tests/generated.rs
index 0516deaff..ee9c870e8 100644
--- a/crates/ide_assists/src/tests/generated.rs
+++ b/crates/ide_assists/src/tests/generated.rs
@@ -147,12 +147,12 @@ fn doctest_apply_demorgan() {
147 "apply_demorgan", 147 "apply_demorgan",
148 r#####" 148 r#####"
149fn main() { 149fn main() {
150 if x != 4 ||$0 !y {} 150 if x != 4 ||$0 y < 3 {}
151} 151}
152"#####, 152"#####,
153 r#####" 153 r#####"
154fn main() { 154fn main() {
155 if !(x == 4 && y) {} 155 if !(x == 4 && !(y < 3)) {}
156} 156}
157"#####, 157"#####,
158 ) 158 )
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index 0074da741..cd026d432 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -217,11 +217,8 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
217 ast::Expr::BinExpr(bin) => match bin.op_kind()? { 217 ast::Expr::BinExpr(bin) => match bin.op_kind()? {
218 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), 218 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
219 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), 219 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
220 // Parenthesize composite boolean expressions before prefixing `!` 220 // Parenthesize other expressions before prefixing `!`
221 ast::BinOp::BooleanAnd | ast::BinOp::BooleanOr => { 221 _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
222 Some(make::expr_prefix(T![!], make::expr_paren(expr.clone())))
223 }
224 _ => None,
225 }, 222 },
226 ast::Expr::MethodCallExpr(mce) => { 223 ast::Expr::MethodCallExpr(mce) => {
227 let receiver = mce.receiver()?; 224 let receiver = mce.receiver()?;