From aa1234e02b1166c57dd2a3cd27fd0b0b3c6cba7e Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 12:07:38 +0100 Subject: Generalize invert_if to just always work --- crates/ra_assists/src/assists/apply_demorgan.rs | 14 ++++++----- crates/ra_assists/src/assists/invert_if.rs | 33 ++++++++++++++++--------- 2 files changed, 30 insertions(+), 17 deletions(-) (limited to 'crates/ra_assists/src/assists') diff --git a/crates/ra_assists/src/assists/apply_demorgan.rs b/crates/ra_assists/src/assists/apply_demorgan.rs index dac6280ad..ba08a8223 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs @@ -31,12 +31,14 @@ pub(crate) fn apply_demorgan(ctx: AssistCtx) -> Option { if !cursor_in_range { return None; } + let lhs = expr.lhs()?; let lhs_range = lhs.syntax().text_range(); + let not_lhs = invert_boolean_expression(lhs); + 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)?; + let not_rhs = invert_boolean_expression(rhs); ctx.add_assist(AssistId("apply_demorgan"), "Apply De Morgan's law", |edit| { edit.target(op_range); @@ -77,12 +79,12 @@ mod tests { } #[test] - fn demorgan_doesnt_apply_with_cursor_not_on_op() { - check_assist_not_applicable(apply_demorgan, "fn f() { <|> !x || !x }") + fn demorgan_general_case() { + check_assist(apply_demorgan, "fn f() { x ||<|> x }", "fn f() { !(!x &&<|> !x) }") } #[test] - fn demorgan_doesnt_apply_when_operands_arent_negated_already() { - check_assist_not_applicable(apply_demorgan, "fn f() { x ||<|> x }") + fn demorgan_doesnt_apply_with_cursor_not_on_op() { + check_assist_not_applicable(apply_demorgan, "fn f() { <|> !x || !x }") } } diff --git a/crates/ra_assists/src/assists/invert_if.rs b/crates/ra_assists/src/assists/invert_if.rs index 694c3642c..983392f21 100644 --- a/crates/ra_assists/src/assists/invert_if.rs +++ b/crates/ra_assists/src/assists/invert_if.rs @@ -1,4 +1,4 @@ -use ra_syntax::ast::{self, AstNode}; +use ra_syntax::ast::{self, make, AstNode}; use ra_syntax::T; use crate::{Assist, AssistCtx, AssistId}; @@ -35,8 +35,8 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option { let then_node = expr.then_branch()?.syntax().clone(); if let ast::ElseBranch::Block(else_block) = expr.else_branch()? { - let flip_cond = invert_boolean_expression(&cond)?; let cond_range = cond.syntax().text_range(); + let flip_cond = invert_boolean_expression(cond); let else_node = else_block.syntax(); let else_range = else_node.text_range(); let then_range = then_node.text_range(); @@ -51,16 +51,23 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option { None } -pub(crate) fn invert_boolean_expression(expr: &ast::Expr) -> Option { +pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { + if let Some(expr) = invert_special_case(&expr) { + return expr; + } + make::expr_prefix(T![!], expr) +} + +pub(crate) fn invert_special_case(expr: &ast::Expr) -> Option { match expr { ast::Expr::BinExpr(bin) => match bin.op_kind()? { ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), + ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), _ => None, }, - ast::Expr::PrefixExpr(pe) => match pe.op_kind()? { - ast::PrefixOp::Not => pe.expr(), - _ => None, - }, + ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), + // FIXME: + // ast::Expr::Literal(true | false ) _ => None, } } @@ -90,12 +97,16 @@ mod tests { } #[test] - fn invert_if_doesnt_apply_with_cursor_not_on_if() { - check_assist_not_applicable(invert_if, "fn f() { if !<|>cond { 3 * 2 } else { 1 } }") + fn invert_if_general_case() { + check_assist( + invert_if, + "fn f() { i<|>f cond { 3 * 2 } else { 1 } }", + "fn f() { i<|>f !cond { 1 } else { 3 * 2 } }", + ) } #[test] - fn invert_if_doesnt_apply_without_negated() { - check_assist_not_applicable(invert_if, "fn f() { i<|>f cond { 3 * 2 } else { 1 } }") + fn invert_if_doesnt_apply_with_cursor_not_on_if() { + check_assist_not_applicable(invert_if, "fn f() { if !<|>cond { 3 * 2 } else { 1 } }") } } -- cgit v1.2.3