rust/crates/ide_assists/src/handlers/apply_demorgan.rs

93 lines
2.6 KiB
Rust
Raw Normal View History

2020-08-12 11:26:51 -05:00
use syntax::ast::{self, AstNode};
2020-06-28 17:36:05 -05:00
use crate::{utils::invert_boolean_expression, AssistContext, AssistId, AssistKind, Assists};
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
// on either `||` or `&&`.
2019-10-26 09:27:47 -05:00
//
2019-10-25 15:38:15 -05:00
// ```
// fn main() {
// if x != 4 ||$0 y < 3 {}
2019-10-25 15:38:15 -05:00
// }
// ```
// ->
// ```
// fn main() {
// if !(x == 4 && !(y < 3)) {}
2019-10-25 15:38:15 -05:00
// }
// ```
pub(crate) fn apply_demorgan(acc: &mut Assists, ctx: &AssistContext) -> Option<()> {
let expr = ctx.find_node_at_offset::<ast::BinExpr>()?;
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);
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 not_lhs = invert_boolean_expression(lhs);
2019-11-23 23:14:57 -06:00
let rhs = expr.rhs()?;
let rhs_range = rhs.syntax().text_range();
let not_rhs = invert_boolean_expression(rhs);
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| {
edit.replace(op_range, opposite_op);
edit.replace(lhs_range, format!("!({}", not_lhs.syntax().text()));
edit.replace(rhs_range, format!("{})", not_rhs.syntax().text()));
},
)
}
// 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,
}
}
#[cfg(test)]
mod tests {
use super::*;
2020-05-06 03:16:55 -05:00
use crate::tests::{check_assist, check_assist_not_applicable};
#[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) }")
}
#[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) }")
}
#[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) }")
}
#[test]
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) }")
}
#[test]
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 }")
}
}