mirror of
https://github.com/rust-lang/rust-analyzer
synced 2025-01-23 10:25:06 +00:00
28 lines
817 B
Rust
28 lines
817 B
Rust
|
//! 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,
|
||
|
}
|
||
|
}
|