mirror of
https://github.com/rust-lang/rust-analyzer.git
synced 2025-10-01 06:11:35 +00:00
Cleanup invert-if
* stick to trivial factory functions in make * compress the logic for inverting Option/Result
This commit is contained in:
parent
ef9cea945d
commit
7721accebf
2 changed files with 16 additions and 24 deletions
|
@ -8,10 +8,10 @@ use ide_db::RootDatabase;
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use rustc_hash::FxHashSet;
|
use rustc_hash::FxHashSet;
|
||||||
use syntax::{
|
use syntax::{
|
||||||
ast::{self, make, NameOwner},
|
ast::{self, make, ArgListOwner, NameOwner},
|
||||||
AstNode, Direction,
|
AstNode, Direction,
|
||||||
SyntaxKind::*,
|
SyntaxKind::*,
|
||||||
SyntaxNode, SyntaxText, TextSize, T,
|
SyntaxNode, TextSize, T,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::assist_config::SnippetCap;
|
use crate::assist_config::SnippetCap;
|
||||||
|
@ -180,23 +180,18 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
|
||||||
_ => None,
|
_ => None,
|
||||||
},
|
},
|
||||||
ast::Expr::MethodCallExpr(mce) => {
|
ast::Expr::MethodCallExpr(mce) => {
|
||||||
const IS_SOME_TEXT: &str = "is_some";
|
let receiver = mce.receiver()?;
|
||||||
const IS_NONE_TEXT: &str = "is_none";
|
let method = mce.name_ref()?;
|
||||||
const IS_OK_TEXT: &str = "is_ok";
|
let arg_list = mce.arg_list()?;
|
||||||
const IS_ERR_TEXT: &str = "is_err";
|
|
||||||
|
|
||||||
let name = mce.name_ref()?;
|
let method = match method.text().as_str() {
|
||||||
let name_text = name.text();
|
"is_some" => "is_none",
|
||||||
|
"is_none" => "is_some",
|
||||||
let caller = || -> Option<SyntaxText> { Some(mce.receiver()?.syntax().text()) };
|
"is_ok" => "is_err",
|
||||||
|
"is_err" => "is_ok",
|
||||||
match name_text {
|
_ => return None,
|
||||||
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),
|
Some(make::expr_method_call(receiver, method, arg_list))
|
||||||
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,
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
|
ast::Expr::PrefixExpr(pe) if pe.op_kind()? == ast::PrefixOp::Not => pe.expr(),
|
||||||
// FIXME:
|
// FIXME:
|
||||||
|
|
|
@ -7,7 +7,7 @@
|
||||||
use itertools::Itertools;
|
use itertools::Itertools;
|
||||||
use stdx::format_to;
|
use stdx::format_to;
|
||||||
|
|
||||||
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxText, SyntaxToken};
|
use crate::{ast, AstNode, SourceFile, SyntaxKind, SyntaxNode, SyntaxToken};
|
||||||
|
|
||||||
pub fn name(text: &str) -> ast::Name {
|
pub fn name(text: &str) -> ast::Name {
|
||||||
ast_from_text(&format!("mod {};", text))
|
ast_from_text(&format!("mod {};", text))
|
||||||
|
@ -137,11 +137,8 @@ pub fn expr_prefix(op: SyntaxKind, expr: ast::Expr) -> ast::Expr {
|
||||||
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
|
pub fn expr_call(f: ast::Expr, arg_list: ast::ArgList) -> ast::Expr {
|
||||||
expr_from_text(&format!("{}{}", f, arg_list))
|
expr_from_text(&format!("{}{}", f, arg_list))
|
||||||
}
|
}
|
||||||
pub fn expr_method_call<F>(text: &str, caller: F) -> Option<ast::Expr>
|
pub fn expr_method_call(receiver: ast::Expr, method: &str, arg_list: ast::ArgList) -> ast::Expr {
|
||||||
where
|
expr_from_text(&format!("{}.{}{}", receiver, method, arg_list))
|
||||||
F: FnOnce() -> Option<SyntaxText>,
|
|
||||||
{
|
|
||||||
try_expr_from_text(&format!("{}.{}()", caller()?, text))
|
|
||||||
}
|
}
|
||||||
fn expr_from_text(text: &str) -> ast::Expr {
|
fn expr_from_text(text: &str) -> ast::Expr {
|
||||||
ast_from_text(&format!("const C: () = {};", text))
|
ast_from_text(&format!("const C: () = {};", text))
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue