diff options
author | Dawer <[email protected]> | 2021-04-16 13:22:11 +0100 |
---|---|---|
committer | Dawer <[email protected]> | 2021-04-16 13:22:11 +0100 |
commit | 8965be3d0e5162c8a39c5ba1fd074740ac732566 (patch) | |
tree | 8e5cccb9a99518807384cc4a4551ddf63c5d7f9e /crates | |
parent | 042c248cc5fd9ce8f73ecdb99b6177a66add1b51 (diff) |
Fill match arms for a tuple of a single enum.
Diffstat (limited to 'crates')
-rw-r--r-- | crates/ide_assists/src/handlers/fill_match_arms.rs | 22 | ||||
-rw-r--r-- | crates/syntax/src/ast/make.rs | 18 |
2 files changed, 24 insertions, 16 deletions
diff --git a/crates/ide_assists/src/handlers/fill_match_arms.rs b/crates/ide_assists/src/handlers/fill_match_arms.rs index 878b3a3fa..80bd1b7e8 100644 --- a/crates/ide_assists/src/handlers/fill_match_arms.rs +++ b/crates/ide_assists/src/handlers/fill_match_arms.rs | |||
@@ -71,12 +71,6 @@ pub(crate) fn fill_match_arms(acc: &mut Assists, ctx: &AssistContext) -> Option< | |||
71 | return None; | 71 | return None; |
72 | } | 72 | } |
73 | 73 | ||
74 | // We do not currently support filling match arms for a tuple | ||
75 | // containing a single enum. | ||
76 | if enum_defs.len() < 2 { | ||
77 | return None; | ||
78 | } | ||
79 | |||
80 | // When calculating the match arms for a tuple of enums, we want | 74 | // When calculating the match arms for a tuple of enums, we want |
81 | // to create a match arm for each possible combination of enum | 75 | // to create a match arm for each possible combination of enum |
82 | // values. The `multi_cartesian_product` method transforms | 76 | // values. The `multi_cartesian_product` method transforms |
@@ -514,10 +508,7 @@ fn main() { | |||
514 | 508 | ||
515 | #[test] | 509 | #[test] |
516 | fn fill_match_arms_single_element_tuple_of_enum() { | 510 | fn fill_match_arms_single_element_tuple_of_enum() { |
517 | // For now we don't hande the case of a single element tuple, but | 511 | check_assist( |
518 | // we could handle this in the future if `make::tuple_pat` allowed | ||
519 | // creating a tuple with a single pattern. | ||
520 | check_assist_not_applicable( | ||
521 | fill_match_arms, | 512 | fill_match_arms, |
522 | r#" | 513 | r#" |
523 | enum A { One, Two } | 514 | enum A { One, Two } |
@@ -528,6 +519,17 @@ fn main() { | |||
528 | } | 519 | } |
529 | } | 520 | } |
530 | "#, | 521 | "#, |
522 | r#" | ||
523 | enum A { One, Two } | ||
524 | |||
525 | fn main() { | ||
526 | let a = A::One; | ||
527 | match (a, ) { | ||
528 | $0(A::One,) => {} | ||
529 | (A::Two,) => {} | ||
530 | } | ||
531 | } | ||
532 | "#, | ||
531 | ); | 533 | ); |
532 | } | 534 | } |
533 | 535 | ||
diff --git a/crates/syntax/src/ast/make.rs b/crates/syntax/src/ast/make.rs index c6a7b99b7..94d4f2cf0 100644 --- a/crates/syntax/src/ast/make.rs +++ b/crates/syntax/src/ast/make.rs | |||
@@ -29,9 +29,13 @@ pub fn ty(text: &str) -> ast::Type { | |||
29 | pub fn ty_unit() -> ast::Type { | 29 | pub fn ty_unit() -> ast::Type { |
30 | ty("()") | 30 | ty("()") |
31 | } | 31 | } |
32 | // FIXME: handle types of length == 1 | ||
33 | pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type { | 32 | pub fn ty_tuple(types: impl IntoIterator<Item = ast::Type>) -> ast::Type { |
34 | let contents = types.into_iter().join(", "); | 33 | let mut count: usize = 0; |
34 | let mut contents = types.into_iter().inspect(|_| count += 1).join(", "); | ||
35 | if count == 1 { | ||
36 | contents.push(','); | ||
37 | } | ||
38 | |||
35 | ty(&format!("({})", contents)) | 39 | ty(&format!("({})", contents)) |
36 | } | 40 | } |
37 | // FIXME: handle path to type | 41 | // FIXME: handle path to type |
@@ -292,11 +296,13 @@ pub fn wildcard_pat() -> ast::WildcardPat { | |||
292 | 296 | ||
293 | /// Creates a tuple of patterns from an iterator of patterns. | 297 | /// Creates a tuple of patterns from an iterator of patterns. |
294 | /// | 298 | /// |
295 | /// Invariant: `pats` must be length > 1 | 299 | /// Invariant: `pats` must be length > 0 |
296 | /// | ||
297 | /// FIXME handle `pats` length == 1 | ||
298 | pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat { | 300 | pub fn tuple_pat(pats: impl IntoIterator<Item = ast::Pat>) -> ast::TuplePat { |
299 | let pats_str = pats.into_iter().map(|p| p.to_string()).join(", "); | 301 | let mut count: usize = 0; |
302 | let mut pats_str = pats.into_iter().inspect(|_| count += 1).join(", "); | ||
303 | if count == 1 { | ||
304 | pats_str.push(','); | ||
305 | } | ||
300 | return from_text(&format!("({})", pats_str)); | 306 | return from_text(&format!("({})", pats_str)); |
301 | 307 | ||
302 | fn from_text(text: &str) -> ast::TuplePat { | 308 | fn from_text(text: &str) -> ast::TuplePat { |