2021-03-05 15:15:49 -06:00
|
|
|
use std::collections::VecDeque;
|
|
|
|
|
2020-08-12 11:26:51 -05:00
|
|
|
use syntax::ast::{self, AstNode};
|
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-08-14 08:40:00 -05: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();
|
2021-08-14 10:08:31 -05:00
|
|
|
|
|
|
|
let opposite_op = match op {
|
|
|
|
ast::BinaryOp::LogicOp(ast::LogicOp::And) => "||",
|
|
|
|
ast::BinaryOp::LogicOp(ast::LogicOp::Or) => "&&",
|
|
|
|
_ => return None,
|
|
|
|
};
|
|
|
|
|
2021-10-12 07:41:59 -05:00
|
|
|
let cursor_in_range = op_range.contains_range(ctx.selection_trimmed());
|
2019-10-03 15:48:35 -05:00
|
|
|
if !cursor_in_range {
|
|
|
|
return None;
|
|
|
|
}
|
2020-02-07 05:07:38 -06:00
|
|
|
|
2021-03-05 15:15:49 -06:00
|
|
|
let mut expr = expr;
|
|
|
|
|
|
|
|
// Walk up the tree while we have the same binary operator
|
|
|
|
while let Some(parent_expr) = expr.syntax().parent().and_then(ast::BinExpr::cast) {
|
2022-01-05 15:30:19 -06:00
|
|
|
match expr.op_kind() {
|
|
|
|
Some(parent_op) if parent_op == op => {
|
|
|
|
expr = parent_expr;
|
2021-03-05 15:15:49 -06:00
|
|
|
}
|
2022-01-05 15:30:19 -06:00
|
|
|
_ => break,
|
2021-03-05 15:15:49 -06:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut expr_stack = vec![expr.clone()];
|
|
|
|
let mut terms = Vec::new();
|
|
|
|
let mut op_ranges = Vec::new();
|
|
|
|
|
|
|
|
// Find all the children with the same binary operator
|
|
|
|
while let Some(expr) = expr_stack.pop() {
|
|
|
|
let mut traverse_bin_expr_arm = |expr| {
|
|
|
|
if let ast::Expr::BinExpr(bin_expr) = expr {
|
|
|
|
if let Some(expr_op) = bin_expr.op_kind() {
|
|
|
|
if expr_op == op {
|
|
|
|
expr_stack.push(bin_expr);
|
|
|
|
} else {
|
|
|
|
terms.push(ast::Expr::BinExpr(bin_expr));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
terms.push(ast::Expr::BinExpr(bin_expr));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
terms.push(expr);
|
|
|
|
}
|
|
|
|
};
|
2020-02-07 05:07:38 -06:00
|
|
|
|
2021-03-05 15:15:49 -06:00
|
|
|
op_ranges.extend(expr.op_token().map(|t| t.text_range()));
|
|
|
|
traverse_bin_expr_arm(expr.lhs()?);
|
|
|
|
traverse_bin_expr_arm(expr.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-05 15:15:49 -06:00
|
|
|
terms.sort_by_key(|t| t.syntax().text_range().start());
|
|
|
|
let mut terms = VecDeque::from(terms);
|
|
|
|
|
2021-06-12 22:59:36 -05:00
|
|
|
let paren_expr = expr.syntax().parent().and_then(ast::ParenExpr::cast);
|
2021-03-03 06:12:00 -06:00
|
|
|
|
|
|
|
let neg_expr = paren_expr
|
|
|
|
.clone()
|
|
|
|
.and_then(|paren_expr| paren_expr.syntax().parent())
|
2021-06-12 22:59:36 -05:00
|
|
|
.and_then(ast::PrefixExpr::cast)
|
2021-03-03 06:12:00 -06:00
|
|
|
.and_then(|prefix_expr| {
|
2021-08-14 09:01:28 -05:00
|
|
|
if prefix_expr.op_kind().unwrap() == ast::UnaryOp::Not {
|
2021-03-03 06:12:00 -06:00
|
|
|
Some(prefix_expr)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-05 15:15:49 -06:00
|
|
|
for op_range in op_ranges {
|
|
|
|
edit.replace(op_range, opposite_op);
|
|
|
|
}
|
2021-03-03 06:12:00 -06:00
|
|
|
|
|
|
|
if let Some(paren_expr) = paren_expr {
|
2021-03-05 15:15:49 -06:00
|
|
|
for term in terms {
|
|
|
|
let range = term.syntax().text_range();
|
2021-08-14 08:40:00 -05:00
|
|
|
let not_term = invert_boolean_expression(term);
|
2021-03-05 15:15:49 -06:00
|
|
|
|
|
|
|
edit.replace(range, not_term.syntax().text());
|
|
|
|
}
|
|
|
|
|
2021-03-03 06:12:00 -06:00
|
|
|
if let Some(neg_expr) = neg_expr {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(demorgan_double_negation);
|
2021-03-03 06:12:00 -06:00
|
|
|
edit.replace(neg_expr.op_token().unwrap().text_range(), "");
|
|
|
|
} else {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::hit!(demorgan_double_parens);
|
2021-03-03 06:12:00 -06:00
|
|
|
edit.replace(paren_expr.l_paren_token().unwrap().text_range(), "!(");
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-05 15:15:49 -06:00
|
|
|
if let Some(lhs) = terms.pop_front() {
|
|
|
|
let lhs_range = lhs.syntax().text_range();
|
2021-08-14 08:40:00 -05:00
|
|
|
let not_lhs = invert_boolean_expression(lhs);
|
2021-03-05 15:15:49 -06:00
|
|
|
|
|
|
|
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
|
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(rhs) = terms.pop_back() {
|
|
|
|
let rhs_range = rhs.syntax().text_range();
|
2021-08-14 08:40:00 -05:00
|
|
|
let not_rhs = invert_boolean_expression(rhs);
|
2021-03-05 15:15:49 -06:00
|
|
|
|
|
|
|
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
|
|
|
|
}
|
|
|
|
|
|
|
|
for term in terms {
|
|
|
|
let term_range = term.syntax().text_range();
|
2021-08-14 08:40:00 -05:00
|
|
|
let not_term = invert_boolean_expression(term);
|
2021-03-05 15:15:49 -06:00
|
|
|
edit.replace(term_range, not_term.syntax().text());
|
|
|
|
}
|
2021-03-03 06:12:00 -06:00
|
|
|
}
|
2020-06-28 17:36:05 -05:00
|
|
|
},
|
|
|
|
)
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
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-06-17 16:30:22 -05:00
|
|
|
use super::*;
|
2021-02-24 04:42:32 -06:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_handles_leq() {
|
2021-06-17 16:30:22 -05:00
|
|
|
check_assist(
|
|
|
|
apply_demorgan,
|
|
|
|
r#"
|
|
|
|
struct S;
|
2021-08-14 08:40:00 -05:00
|
|
|
fn f() { S < S &&$0 S <= S }
|
2021-06-17 16:30:22 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
2021-08-14 08:40:00 -05:00
|
|
|
fn f() { !(S >= S || S > S) }
|
2021-06-17 16:30:22 -05:00
|
|
|
"#,
|
2021-02-24 04:42:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_handles_geq() {
|
2021-06-17 16:30:22 -05:00
|
|
|
check_assist(
|
|
|
|
apply_demorgan,
|
|
|
|
r#"
|
|
|
|
struct S;
|
2021-08-14 08:40:00 -05:00
|
|
|
fn f() { S > S &&$0 S >= S }
|
2021-06-17 16:30:22 -05:00
|
|
|
"#,
|
|
|
|
r#"
|
|
|
|
struct S;
|
2021-08-14 08:40:00 -05:00
|
|
|
fn f() { !(S <= S || S < S) }
|
2021-06-17 16:30:22 -05:00
|
|
|
"#,
|
2021-02-24 04:42:32 -06:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2021-03-05 15:15:49 -06:00
|
|
|
#[test]
|
|
|
|
fn demorgan_multiple_terms() {
|
|
|
|
check_assist(apply_demorgan, "fn f() { x ||$0 y || z }", "fn f() { !(!x && !y && !z) }");
|
|
|
|
check_assist(apply_demorgan, "fn f() { x || y ||$0 z }", "fn f() { !(!x && !y && !z) }");
|
|
|
|
}
|
|
|
|
|
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() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(demorgan_double_negation);
|
2021-03-03 06:12:00 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { !(x ||$0 x) }", "fn f() { (!x && !x) }")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_double_parens() {
|
2021-03-08 14:19:44 -06:00
|
|
|
cov_mark::check!(demorgan_double_parens);
|
2021-03-03 06:12:00 -06:00
|
|
|
check_assist(apply_demorgan, "fn f() { (x ||$0 x) }", "fn f() { !(!x && !x) }")
|
|
|
|
}
|
2022-01-05 15:30:19 -06:00
|
|
|
|
|
|
|
// https://github.com/rust-analyzer/rust-analyzer/issues/10963
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_hang() {
|
|
|
|
check_assist(
|
|
|
|
apply_demorgan,
|
|
|
|
"fn f() { 1 || 3 &&$0 4 || 5 }",
|
|
|
|
"fn f() { !(!1 || !3 || !4) || 5 }",
|
|
|
|
)
|
|
|
|
}
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|