From d00add1f1fec59494c3c1a99c27937ae3891458d Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Fri, 7 Feb 2020 15:57:38 +0100 Subject: Introduce assists utils --- crates/ra_assists/src/handlers/apply_demorgan.rs | 3 +-- crates/ra_assists/src/handlers/early_return.rs | 2 +- crates/ra_assists/src/handlers/invert_if.rs | 25 ++-------------------- crates/ra_assists/src/lib.rs | 1 + crates/ra_assists/src/utils.rs | 27 ++++++++++++++++++++++++ 5 files changed, 32 insertions(+), 26 deletions(-) create mode 100644 crates/ra_assists/src/utils.rs diff --git a/crates/ra_assists/src/handlers/apply_demorgan.rs b/crates/ra_assists/src/handlers/apply_demorgan.rs index ba08a8223..239807e24 100644 --- a/crates/ra_assists/src/handlers/apply_demorgan.rs +++ b/crates/ra_assists/src/handlers/apply_demorgan.rs @@ -1,7 +1,6 @@ -use super::invert_if::invert_boolean_expression; use ra_syntax::ast::{self, AstNode}; -use crate::{Assist, AssistCtx, AssistId}; +use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId}; // Assist: apply_demorgan // diff --git a/crates/ra_assists/src/handlers/early_return.rs b/crates/ra_assists/src/handlers/early_return.rs index 8eb3bd473..22f88884f 100644 --- a/crates/ra_assists/src/handlers/early_return.rs +++ b/crates/ra_assists/src/handlers/early_return.rs @@ -10,7 +10,7 @@ use ra_syntax::{ use crate::{ assist_ctx::{Assist, AssistCtx}, - handlers::invert_if::invert_boolean_expression, + utils::invert_boolean_expression, AssistId, }; diff --git a/crates/ra_assists/src/handlers/invert_if.rs b/crates/ra_assists/src/handlers/invert_if.rs index 983392f21..a594e7e0c 100644 --- a/crates/ra_assists/src/handlers/invert_if.rs +++ b/crates/ra_assists/src/handlers/invert_if.rs @@ -1,7 +1,7 @@ -use ra_syntax::ast::{self, make, AstNode}; +use ra_syntax::ast::{self, AstNode}; use ra_syntax::T; -use crate::{Assist, AssistCtx, AssistId}; +use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId}; // Assist: invert_if // @@ -51,27 +51,6 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option { None } -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) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), - // FIXME: - // ast::Expr::Literal(true | false ) - _ => None, - } -} - #[cfg(test)] mod tests { use super::*; diff --git a/crates/ra_assists/src/lib.rs b/crates/ra_assists/src/lib.rs index 7b08e8fd7..eca6dec4b 100644 --- a/crates/ra_assists/src/lib.rs +++ b/crates/ra_assists/src/lib.rs @@ -9,6 +9,7 @@ mod assist_ctx; mod marks; #[cfg(test)] mod doc_tests; +mod utils; pub mod ast_transform; use std::cmp::Ordering; diff --git a/crates/ra_assists/src/utils.rs b/crates/ra_assists/src/utils.rs new file mode 100644 index 000000000..0d5722295 --- /dev/null +++ b/crates/ra_assists/src/utils.rs @@ -0,0 +1,27 @@ +//! Assorted functions shared by several assists. + +use ra_syntax::{ + ast::{self, make}, + T, +}; + +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) +} + +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) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), + // FIXME: + // ast::Expr::Literal(true | false ) + _ => None, + } +} -- cgit v1.2.3