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 | |
parent | ef9cea945d5767e7c60d5931a7649a73caea23ad (diff) |
Cleanup invert-if
* stick to trivial factory functions in make
* compress the logic for inverting Option/Result
-rw-r--r-- | crates/assists/src/utils.rs | 33 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 9 |
2 files changed, 17 insertions, 25 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: |
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index 7958721e2..7ba625990 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -7,7 +7,7 @@ | |||
7 | use itertools::Itertools; | 7 | use itertools::Itertools; |
8 | use stdx::format_to; | 8 | use stdx::format_to; |
9 | 9 | ||
10 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxText, SyntaxToken}; | 10 | use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken}; |
11 | 11 | ||
12 | pub fn name(text: &str) -> ast::Name { | 12 | pub fn name(text: &str) -> ast::Name { |
13 | ast_from_text(&format!("mod {};", text)) | 13 | ast_from_text(&format!("mod {};", text)) |
@@ -137,11 +137,8 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr { | |||
137 | pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { | 137 | pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr { |
138 | expr_from_text(&format!("{}{}", f, arg_list)) | 138 | expr_from_text(&format!("{}{}", f, arg_list)) |
139 | } | 139 | } |
140 | pub fn expr_method_call<F>(text: &str, caller: F) -> Option<ast::Expr> | 140 | pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr { |
141 | where | 141 | expr_from_text(&format!("{}.{}{}", receiver, method, arg_list)) |
142 | F: FnOnce() -> Option<SyntaxText>, | ||
143 | { | ||
144 | try_expr_from_text(&format!("{}.{}()", caller()?, text)) | ||
145 | } | 142 | } |
146 | fn expr_from_text(text: &str) -> ast::Expr { | 143 | fn expr_from_text(text: &str) -> ast::Expr { |
147 | ast_from_text(&format!("const C: () = {};", text)) | 144 | ast_from_text(&format!("const C: () = {};", text)) |