Skip to content

Commit e17243d

Browse files
committed
[#1807] Refactor file structure
Use the more conventional way of importing the ast types, and put the assist at the top of the file.
1 parent 1ed1e3d commit e17243d

File tree

1 file changed

+33
-33
lines changed

1 file changed

+33
-33
lines changed

crates/ra_assists/src/assists/apply_demorgan.rs

Lines changed: 33 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,19 @@
22
//! This assist transforms boolean expressions of the form `!a || !b` into
33
//! `!(a && b)`.
44
use hir::db::HirDatabase;
5-
use ra_syntax::ast::{AstNode, BinExpr, BinOp, Expr, PrefixOp};
5+
use ra_syntax::ast::{self, AstNode};
66
use ra_syntax::SyntaxNode;
77

88
use crate::{Assist, AssistCtx, AssistId};
99

10-
// Return the opposite text for a given logical operator, if it makes sense
11-
fn opposite_logic_op(kind: BinOp) -> Option<&'static str> {
12-
match kind {
13-
BinOp::BooleanOr => Some("&&"),
14-
BinOp::BooleanAnd => Some("||"),
15-
_ => None,
16-
}
17-
}
18-
19-
// This function tries to undo unary negation, or inequality
20-
fn undo_negation(node: SyntaxNode) -> Option<String> {
21-
match Expr::cast(node)? {
22-
Expr::BinExpr(bin) => match bin.op_kind()? {
23-
BinOp::NegatedEqualityTest => {
24-
let lhs = bin.lhs()?.syntax().text();
25-
let rhs = bin.rhs()?.syntax().text();
26-
Some(format!("{} == {}", lhs, rhs))
27-
}
28-
_ => None,
29-
},
30-
Expr::PrefixExpr(pe) => match pe.op_kind()? {
31-
PrefixOp::Not => {
32-
let child = pe.expr()?.syntax().text();
33-
Some(String::from(child))
34-
}
35-
_ => None,
36-
},
37-
_ => None,
38-
}
39-
}
40-
4110
/// Assist for applying demorgan's law
4211
///
4312
/// This transforms expressions of the form `!l || !r` into `!(l && r)`.
4413
/// This also works with `&&`. This assist can only be applied with the cursor
4514
/// on either `||` or `&&`, with both operands being a negation of some kind.
4615
/// This means something of the form `!x` or `x != y`.
4716
pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
48-
let expr = ctx.node_at_offset::<BinExpr>()?;
17+
let expr = ctx.node_at_offset::<ast::BinExpr>()?;
4918
let op = expr.op_kind()?;
5019
let op_range = expr.op_token()?.text_range();
5120
let opposite_op = opposite_logic_op(op)?;
@@ -69,6 +38,37 @@ pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Ass
6938
ctx.build()
7039
}
7140

41+
// Return the opposite text for a given logical operator, if it makes sense
42+
fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
43+
match kind {
44+
ast::BinOp::BooleanOr => Some("&&"),
45+
ast::BinOp::BooleanAnd => Some("||"),
46+
_ => None,
47+
}
48+
}
49+
50+
// This function tries to undo unary negation, or inequality
51+
fn undo_negation(node: SyntaxNode) -> Option<String> {
52+
match ast::Expr::cast(node)? {
53+
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
54+
ast::BinOp::NegatedEqualityTest => {
55+
let lhs = bin.lhs()?.syntax().text();
56+
let rhs = bin.rhs()?.syntax().text();
57+
Some(format!("{} == {}", lhs, rhs))
58+
}
59+
_ => None,
60+
},
61+
ast::Expr::PrefixExpr(pe) => match pe.op_kind()? {
62+
ast::PrefixOp::Not => {
63+
let child = pe.expr()?.syntax().text();
64+
Some(String::from(child))
65+
}
66+
_ => None,
67+
},
68+
_ => None,
69+
}
70+
}
71+
7272
#[cfg(test)]
7373
mod tests {
7474
use super::*;

0 commit comments

Comments
 (0)