diff options
Diffstat (limited to 'crates/assists/src/handlers/fill_match_arms.rs')
-rw-r--r-- | crates/assists/src/handlers/fill_match_arms.rs | 49 |
1 files changed, 29 insertions, 20 deletions
diff --git a/crates/assists/src/handlers/fill_match_arms.rs b/crates/assists/src/handlers/fill_match_arms.rs index da47187e4..4964ddc7d 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), |
@@ -291,6 +272,34 @@ mod tests { | |||
291 | } | 272 | } |
292 | 273 | ||
293 | #[test] | 274 | #[test] |
275 | fn partial_fill_option() { | ||
276 | check_assist( | ||
277 | fill_match_arms, | ||
278 | r#" | ||
279 | enum Option<T> { Some(T), None } | ||
280 | use Option::*; | ||
281 | |||
282 | fn main() { | ||
283 | match None$0 { | ||
284 | None => {} | ||
285 | } | ||
286 | } | ||
287 | "#, | ||
288 | r#" | ||
289 | enum Option<T> { Some(T), None } | ||
290 | use Option::*; | ||
291 | |||
292 | fn main() { | ||
293 | match None { | ||
294 | None => {} | ||
295 | Some(${0:_}) => {} | ||
296 | } | ||
297 | } | ||
298 | "#, | ||
299 | ); | ||
300 | } | ||
301 | |||
302 | #[test] | ||
294 | fn partial_fill_or_pat() { | 303 | fn partial_fill_or_pat() { |
295 | check_assist( | 304 | check_assist( |
296 | fill_match_arms, | 305 | fill_match_arms, |