mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-11-15 13:52:59 +00:00
[#1807] Refactor file structure
Use the more conventional way of importing the ast types, and put the assist at the top of the file.
This commit is contained in:
parent
1ed1e3d4a7
commit
e17243d698
@ -2,42 +2,11 @@
|
|||||||
//! This assist transforms boolean expressions of the form `!a || !b` into
|
//! This assist transforms boolean expressions of the form `!a || !b` into
|
||||||
//! `!(a && b)`.
|
//! `!(a && b)`.
|
||||||
use hir::db::HirDatabase;
|
use hir::db::HirDatabase;
|
||||||
use ra_syntax::ast::{AstNode, BinExpr, BinOp, Expr, PrefixOp};
|
use ra_syntax::ast::{self, AstNode};
|
||||||
use ra_syntax::SyntaxNode;
|
use ra_syntax::SyntaxNode;
|
||||||
|
|
||||||
use crate::{Assist, AssistCtx, AssistId};
|
use crate::{Assist, AssistCtx, AssistId};
|
||||||
|
|
||||||
// Return the opposite text for a given logical operator, if it makes sense
|
|
||||||
fn opposite_logic_op(kind: BinOp) -> Option<&'static str> {
|
|
||||||
match kind {
|
|
||||||
BinOp::BooleanOr => Some("&&"),
|
|
||||||
BinOp::BooleanAnd => Some("||"),
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// This function tries to undo unary negation, or inequality
|
|
||||||
fn undo_negation(node: SyntaxNode) -> Option<String> {
|
|
||||||
match Expr::cast(node)? {
|
|
||||||
Expr::BinExpr(bin) => match bin.op_kind()? {
|
|
||||||
BinOp::NegatedEqualityTest => {
|
|
||||||
let lhs = bin.lhs()?.syntax().text();
|
|
||||||
let rhs = bin.rhs()?.syntax().text();
|
|
||||||
Some(format!("{} == {}", lhs, rhs))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
Expr::PrefixExpr(pe) => match pe.op_kind()? {
|
|
||||||
PrefixOp::Not => {
|
|
||||||
let child = pe.expr()?.syntax().text();
|
|
||||||
Some(String::from(child))
|
|
||||||
}
|
|
||||||
_ => None,
|
|
||||||
},
|
|
||||||
_ => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Assist for applying demorgan's law
|
/// Assist for applying demorgan's law
|
||||||
///
|
///
|
||||||
/// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
/// This transforms expressions of the form `!l || !r` into `!(l && r)`.
|
||||||
@ -45,7 +14,7 @@ fn undo_negation(node: SyntaxNode) -> Option<String> {
|
|||||||
/// on either `||` or `&&`, with both operands being a negation of some kind.
|
/// on either `||` or `&&`, with both operands being a negation of some kind.
|
||||||
/// This means something of the form `!x` or `x != y`.
|
/// This means something of the form `!x` or `x != y`.
|
||||||
pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Assist> {
|
||||||
let expr = ctx.node_at_offset::<BinExpr>()?;
|
let expr = ctx.node_at_offset::<ast::BinExpr>()?;
|
||||||
let op = expr.op_kind()?;
|
let op = expr.op_kind()?;
|
||||||
let op_range = expr.op_token()?.text_range();
|
let op_range = expr.op_token()?.text_range();
|
||||||
let opposite_op = opposite_logic_op(op)?;
|
let opposite_op = opposite_logic_op(op)?;
|
||||||
@ -69,6 +38,37 @@ pub(crate) fn apply_demorgan(mut ctx: AssistCtx<impl HirDatabase>) -> Option<Ass
|
|||||||
ctx.build()
|
ctx.build()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Return the opposite text for a given logical operator, if it makes sense
|
||||||
|
fn opposite_logic_op(kind: ast::BinOp) -> Option<&'static str> {
|
||||||
|
match kind {
|
||||||
|
ast::BinOp::BooleanOr => Some("&&"),
|
||||||
|
ast::BinOp::BooleanAnd => Some("||"),
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// This function tries to undo unary negation, or inequality
|
||||||
|
fn undo_negation(node: SyntaxNode) -> Option<String> {
|
||||||
|
match ast::Expr::cast(node)? {
|
||||||
|
ast::Expr::BinExpr(bin) => match bin.op_kind()? {
|
||||||
|
ast::BinOp::NegatedEqualityTest => {
|
||||||
|
let lhs = bin.lhs()?.syntax().text();
|
||||||
|
let rhs = bin.rhs()?.syntax().text();
|
||||||
|
Some(format!("{} == {}", lhs, rhs))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
ast::Expr::PrefixExpr(pe) => match pe.op_kind()? {
|
||||||
|
ast::PrefixOp::Not => {
|
||||||
|
let child = pe.expr()?.syntax().text();
|
||||||
|
Some(String::from(child))
|
||||||
|
}
|
||||||
|
_ => None,
|
||||||
|
},
|
||||||
|
_ => None,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user