diff options
Diffstat (limited to 'crates/ide_assists/src/utils.rs')
-rw-r--r-- | crates/ide_assists/src/utils.rs | 50 |
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 @@ | |||
3 | use std::ops; | 3 | use std::ops; |
4 | 4 | ||
5 | use ast::TypeBoundsOwner; | 5 | use ast::TypeBoundsOwner; |
6 | use hir::{Adt, HasSource}; | 6 | use hir::{Adt, HasSource, Semantics}; |
7 | use ide_db::{helpers::SnippetCap, RootDatabase}; | 7 | use ide_db::{ |
8 | helpers::{FamousDefs, SnippetCap}, | ||
9 | RootDatabase, | ||
10 | }; | ||
8 | use itertools::Itertools; | 11 | use itertools::Itertools; |
9 | use stdx::format_to; | 12 | use stdx::format_to; |
10 | use syntax::{ | 13 | use 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 | ||
208 | pub(crate) fn invert_boolean_expression(expr: ast::Expr) -> ast::Expr { | 211 | pub(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 | ||
215 | fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | 221 | fn 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 | ||
269 | fn 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 | |||
278 | fn 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 | |||
250 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { | 290 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { |
251 | [Direction::Next, Direction::Prev].iter().copied() | 291 | [Direction::Next, Direction::Prev].iter().copied() |
252 | } | 292 | } |