From 5728d7186e64966a20ece3dc479153219a47ba17 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?D=C3=A1niel=20Buga?= Date: Fri, 22 Jan 2021 23:31:19 +0100 Subject: Deduplicate variant matching --- crates/assists/src/handlers/fill_match_arms.rs | 21 +---------------- .../src/handlers/replace_if_let_with_match.rs | 27 ++++------------------ crates/assists/src/utils.rs | 19 +++++++++++++++ 3 files changed, 25 insertions(+), 42 deletions(-) (limited to 'crates/assists/src') diff --git a/crates/assists/src/handlers/fill_match_arms.rs b/crates/assists/src/handlers/fill_match_arms.rs index da47187e4..7663d211d 100644 --- a/crates/assists/src/handlers/fill_match_arms.rs +++ b/crates/assists/src/handlers/fill_match_arms.rs @@ -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, 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, expr: &ast::Expr) -> Option { sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() { Some(Adt::Enum(e)) => Some(e), diff --git a/crates/assists/src/handlers/replace_if_let_with_match.rs b/crates/assists/src/handlers/replace_if_let_with_match.rs index abebd4b6a..aee880625 100644 --- a/crates/assists/src/handlers/replace_if_let_with_match.rs +++ b/crates/assists/src/handlers/replace_if_let_with_match.rs @@ -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. diff --git a/crates/assists/src/utils.rs b/crates/assists/src/utils.rs index 44c35bafa..4e762e18b 100644 --- a/crates/assists/src/utils.rs +++ b/crates/assists/src/utils.rs @@ -248,3 +248,22 @@ fn invert_special_case(expr: &ast::Expr) -> Option { pub(crate) fn next_prev() -> impl Iterator { [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 +} -- cgit v1.2.3