Skip to content

Commit 7721acc

Browse files
committed
Cleanup invert-if
* stick to trivial factory functions in make * compress the logic for inverting Option/Result
1 parent ef9cea9 commit 7721acc

File tree

2 files changed

+17
-25
lines changed

2 files changed

+17
-25
lines changed

crates/assists/src/utils.rs

Lines changed: 14 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use ide_db::RootDatabase;
88
use itertools::Itertools;
99
use rustc_hash::FxHashSet;
1010
use syntax::{
11-
ast::{self, make, NameOwner},
11+
ast::{self, make, ArgListOwner, NameOwner},
1212
AstNode, Direction,
1313
SyntaxKind::*,
14-
SyntaxNode, SyntaxText, TextSize, T,
14+
SyntaxNode, TextSize, T,
1515
};
1616

1717
use crate::assist_config::SnippetCap;
@@ -180,23 +180,18 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
180180
_ => None,
181181
},
182182
ast::Expr::MethodCallExpr(mce) => {
183-
const IS_SOME_TEXT: &str = "is_some";
184-
const IS_NONE_TEXT: &str = "is_none";
185-
const IS_OK_TEXT: &str = "is_ok";
186-
const IS_ERR_TEXT: &str = "is_err";
187-
188-
let name = mce.name_ref()?;
189-
let name_text = name.text();
190-
191-
let caller = || -> Option<SyntaxText> { Some(mce.receiver()?.syntax().text()) };
192-
193-
match name_text {
194-
x if x == IS_SOME_TEXT => make::expr_method_call(IS_NONE_TEXT, caller),
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-
}
183+
let receiver = mce.receiver()?;
184+
let method = mce.name_ref()?;
185+
let arg_list = mce.arg_list()?;
186+
187+
let method = match method.text().as_str() {
188+
"is_some" => "is_none",
189+
"is_none" => "is_some",
190+
"is_ok" => "is_err",
191+
"is_err" => "is_ok",
192+
_ => return None,
193+
};
194+
Some(make::expr_method_call(receiver, method, arg_list))
200195
}
201196
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
202197
// FIXME:

crates/syntax/src/ast/make.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
use itertools::Itertools;
88
use stdx::format_to;
99

10-
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxText, SyntaxToken};
10+
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
1111

1212
pub fn name(text: &str) -> ast::Name {
1313
ast_from_text(&format!("mod {};", text))
@@ -137,11 +137,8 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
137137
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
138138
expr_from_text(&format!("{}{}", f, arg_list))
139139
}
140-
pub fn expr_method_call<F>(text: &str, caller: F) -> Option<ast::Expr>
141-
where
142-
F: FnOnce() -> Option<SyntaxText>,
143-
{
144-
try_expr_from_text(&format!("{}.{}()", caller()?, text))
140+
pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr {
141+
expr_from_text(&format!("{}.{}{}", receiver, method, arg_list))
145142
}
146143
fn expr_from_text(text: &str) -> ast::Expr {
147144
ast_from_text(&format!("const C: () = {};", text))

0 commit comments

Comments
 (0)