Deduplicate variant matching
This commit is contained in:
parent
24f0cd8293
commit
5728d7186e
3 changed files with 25 additions and 42 deletions
|
@ -8,7 +8,7 @@ use syntax::ast::{self, make, AstNode, MatchArm, NameOwner, Pat};
|
|||
use test_utils::mark;
|
||||
|
||||
use crate::{
|
||||
utils::{render_snippet, Cursor},
|
||||
utils::{does_pat_match_variant, render_snippet, Cursor},
|
||||
AssistContext, AssistId, AssistKind, Assists,
|
||||
};
|
||||
|
||||
|
@ -147,25 +147,6 @@ fn is_variant_missing(existing_arms: &mut Vec<MatchArm>, var: &Pat) -> bool {
|
|||
})
|
||||
}
|
||||
|
||||
fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
|
||||
let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text());
|
||||
|
||||
let pat_head = match pat {
|
||||
Pat::IdentPat(bind_pat) => {
|
||||
if let Some(p) = bind_pat.pat() {
|
||||
first_node_text(&p)
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
pat => first_node_text(pat),
|
||||
};
|
||||
|
||||
let var_head = first_node_text(var);
|
||||
|
||||
pat_head == var_head
|
||||
}
|
||||
|
||||
fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> {
|
||||
sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() {
|
||||
Some(Adt::Enum(e)) => Some(e),
|
||||
|
|
|
@ -5,12 +5,15 @@ use syntax::{
|
|||
ast::{
|
||||
self,
|
||||
edit::{AstNodeEdit, IndentLevel},
|
||||
make, Pat,
|
||||
make,
|
||||
},
|
||||
AstNode,
|
||||
};
|
||||
|
||||
use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists};
|
||||
use crate::{
|
||||
utils::{does_pat_match_variant, unwrap_trivial_block},
|
||||
AssistContext, AssistId, AssistKind, Assists,
|
||||
};
|
||||
|
||||
// Assist: replace_if_let_with_match
|
||||
//
|
||||
|
@ -87,26 +90,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext)
|
|||
)
|
||||
}
|
||||
|
||||
fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool {
|
||||
let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text());
|
||||
|
||||
let pat_head = match pat {
|
||||
Pat::IdentPat(bind_pat) => {
|
||||
if let Some(p) = bind_pat.pat() {
|
||||
first_node_text(&p)
|
||||
} else {
|
||||
return pat.syntax().text() == var.syntax().text();
|
||||
}
|
||||
}
|
||||
pat => first_node_text(pat),
|
||||
};
|
||||
|
||||
let var_head = first_node_text(var);
|
||||
println!("{:?} {:?}", pat_head, var_head);
|
||||
|
||||
pat_head == var_head
|
||||
}
|
||||
|
||||
// Assist: replace_match_with_if_let
|
||||
//
|
||||
// Replaces a binary `match` with a wildcard pattern and no guards with an `if let` expression.
|
||||
|
|
|
@ -248,3 +248,22 @@ fn invert_special_case(expr: &ast::Expr) -> Option<ast::Expr> {
|
|||
pub(crate) fn next_prev() -> impl Iterator<Item = Direction> {
|
||||
[Direction::Next, Direction::Prev].iter().copied()
|
||||
}
|
||||
|
||||
pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool {
|
||||
let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text());
|
||||
|
||||
let pat_head = match pat {
|
||||
ast::Pat::IdentPat(bind_pat) => {
|
||||
if let Some(p) = bind_pat.pat() {
|
||||
first_node_text(&p)
|
||||
} else {
|
||||
return pat.syntax().text() == var.syntax().text();
|
||||
}
|
||||
}
|
||||
pat => first_node_text(pat),
|
||||
};
|
||||
|
||||
let var_head = first_node_text(var);
|
||||
|
||||
pat_head == var_head
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue