From 7721accebf5e2c39bd4cb984346b11b72c12ba56 Mon Sep 17 00:00:00 2001 From: Aleksey Kladov Date: Tue, 25 Aug 2020 10:57:51 +0200 Subject: Cleanup invert-if * stick to trivial factory functions in make * compress the logic for inverting Option/Result --- crates/assists/src/utils.rs | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'crates/assists') 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; use itertools::Itertools; use rustc_hash::FxHashSet; use syntax::{ - ast::{self, make, NameOwner}, + ast::{self, make, ArgListOwner, NameOwner}, AstNode, Direction, SyntaxKind::*, - SyntaxNode, SyntaxText, TextSize, T, + SyntaxNode, TextSize, T, }; use crate::assist_config::SnippetCap; @@ -180,23 +180,18 @@ fn invert_special_case(expr: &ast::Expr) -> Option { _ => None, }, ast::Expr::MethodCallExpr(mce) => { - const IS_SOME_TEXT: &str = "is_some"; - const IS_NONE_TEXT: &str = "is_none"; - const IS_OK_TEXT: &str = "is_ok"; - const IS_ERR_TEXT: &str = "is_err"; - - let name = mce.name_ref()?; - let name_text = name.text(); - - let caller = || -> Option { Some(mce.receiver()?.syntax().text()) }; - - match name_text { - x if x == IS_SOME_TEXT => make::expr_method_call(IS_NONE_TEXT, caller), - x if x == IS_NONE_TEXT => make::expr_method_call(IS_SOME_TEXT, caller), - x if x == IS_OK_TEXT => make::expr_method_call(IS_ERR_TEXT, caller), - x if x == IS_ERR_TEXT => make::expr_method_call(IS_OK_TEXT, caller), - _ => None, - } + let receiver = mce.receiver()?; + let method = mce.name_ref()?; + let arg_list = mce.arg_list()?; + + let method = match method.text().as_str() { + "is_some" => "is_none", + "is_none" => "is_some", + "is_ok" => "is_err", + "is_err" => "is_ok", + _ => return None, + }; + Some(make::expr_method_call(receiver, method, arg_list)) } ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(), // FIXME: -- cgit v1.2.3