Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add generate_enum_match assist #7562

Merged
merged 2 commits into from
Feb 5, 2021

Conversation

yoshuawuyts
Copy link
Member

@yoshuawuyts yoshuawuyts commented Feb 5, 2021

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

pub(crate) enum Variant {
    Undefined,
    Minor, // cursor here
    Major,
}

Output

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

@yoshuawuyts
Copy link
Member Author

Yay, CI has passed. I think this is ready for a review now!

Copy link
Member

@matklad matklad left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM overall ❤️ !

crates/assists/src/handlers/generate_enum_match_method.rs Outdated Show resolved Hide resolved
crates/assists/src/handlers/generate_enum_match_method.rs Outdated Show resolved Hide resolved
crates/assists/src/handlers/generate_enum_match_method.rs Outdated Show resolved Hide resolved
crates/assists/src/handlers/generate_enum_match_method.rs Outdated Show resolved Hide resolved
@matklad
Copy link
Member

matklad commented Feb 5, 2021

see microsoft/language-server-protocol#1164 for "quick pick" related discussion.

@matklad
Copy link
Member

matklad commented Feb 5, 2021

If you feel sufiiciently motivated, I'd love to see the impl of this idea:

For each refactor type, Dart documents a specific JSON format with data to be shown to the user as a feedback, and JSON format for the user's reply. It says nothing how the data is displayed in the UI -- client implements specific GUI for each refactor. This obviously means manual work for each (client, refactor) pair, but allows for the best possible experience.

An interesting facet here is that the communication protocol for a refactor is stateless. This works a bit like HTTP GET/POST for a form. Rather than the server recording that user X now views form Y after X does GET Y, the corresponding POST Y contains all the info required to restore the context.

See 2075e77 for how to implement extensions to the protocl.

@yoshuawuyts
Copy link
Member Author

Okay, update the PR with all proposed changes. This includes moving find_struct_impl to the assist utils!

@yoshuawuyts
Copy link
Member Author

If you feel sufiiciently motivated, I'd love to see the impl of this idea:

This will depend on my availability, but I may give it a shot. Thanks so much for sharing these references so far. It's giving me a better idea of what would be required to do this, and I may end up giving it a shot!

@matklad
Copy link
Member

matklad commented Feb 5, 2021

bors r+

@bors
Copy link
Contributor

bors bot commented Feb 5, 2021

@bors bors bot merged commit b89fef5 into rust-lang:master Feb 5, 2021
@yoshuawuyts yoshuawuyts deleted the generate-enum-match branch February 5, 2021 14:45
bors bot added a commit that referenced this pull request Feb 5, 2021
7570: Add doc gen to the `generate_enum_match_method` assist r=yoshuawuyts a=yoshuawuyts

Implements a small extension to #7562, generating default comments. I wasn't sure if this would fit the goals of Rust-Analyzer, so I chose to split it into a separate PR. This is especially useful when writing code in a codebase which uses `#![warn(missing_docs)]` lint, as many production-grade libraries do.

The comments we're generating here are similar to the ones found on [`Option::is_some`](https://doc.rust-lang.org/std/option/enum.Option.html#method.is_some) and [`Result::is_err`](https://doc.rust-lang.org/std/result/enum.Result.html#method.is_err). I briefly considered only generating these for `pub` types, but they seem small and unobtrusive enough that they're probably useful in the general case. Thanks!

## Example

__input__
```rust
pub(crate) enum Variant {
    Undefined,
    Minor, // cursor here
    Major,
}
```

__output__
```rust
pub(crate) enum Variant {
    Undefined,
    Minor,
    Major,
}

impl Variant {
    /// Returns `true` if the variant is [`Minor`].
    pub(crate) fn is_minor(&self) -> bool {
        matches!(self, Self::Minor)
    }
}
```

## Future Directions

This opens up the path to adding an assist for generating these comments on existing `is_` methods. This would make it both easy to document new code, and update existing code with documentation.

7571: Cleanup decl_check r=Veykril a=Veykril

bors r+

Co-authored-by: Yoshua Wuyts <[email protected]>
Co-authored-by: Lukas Wirth <[email protected]>
bors bot added a commit that referenced this pull request Feb 5, 2021
7572: Add `find_or_create_impl_block` to assist utils r=matklad a=yoshuawuyts

This is another continuation of #7562, introducing a small util to either find an `impl` block, or create a new one if none exists. I copied this code from the `generate_new` assist into #7562, and this unifies both into a helper.

It doesn't feel super polished in its current state, but my hope is that this is enough of a starting point that it can be expanded on later. For example something that would be useful would be a flag which either returns the index of the start of the block, or the end of the block.

Anyway, I hope this is useful. Thanks!

Co-authored-by: Yoshua Wuyts <[email protected]>
@lnicola
Copy link
Member

lnicola commented Feb 8, 2021

generate_enum_match

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants