2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast::{self, AstNode};
|
2021-03-03 06:12:00 -06:00
|
|
|
use test_utils::mark;
|
2019-10-03 15:48:35 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKind, Assists};
|
2019-10-03 15:48:35 -05:00
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: apply_demorgan
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2020-08-12 06:56:58 -05:00
|
|
|
// Apply https://en.wikipedia.org/wiki/De_Morgan%27s_laws[De Morgan's law].
|
2019-10-25 15:38:15 -05:00
|
|
|
// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
|
|
|
// This also works with `&&`. This assist can only be applied with the cursor
|
2021-02-19 07:48:07 -06:00
|
|
|
// on either `||` or `&&`.
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
|
|
|
// fn main() {
|
2021-02-22 09:23:42 -06:00
|
|
|
// if x != 4 ||$0 y < 3.14 {}
|
2019-10-25 15:38:15 -05:00
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
2021-02-22 09:23:42 -06:00
|
|
|
// if !(x == 4 && !(y < 3.14)) {}
|
2019-10-25 15:38:15 -05:00
|
|
|
// }
|
|
|
|
// ```
|
2020-05-06 11:45:35 -05:00
|
|
|
pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
|
2019-10-27 03:48:40 -05:00
|
|
|
let expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
|
2019-10-03 15:48:35 -05:00
|
|
|
let op = expr.op_kind()?;
|
|
|
|
let op_range = expr.op_token()?.text_range();
|
|
|
|
let opposite_op = opposite_logic_op(op)?;
|
2020-04-24 16:40:41 -05:00
|
|
|
let cursor_in_range = op_range.contains_range(ctx.frange.range);
|
2019-10-03 15:48:35 -05:00
|
|
|
if !cursor_in_range {
|
|
|
|
return None;
|
|
|
|
}
|
2020-02-07 05:07:38 -06:00
|
|
|
|
2019-11-23 23:14:57 -06:00
|
|
|
let lhs = expr.lhs()?;
|
|
|
|
let lhs_range = lhs.syntax().text_range();
|
2021-02-22 09:23:42 -06:00
|
|
|
let not_lhs = invert_boolean_expression(&ctx.sema, lhs);
|
2020-02-07 05:07:38 -06:00
|
|
|
|
2019-11-23 23:14:57 -06:00
|
|
|
let rhs = expr.rhs()?;
|
|
|
|
let rhs_range = rhs.syntax().text_range();
|
2021-02-22 09:23:42 -06:00
|
|
|
let not_rhs = invert_boolean_expression(&ctx.sema, rhs);
|
2019-10-03 15:48:35 -05:00
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
acc.add(
|
2020-07-02 16:48:35 -05:00
|
|
|
AssistId("apply_demorgan", AssistKind::RefactorRewrite),
|
2020-06-28 17:36:05 -05:00
|
|
|
"Apply De Morgan's law",
|
|
|
|
op_range,
|
|
|
|
|edit| {
|
2021-03-03 06:12:00 -06:00
|
|
|
let paren_expr = expr.syntax().parent().and_then(|parent| ast::ParenExpr::cast(parent));
|
|
|
|
|
|
|
|
let neg_expr = paren_expr
|
|
|
|
.clone()
|
|
|
|
.and_then(|paren_expr| paren_expr.syntax().parent())
|
|
|
|
.and_then(|parent| ast::PrefixExpr::cast(parent))
|
|
|
|
.and_then(|prefix_expr| {
|
|
|
|
if prefix_expr.op_kind().unwrap() == ast::PrefixOp::Not {
|
|
|
|
Some(prefix_expr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2020-06-28 17:36:05 -05:00
|
|
|
edit.replace(op_range, opposite_op);
|
2021-03-03 06:12:00 -06:00
|
|
|
|
|
|
|
if let Some(paren_expr) = paren_expr {
|
|
|
|
edit.replace(lhs_range, not_lhs.syntax().text());
|
|
|
|
edit.replace(rhs_range, not_rhs.syntax().text());
|
|
|
|
if let Some(neg_expr) = neg_expr {
|
|
|
|
mark::hit!(demorgan_double_negation);
|
|
|
|
edit.replace(neg_expr.op_token().unwrap().text_range(), "");
|
|
|
|
} else {
|
|
|
|
mark::hit!(demorgan_double_parens);
|
|
|
|
edit.replace(paren_expr.l_paren_token().unwrap().text_range(), "!(");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
|
|
|
|
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
|
|
|
|
}
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
2019-10-04 03:51:41 -05:00
|
|
|
// Return the opposite text for a given logical operator, if it makes sense
|
|
|
|
fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
|
|
|
|
match kind {
|
|
|
|
ast::BinOp::BooleanOr => Some("&&"),
|
|
|
|
ast::BinOp::BooleanAnd => Some("||"),
|
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:48:35 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2021-02-24 04:42:32 -06:00
|
|
|
use ide_db::helpers::FamousDefs;
|
2021-03-03 06:12:00 -06:00
|
|
|
use test_utils::mark;
|
2021-02-24 04:42:32 -06:00
|
|
|
|
2019-10-03 15:48:35 -05:00
|
|
|
use super::*;
|
|
|
|
|
2020-05-06 03:16:55 -05:00
|
|
|
use crate::tests::{check_assist, check_assist_not_applicable};
|
2019-10-03 15:48:35 -05:00
|
|
|
|
2021-02-24 04:42:32 -06:00
|
|
|
const ORDABLE_FIXTURE: &'static str = r"
|
|
|
|
//- /lib.rs deps:core crate:ordable
|
|
|
|
struct NonOrderable;
|
|
|
|
struct Orderable;
|
|
|
|
impl core::cmp::Ord for Orderable {}
|
|
|
|
";
|
|
|
|
|
|
|
|
fn check(ra_fixture_before: &str, ra_fixture_after: &str) {
|
|
|
|
let before = &format!(
|
|
|
|
"//- /main.rs crate:main deps:core,ordable\n{}\n{}{}",
|
|
|
|
ra_fixture_before,
|
|
|
|
FamousDefs::FIXTURE,
|
|
|
|
ORDABLE_FIXTURE
|
|
|
|
);
|
|
|
|
check_assist(apply_demorgan, before, &format!("{}\n", ra_fixture_after));
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_handles_leq() {
|
|
|
|
check(
|
|
|
|
r"use ordable::Orderable;
|
|
|
|
fn f() {
|
|
|
|
Orderable < Orderable &&$0 Orderable <= Orderable
|
|
|
|
}",
|
|
|
|
r"use ordable::Orderable;
|
|
|
|
fn f() {
|
|
|
|
!(Orderable >= Orderable || Orderable > Orderable)
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r"use ordable::NonOrderable;
|
|
|
|
fn f() {
|
|
|
|
NonOrderable < NonOrderable &&$0 NonOrderable <= NonOrderable
|
|
|
|
}",
|
|
|
|
r"use ordable::NonOrderable;
|
|
|
|
fn f() {
|
|
|
|
!(!(NonOrderable < NonOrderable) || !(NonOrderable <= NonOrderable))
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_handles_geq() {
|
|
|
|
check(
|
|
|
|
r"use ordable::Orderable;
|
|
|
|
fn f() {
|
|
|
|
Orderable > Orderable &&$0 Orderable >= Orderable
|
|
|
|
}",
|
|
|
|
r"use ordable::Orderable;
|
|
|
|
fn f() {
|
|
|
|
!(Orderable <= Orderable || Orderable < Orderable)
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
check(
|
|
|
|
r"use ordable::NonOrderable;
|
|
|
|
fn f() {
|
|
|
|
Orderable > Orderable &&$0 Orderable >= Orderable
|
|
|
|
}",
|
|
|
|
r"use ordable::NonOrderable;
|
|
|
|
fn f() {
|
|
|
|
!(!(Orderable > Orderable) || !(Orderable >= Orderable))
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-10-03 15:48:35 -05:00
|
|
|
#[test]
|
|
|
|
fn demorgan_turns_and_into_or() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { !x &&$0 !x }", "fn f() { !(x || x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_turns_or_into_and() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { !x ||$0 !x }", "fn f() { !(x && x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_removes_inequality() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { x != x ||$0 !x }", "fn f() { !(x == x && x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 05:07:38 -06:00
|
|
|
fn demorgan_general_case() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { x ||$0 x }", "fn f() { !(!x && !x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2020-02-07 05:07:38 -06:00
|
|
|
fn demorgan_doesnt_apply_with_cursor_not_on_op() {
|
2021-01-06 14:15:48 -06:00
|
|
|
check_assist_not_applicable(apply_demorgan, "fn f() { $0 !x || !x }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
2021-03-03 06:12:00 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_double_negation() {
|
|
|
|
mark::check!(demorgan_double_negation);
|
|
|
|
check_assist(apply_demorgan, "fn f() { !(x ||$0 x) }", "fn f() { (!x && !x) }")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_double_parens() {
|
|
|
|
mark::check!(demorgan_double_parens);
|
|
|
|
check_assist(apply_demorgan, "fn f() { (x ||$0 x) }", "fn f() { !(!x && !x) }")
|
|
|
|
}
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|