aboutsummaryrefslogtreecommitdiff
path: root/crates/ide_assists/src/utils.rs
diff options
context:
space:
mode:
authorlbrande <[email protected]>2021-02-22 15:23:42 +0000
committerLukas Wirth <[email protected]>2021-02-24 10:58:37 +0000
commit9db970ee082e315cfa04db163fe2e0268b618531 (patch)
tree9fef7832ba257eba27566653987b90ca089018de /crates/ide_assists/src/utils.rs
parentf7a4a87de2840789e12722afc7df9f4db2db013c (diff)
De Morgan's Law assist now correctly inverts <, <=, >, >=.
Diffstat (limited to 'crates/ide_assists/src/utils.rs')
-rw-r--r--crates/ide_assists/src/utils.rs50
1 files changed, 45 insertions, 5 deletions
diff --git a/crates/ide_assists/src/utils.rs b/crates/ide_assists/src/utils.rs
index cd026d432..38ed74673 100644
--- a/crates/ide_assists/src/utils.rs
+++ b/crates/ide_assists/src/utils.rs
@@ -3,8 +3,11 @@
3use std::ops; 3use std::ops;
4 4
5use ast::TypeBoundsOwner; 5use ast::TypeBoundsOwner;
6use hir::{Adt, HasSource}; 6use hir::{Adt, HasSource, Semantics};
7use ide_db::{helpers::SnippetCap, RootDatabase}; 7use ide_db::{
8 helpers::{FamousDefs, SnippetCap},
9 RootDatabase,
10};
8use itertools::Itertools; 11use itertools::Itertools;
9use stdx::format_to; 12use stdx::format_to;
10use syntax::{ 13use syntax::{
@@ -205,18 +208,34 @@ pub(crate) fn vis_offset(node: &SyntaxNode) -> TextSize {
205 .unwrap_or_else(|| node.text_range().start()) 208 .unwrap_or_else(|| node.text_range().start())
206} 209}
207 210
208pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { 211pub(crate) fn invert_boolean_expression(
209 if let Some(expr) = invert_special_case(&expr) { 212 sema: &Semantics<RootDatabase>,
213 expr: ast::Expr,
214) -> ast::Expr {
215 if let Some(expr) = invert_special_case(sema, &expr) {
210 return expr; 216 return expr;
211 } 217 }
212 make::expr_prefix(T![!], expr) 218 make::expr_prefix(T![!], expr)
213} 219}
214 220
215fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { 221fn invert_special_case(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<ast::Expr> {
216 match expr { 222 match expr {
217 ast::Expr::BinExpr(bin) => match bin.op_kind()? { 223 ast::Expr::BinExpr(bin) => match bin.op_kind()? {
218 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()), 224 ast::BinOp::NegatedEqualityTest => bin.replace_op(T![==]).map(|it| it.into()),
219 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()), 225 ast::BinOp::EqualityTest => bin.replace_op(T![!=]).map(|it| it.into()),
226 // Swap `<` with `>=`, `<=` with `>`, ... if operands `impl Ord`
227 ast::BinOp::LesserTest if bin_impls_ord(sema, bin) => {
228 bin.replace_op(T![>=]).map(|it| it.into())
229 }
230 ast::BinOp::LesserEqualTest if bin_impls_ord(sema, bin) => {
231 bin.replace_op(T![>]).map(|it| it.into())
232 }
233 ast::BinOp::GreaterTest if bin_impls_ord(sema, bin) => {
234 bin.replace_op(T![<=]).map(|it| it.into())
235 }
236 ast::BinOp::GreaterEqualTest if bin_impls_ord(sema, bin) => {
237 bin.replace_op(T![<]).map(|it| it.into())
238 }
220 // Parenthesize other expressions before prefixing `!` 239 // Parenthesize other expressions before prefixing `!`
221 _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))), 240 _ => Some(make::expr_prefix(T![!], make::expr_paren(expr.clone()))),
222 }, 241 },
@@ -247,6 +266,27 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
247 } 266 }
248} 267}
249 268
269fn bin_impls_ord(sema: &Semantics<RootDatabase>, bin: &ast::BinExpr) -> bool {
270 if let (Some(lhs), Some(rhs)) = (bin.lhs(), bin.rhs()) {
271 return sema.type_of_expr(&lhs) == sema.type_of_expr(&rhs)
272 && impls_ord(sema, &lhs)
273 && impls_ord(sema, &rhs);
274 }
275 false
276}
277
278fn impls_ord(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> bool {
279 let krate = sema.scope(expr.syntax()).module().map(|it| it.krate());
280 let famous_defs = FamousDefs(&sema, krate);
281
282 if let Some(ty) = sema.type_of_expr(expr) {
283 if let Some(ord_trait) = famous_defs.core_cmp_Ord() {
284 return ty.autoderef(sema.db).any(|ty| ty.impls_trait(sema.db, ord_trait, &[]));
285 }
286 }
287 false
288}
289
250pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { 290pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
251 [Direction::Next, Direction::Prev].iter().copied() 291 [Direction::Next, Direction::Prev].iter().copied()
252} 292}