diff options
author | Aleksey Kladov <[email protected]> | 2020-02-07 14:57:38 +0000 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-02-07 16:28:02 +0000 |
commit | d00add1f1fec59494c3c1a99c27937ae3891458d (patch) | |
tree | 06c2bf44b0f9967f5e5f4cdddcec743d896478e8 | |
parent | 561b4b11ff1d87ea1ff2477dcba6ae1f396573a3 (diff) |
Introduce assists utils
-rw-r--r-- | crates/ra_assists/src/handlers/apply_demorgan.rs | 3 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/early_return.rs | 2 | ||||
-rw-r--r-- | crates/ra_assists/src/handlers/invert_if.rs | 25 | ||||
-rw-r--r-- | crates/ra_assists/src/lib.rs | 1 | ||||
-rw-r--r-- | crates/ra_assists/src/utils.rs | 27 |
5 files changed, 32 insertions, 26 deletions
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 @@ | |||
1 | use super::invert_if::invert_boolean_expression; | ||
2 | use ra_syntax::ast::{self, AstNode}; | 1 | use ra_syntax::ast::{self, AstNode}; |
3 | 2 | ||
4 | use crate::{Assist, AssistCtx, AssistId}; | 3 | use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId}; |
5 | 4 | ||
6 | // Assist: apply_demorgan | 5 | // Assist: apply_demorgan |
7 | // | 6 | // |
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::{ | |||
10 | 10 | ||
11 | use crate::{ | 11 | use crate::{ |
12 | assist_ctx::{Assist, AssistCtx}, | 12 | assist_ctx::{Assist, AssistCtx}, |
13 | handlers::invert_if::invert_boolean_expression, | 13 | utils::invert_boolean_expression, |
14 | AssistId, | 14 | AssistId, |
15 | }; | 15 | }; |
16 | 16 | ||
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 @@ | |||
1 | use ra_syntax::ast::{self, make, AstNode}; | 1 | use ra_syntax::ast::{self, AstNode}; |
2 | use ra_syntax::T; | 2 | use ra_syntax::T; |
3 | 3 | ||
4 | use crate::{Assist, AssistCtx, AssistId}; | 4 | use crate::{utils::invert_boolean_expression, Assist, AssistCtx, AssistId}; |
5 | 5 | ||
6 | // Assist: invert_if | 6 | // Assist: invert_if |
7 | // | 7 | // |
@@ -51,27 +51,6 @@ pub(crate) fn invert_if(ctx: AssistCtx) -> Option<Assist> { | |||
51 | None | 51 | None |
52 | } | 52 | } |
53 | 53 | ||
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 | |||
75 | #[cfg(test)] | 54 | #[cfg(test)] |
76 | mod tests { | 55 | mod tests { |
77 | use super::*; | 56 | 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; | |||
9 | mod marks; | 9 | mod marks; |
10 | #[cfg(test)] | 10 | #[cfg(test)] |
11 | mod doc_tests; | 11 | mod doc_tests; |
12 | mod utils; | ||
12 | pub mod ast_transform; | 13 | pub mod ast_transform; |
13 | 14 | ||
14 | use std::cmp::Ordering; | 15 | 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 @@ | |||
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 | } | ||