diff options
author | Aleksey Kladov <[email protected]> | 2020-08-25 09:57:51 +0100 |
---|---|---|
committer | Aleksey Kladov <[email protected]> | 2020-08-25 10:00:32 +0100 |
commit | 7721accebf5e2c39bd4cb984346b11b72c12ba56 (patch) | |
tree | f23650fe42fdc32d81108ff891d17d0bb7b60b6f /crates/assists | |
parent | ef9cea945d5767e7c60d5931a7649a73caea23ad (diff) |
Cleanup invert-if
* stick to trivial factory functions in make
* compress the logic for inverting Option/Result
Diffstat (limited to 'crates/assists')
-rw-r--r-- | crates/assists/src/utils.rs | 33 |
1 files changed, 14 insertions, 19 deletions
diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index e15c982e7..daa7b64f7 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs | |||
@@ -8,10 +8,10 @@ use ide_db::RootDatabase; | |||
8 | use itertools::Itertools; | 8 | use itertools::Itertools; |
9 | use rustc_hash::FxHashSet; | 9 | use rustc_hash::FxHashSet; |
10 | use syntax::{ | 10 | use syntax::{ |
11 | ast::{self, make, NameOwner}, | 11 | ast::{self, make, ArgListOwner, NameOwner}, |
12 | AstNode, Direction, | 12 | AstNode, Direction, |
13 | SyntaxKind::*, | 13 | SyntaxKind::*, |
14 | SyntaxNode, SyntaxText, TextSize, T, | 14 | SyntaxNode, TextSize, T, |
15 | }; | 15 | }; |
16 | 16 | ||
17 | use crate::assist_config::SnippetCap; | 17 | use crate::assist_config::SnippetCap; |
@@ -180,23 +180,18 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> { | |||
180 | _ => None, | 180 | _ => None, |
181 | }, | 181 | }, |
182 | ast::Expr::MethodCallExpr(mce) => { | 182 | ast::Expr::MethodCallExpr(mce) => { |
183 | const IS_SOME_TEXT: &str = "is_some"; | 183 | let receiver = mce.receiver()?; |
184 | const IS_NONE_TEXT: &str = "is_none"; | 184 | let method = mce.name_ref()?; |
185 | const IS_OK_TEXT: &str = "is_ok"; | 185 | let arg_list = mce.arg_list()?; |
186 | const IS_ERR_TEXT: &str = "is_err"; | 186 | |
187 | 187 | let method = match method.text().as_str() { | |
188 | let name = mce.name_ref()?; | 188 | "is_some" => "is_none", |
189 | let name_text = name.text(); | 189 | "is_none" => "is_some", |
190 | 190 | "is_ok" => "is_err", | |
191 | let caller = || -> Option<SyntaxText> { Some(mce.receiver()?.syntax().text()) }; | 191 | "is_err" => "is_ok", |
192 | 192 | _ => return None, | |
193 | match name_text { | 193 | }; |
194 | x if x == IS_SOME_TEXT => make::expr_method_call(IS_NONE_TEXT, caller), | 194 | Some(make::expr_method_call(receiver, method, arg_list)) |
195 | x if x == IS_NONE_TEXT => make::expr_method_call(IS_SOME_TEXT, caller), | ||
196 | x if x == IS_OK_TEXT => make::expr_method_call(IS_ERR_TEXT, caller), | ||
197 | x if x == IS_ERR_TEXT => make::expr_method_call(IS_OK_TEXT, caller), | ||
198 | _ => None, | ||
199 | } | ||
200 | } | 195 | } |
201 | ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), | 196 | ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), |
202 | // FIXME: | 197 | // FIXME: |