Introduce assists utils
This commit is contained in:
parent
561b4b11ff
commit
d00add1f1f
@ -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
|
||||
//
|
||||
|
@ -10,7 +10,7 @@ use ra_syntax::{
|
||||
|
||||
use crate::{
|
||||
assist_ctx::{Assist, AssistCtx},
|
||||
handlers::invert_if::invert_boolean_expression,
|
||||
utils::invert_boolean_expression,
|
||||
AssistId,
|
||||
};
|
||||
|
||||
|
@ -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<Assist> {
|
||||
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<ast::Expr> {
|
||||
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::*;
|
||||
|
@ -9,6 +9,7 @@ mod assist_ctx;
|
||||
mod marks;
|
||||
#[cfg(test)]
|
||||
mod doc_tests;
|
||||
mod utils;
|
||||
pub mod ast_transform;
|
||||
|
||||
use std::cmp::Ordering;
|
||||
|
27
crates/ra_assists/src/utils.rs
Normal file
27
crates/ra_assists/src/utils.rs
Normal file
@ -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<ast::Expr> {
|
||||
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,
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user