2019-11-23 23:14:57 -06:00
|
|
|
use super::invert_if::invert_boolean_expression;
|
2019-10-04 03:51:41 -05:00
|
|
|
use ra_syntax::ast::{self, AstNode};
|
2019-10-03 15:48:35 -05:00
|
|
|
|
|
|
|
use crate::{Assist, AssistCtx, AssistId};
|
|
|
|
|
2019-10-25 15:38:15 -05:00
|
|
|
// Assist: apply_demorgan
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// Apply [De Morgan's law](https://en.wikipedia.org/wiki/De_Morgan%27s_laws).
|
|
|
|
// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
|
|
|
// This also works with `&&`. This assist can only be applied with the cursor
|
|
|
|
// on either `||` or `&&`, with both operands being a negation of some kind.
|
|
|
|
// This means something of the form `!x` or `x != y`.
|
2019-10-26 09:27:47 -05:00
|
|
|
//
|
2019-10-25 15:38:15 -05:00
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// if x != 4 ||<|> !y {}
|
|
|
|
// }
|
|
|
|
// ```
|
|
|
|
// ->
|
|
|
|
// ```
|
|
|
|
// fn main() {
|
|
|
|
// if !(x == 4 && y) {}
|
|
|
|
// }
|
|
|
|
// ```
|
2020-02-06 09:58:57 -06:00
|
|
|
pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option<Assist> {
|
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)?;
|
|
|
|
let cursor_in_range = ctx.frange.range.is_subrange(&op_range);
|
|
|
|
if !cursor_in_range {
|
|
|
|
return None;
|
|
|
|
}
|
2019-11-23 23:14:57 -06:00
|
|
|
let lhs = expr.lhs()?;
|
|
|
|
let lhs_range = lhs.syntax().text_range();
|
|
|
|
let rhs = expr.rhs()?;
|
|
|
|
let rhs_range = rhs.syntax().text_range();
|
|
|
|
let not_lhs = invert_boolean_expression(&lhs)?;
|
|
|
|
let not_rhs = invert_boolean_expression(&rhs)?;
|
2019-10-03 15:48:35 -05:00
|
|
|
|
2020-01-14 11:32:26 -06:00
|
|
|
ctx.add_assist(AssistId("apply_demorgan"), "Apply De Morgan's law", |edit| {
|
2019-10-03 15:48:35 -05:00
|
|
|
edit.target(op_range);
|
|
|
|
edit.replace(op_range, opposite_op);
|
2019-11-23 23:14:57 -06:00
|
|
|
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
|
|
|
|
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
|
2019-10-27 09:35:37 -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 {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
use crate::helpers::{check_assist, check_assist_not_applicable};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_turns_and_into_or() {
|
2019-10-04 01:21:24 -05:00
|
|
|
check_assist(apply_demorgan, "fn f() { !x &&<|> !x }", "fn f() { !(x ||<|> x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_turns_or_into_and() {
|
2019-10-04 01:21:24 -05:00
|
|
|
check_assist(apply_demorgan, "fn f() { !x ||<|> !x }", "fn f() { !(x &&<|> x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_removes_inequality() {
|
2019-10-04 01:21:24 -05:00
|
|
|
check_assist(apply_demorgan, "fn f() { x != x ||<|> !x }", "fn f() { !(x == x &&<|> x) }")
|
2019-10-03 15:48:35 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_apply_with_cursor_not_on_op() {
|
|
|
|
check_assist_not_applicable(apply_demorgan, "fn f() { <|> !x || !x }")
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn demorgan_doesnt_apply_when_operands_arent_negated_already() {
|
|
|
|
check_assist_not_applicable(apply_demorgan, "fn f() { x ||<|> x }")
|
|
|
|
}
|
|
|
|
}
|