diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-02-05 14:28:25 +0000 |
---|---|---|
committer | GitHub <[email protected]> | 2021-02-05 14:28:25 +0000 |
commit | b89fef522043f0fe4dc1977059b70bbd20d6fd75 (patch) | |
tree | f9cc2fc85cd7233351c897f725585efe55b867c6 /crates/assists/src/tests/generated.rs | |
parent | 5009958847efa5d3cd85f2a9a84074069ca2088d (diff) | |
parent | dfd751303ec6336a4a78776eb8030790b7b0b000 (diff) |
Merge #7562
7562: add `generate_enum_match` assist r=matklad a=yoshuawuyts
This adds a `generate_enum_match` assist, which generates `is_` variants for enums (e.g. `Option::{is_none,is_some}` in std). This is my first attempt at contributing to Rust-Analyzer, so I'm not sure if I've gotten everything right. Thanks!
## Example
**Input**
```rust
pub(crate) enum Variant {
Undefined,
Minor, // cursor here
Major,
}
```
**Output**
```rust
pub(crate) enum Variant {
Undefined,
Minor,
Major,
}
impl Variant {
pub(crate) fn is_minor(&self) -> bool {
matches!(self, Self::Minor)
}
}
```
## Future Directions
I made this as a stepping stone for some of the more involved refactors (e.g. #5944). I'm not sure yet how to create, use, and test `window.showQuickPick`-based asssists in RA. But once that's possible, it'd probably be nice to be able to generate match methods in bulk through the quickpick UI rather than one-by-one:
```
[x] Select enum members to generate methods for. (3 selected) [ OK ]
---------------------------------------------------------------------------
[x] Undefined
[x] Minor
[x] Major
```
Co-authored-by: Yoshua Wuyts <[email protected]>
Co-authored-by: Yoshua Wuyts <[email protected]>
Diffstat (limited to 'crates/assists/src/tests/generated.rs')
-rw-r--r-- | crates/assists/src/tests/generated.rs | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/crates/assists/src/tests/generated.rs b/crates/assists/src/tests/generated.rs index e84f208a3..960815bd9 100644 --- a/crates/assists/src/tests/generated.rs +++ b/crates/assists/src/tests/generated.rs | |||
@@ -460,6 +460,33 @@ struct Point { | |||
460 | } | 460 | } |
461 | 461 | ||
462 | #[test] | 462 | #[test] |
463 | fn doctest_generate_enum_match_method() { | ||
464 | check_doc_test( | ||
465 | "generate_enum_match_method", | ||
466 | r#####" | ||
467 | enum Version { | ||
468 | Undefined, | ||
469 | Minor$0, | ||
470 | Major, | ||
471 | } | ||
472 | "#####, | ||
473 | r#####" | ||
474 | enum Version { | ||
475 | Undefined, | ||
476 | Minor, | ||
477 | Major, | ||
478 | } | ||
479 | |||
480 | impl Version { | ||
481 | fn is_minor(&self) -> bool { | ||
482 | matches!(self, Self::Minor) | ||
483 | } | ||
484 | } | ||
485 | "#####, | ||
486 | ) | ||
487 | } | ||
488 | |||
489 | #[test] | ||
463 | fn doctest_generate_from_impl_for_enum() { | 490 | fn doctest_generate_from_impl_for_enum() { |
464 | check_doc_test( | 491 | check_doc_test( |
465 | "generate_from_impl_for_enum", | 492 | "generate_from_impl_for_enum", |