diff options
author | Dániel Buga <[email protected]> | 2021-01-22 22:31:19 +0000 |
---|---|---|
committer | Dániel Buga <[email protected]> | 2021-01-22 22:39:43 +0000 |
commit | 5728d7186e64966a20ece3dc479153219a47ba17 (patch) | |
tree | 6b337aa5e673f30cc6168f882ec5a85076bbc48b | |
parent | 24f0cd8293c73b376a5e448dcbadcf189e8698aa (diff) |
Deduplicate variant matching
-rw-r--r-- | crates/assists/src/handlers/fill_match_arms.rs | 21 | ||||
-rw-r--r-- | crates/assists/src/handlers/replace_if_let_with_match.rs | 27 | ||||
-rw-r--r-- | crates/assists/src/utils.rs | 19 |
3 files changed, 25 insertions, 42 deletions
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}; | |||
8 | use test_utils::mark; | 8 | use test_utils::mark; |
9 | 9 | ||
10 | use crate::{ | 10 | use crate::{ |
11 | utils::{render_snippet, Cursor}, | 11 | utils::{does_pat_match_variant, render_snippet, Cursor}, |
12 | AssistContext, AssistId, AssistKind, Assists, | 12 | AssistContext, AssistId, AssistKind, Assists, |
13 | }; | 13 | }; |
14 | 14 | ||
@@ -147,25 +147,6 @@ fn is_variant_missing(existing_arms: &mut Vec<MatchArm>, var: &Pat) -> bool { | |||
147 | }) | 147 | }) |
148 | } | 148 | } |
149 | 149 | ||
150 | fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool { | ||
151 | let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text()); | ||
152 | |||
153 | let pat_head = match pat { | ||
154 | Pat::IdentPat(bind_pat) => { | ||
155 | if let Some(p) = bind_pat.pat() { | ||
156 | first_node_text(&p) | ||
157 | } else { | ||
158 | return false; | ||
159 | } | ||
160 | } | ||
161 | pat => first_node_text(pat), | ||
162 | }; | ||
163 | |||
164 | let var_head = first_node_text(var); | ||
165 | |||
166 | pat_head == var_head | ||
167 | } | ||
168 | |||
169 | fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> { | 150 | fn resolve_enum_def(sema: &Semantics<RootDatabase>, expr: &ast::Expr) -> Option<hir::Enum> { |
170 | sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() { | 151 | sema.type_of_expr(&expr)?.autoderef(sema.db).find_map(|ty| match ty.as_adt() { |
171 | Some(Adt::Enum(e)) => Some(e), | 152 | 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::{ | |||
5 | ast::{ | 5 | ast::{ |
6 | self, | 6 | self, |
7 | edit::{AstNodeEdit, IndentLevel}, | 7 | edit::{AstNodeEdit, IndentLevel}, |
8 | make, Pat, | 8 | make, |
9 | }, | 9 | }, |
10 | AstNode, | 10 | AstNode, |
11 | }; | 11 | }; |
12 | 12 | ||
13 | use crate::{utils::unwrap_trivial_block, AssistContext, AssistId, AssistKind, Assists}; | 13 | use crate::{ |
14 | utils::{does_pat_match_variant, unwrap_trivial_block}, | ||
15 | AssistContext, AssistId, AssistKind, Assists, | ||
16 | }; | ||
14 | 17 | ||
15 | // Assist: replace_if_let_with_match | 18 | // Assist: replace_if_let_with_match |
16 | // | 19 | // |
@@ -87,26 +90,6 @@ pub(crate) fn replace_if_let_with_match(acc: &mut Assists, ctx: &AssistContext) | |||
87 | ) | 90 | ) |
88 | } | 91 | } |
89 | 92 | ||
90 | fn does_pat_match_variant(pat: &Pat, var: &Pat) -> bool { | ||
91 | let first_node_text = |pat: &Pat| pat.syntax().first_child().map(|node| node.text()); | ||
92 | |||
93 | let pat_head = match pat { | ||
94 | Pat::IdentPat(bind_pat) => { | ||
95 | if let Some(p) = bind_pat.pat() { | ||
96 | first_node_text(&p) | ||
97 | } else { | ||
98 | return pat.syntax().text() == var.syntax().text(); | ||
99 | } | ||
100 | } | ||
101 | pat => first_node_text(pat), | ||
102 | }; | ||
103 | |||
104 | let var_head = first_node_text(var); | ||
105 | println!("{:?} {:?}", pat_head, var_head); | ||
106 | |||
107 | pat_head == var_head | ||
108 | } | ||
109 | |||
110 | // Assist: replace_match_with_if_let | 93 | // Assist: replace_match_with_if_let |
111 | // | 94 | // |
112 | // Replaces a binary `match` with a wildcard pattern and no guards with an `if let` expression. | 95 | // 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<ast::Expr> { | |||
248 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { | 248 | pub(crate) fn next_prev() -> impl Iterator<Item = Direction> { |
249 | [Direction::Next, Direction::Prev].iter().copied() | 249 | [Direction::Next, Direction::Prev].iter().copied() |
250 | } | 250 | } |
251 | |||
252 | pub(crate) fn does_pat_match_variant(pat: &ast::Pat, var: &ast::Pat) -> bool { | ||
253 | let first_node_text = |pat: &ast::Pat| pat.syntax().first_child().map(|node| node.text()); | ||
254 | |||
255 | let pat_head = match pat { | ||
256 | ast::Pat::IdentPat(bind_pat) => { | ||
257 | if let Some(p) = bind_pat.pat() { | ||
258 | first_node_text(&p) | ||
259 | } else { | ||
260 | return pat.syntax().text() == var.syntax().text(); | ||
261 | } | ||
262 | } | ||
263 | pat => first_node_text(pat), | ||
264 | }; | ||
265 | |||
266 | let var_head = first_node_text(var); | ||
267 | |||
268 | pat_head == var_head | ||
269 | } | ||