Skip to content

Commit d00add1

Browse files
committed
Introduce assists utils
1 parent 561b4b1 commit d00add1

File tree

5 files changed

+32
-26
lines changed

5 files changed

+32
-26
lines changed

crates/ra_assists/src/handlers/apply_demorgan.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
use super::invert_if::invert_boolean_expression;
21
use ra_syntax::ast::{self, AstNode};
32

4-
use crate::{Assist, AssistCtx, AssistId};
3+
use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId};
54

65
// Assist: apply_demorgan
76
//

crates/ra_assists/src/handlers/early_return.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use ra_syntax::{
1010

1111
use crate::{
1212
assist_ctx::{Assist, AssistCtx},
13-
handlers::invert_if::invert_boolean_expression,
13+
utils::invert_boolean_expression,
1414
AssistId,
1515
};
1616

crates/ra_assists/src/handlers/invert_if.rs

Lines changed: 2 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
use ra_syntax::ast::{self, make, AstNode};
1+
use ra_syntax::ast::{self, AstNode};
22
use ra_syntax::T;
33

4-
use crate::{Assist, AssistCtx, AssistId};
4+
use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId};
55

66
// Assist: invert_if
77
//
@@ -51,27 +51,6 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> {
5151
None
5252
}
5353

54-
pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
55-
if let Some(expr) = invert_special_case(&expr) {
56-
return expr;
57-
}
58-
make::expr_prefix(T![!], expr)
59-
}
60-
61-
pub(crate) fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
62-
match expr {
63-
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
64-
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
65-
ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
66-
_ => None,
67-
},
68-
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
69-
// FIXME:
70-
// ast::Expr::Literal(true | false )
71-
_ => None,
72-
}
73-
}
74-
7554
#[cfg(test)]
7655
mod tests {
7756
use super::*;

crates/ra_assists/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ mod assist_ctx;
99
mod marks;
1010
#[cfg(test)]
1111
mod doc_tests;
12+
mod utils;
1213
pub mod ast_transform;
1314

1415
use std::cmp::Ordering;

crates/ra_assists/src/utils.rs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Assorted functions shared by several assists.
2+
3+
use ra_syntax::{
4+
ast::{self, make},
5+
T,
6+
};
7+
8+
pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr {
9+
if let Some(expr) = invert_special_case(&expr) {
10+
return expr;
11+
}
12+
make::expr_prefix(T![!], expr)
13+
}
14+
15+
fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
16+
match expr {
17+
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
18+
ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
19+
ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
20+
_ => None,
21+
},
22+
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
23+
// FIXME:
24+
// ast::Expr::Literal(true | false )
25+
_ => None,
26+
}
27+
}

0 commit comments

Comments
 (0)