From e17243d69893c7bba29ea5727154cb1d521fe9c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=BAc=C3=A1s=20Meier?= Date: Fri, 4 Oct 2019 10:51:41 +0200 Subject: [#1807] Refactor file structure Use the more conventional way of importing the ast types, and put the assist at the top of the file. --- crates/ra_assists/src/assists/apply_demorgan.rs | 66 ++++++++++++------------- 1 file changed, 33 insertions(+), 33 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 caecc50cc..5f2b0dd18 100644 --- a/crates/ra_assists/src/assists/apply_demorgan.rs +++ b/crates/ra_assists/src/assists/apply_demorgan.rs @@ -2,42 +2,11 @@ //! This assist transforms boolean expressions of the form `!a || !b` into //! `!(a && b)`. use hir::db::HirDatabase; -use ra_syntax::ast::{AstNode, BinExpr, BinOp, Expr, PrefixOp}; +use ra_syntax::ast::{self, AstNode}; use ra_syntax::SyntaxNode; use crate::{Assist, AssistCtx, AssistId}; -// Return the opposite text for a given logical operator, if it makes sense -fn opposite_logic_op(kind: BinOp) -> Option<&'static str> { - match kind { - BinOp::BooleanOr => Some("&&"), - BinOp::BooleanAnd => Some("||"), - _ => None, - } -} - -// This function tries to undo unary negation, or inequality -fn undo_negation(node: SyntaxNode) -> Option { - match Expr::cast(node)? { - Expr::BinExpr(bin) => match bin.op_kind()? { - BinOp::NegatedEqualityTest => { - let lhs = bin.lhs()?.syntax().text(); - let rhs = bin.rhs()?.syntax().text(); - Some(format!("{} == {}", lhs, rhs)) - } - _ => None, - }, - Expr::PrefixExpr(pe) => match pe.op_kind()? { - PrefixOp::Not => { - let child = pe.expr()?.syntax().text(); - Some(String::from(child)) - } - _ => None, - }, - _ => None, - } -} - /// Assist for applying demorgan's law /// /// This transforms expressions of the form `!l || !r` into `!(l && r)`. @@ -45,7 +14,7 @@ fn undo_negation(node: SyntaxNode) -> Option { /// on either `||` or `&&`, with both operands being a negation of some kind. /// This means something of the form `!x` or `x != y`. pub(crate) fn apply_demorgan(mut ctx: AssistCtx) -> Option { - let expr = ctx.node_at_offset::()?; + let expr = ctx.node_at_offset::()?; let op = expr.op_kind()?; let op_range = expr.op_token()?.text_range(); let opposite_op = opposite_logic_op(op)?; @@ -69,6 +38,37 @@ pub(crate) fn apply_demorgan(mut ctx: AssistCtx) -> Option Option<&'static str> { + match kind { + ast::BinOp::BooleanOr => Some("&&"), + ast::BinOp::BooleanAnd => Some("||"), + _ => None, + } +} + +// This function tries to undo unary negation, or inequality +fn undo_negation(node: SyntaxNode) -> Option { + match ast::Expr::cast(node)? { + ast::Expr::BinExpr(bin) => match bin.op_kind()? { + ast::BinOp::NegatedEqualityTest => { + let lhs = bin.lhs()?.syntax().text(); + let rhs = bin.rhs()?.syntax().text(); + Some(format!("{} == {}", lhs, rhs)) + } + _ => None, + }, + ast::Expr::PrefixExpr(pe) => match pe.op_kind()? { + ast::PrefixOp::Not => { + let child = pe.expr()?.syntax().text(); + Some(String::from(child)) + } + _ => None, + }, + _ => None, + } +} + #[cfg(test)] mod tests { use super::*; -- cgit v1.2.3