Skip to content

Better Type Hints for Enum Structs/Tuples #2947

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

Closed
sanbox-irl opened this issue Jan 29, 2020 · 1 comment
Closed

Better Type Hints for Enum Structs/Tuples #2947

sanbox-irl opened this issue Jan 29, 2020 · 1 comment

Comments

@sanbox-irl
Copy link

Hello!

This is relatively small, but it might not be something the RA can do yet.

If I have an enum like this:

pub enum PlayerInputType {
    Start,
    PlayStaffCard(String),
    PickIssue(String),
    BooleanDecision(bool),
    PickPlayer(PlayerName),
    StubbedAction,
    Inaction,
    AdvanceAnIssue {
        issue_name: String,
        staff_card_name: String,
    },

Rust-analyzer can be a little strange with autocompletion here for the non-tuple variant (AdvanceAnIssue).

Here is what autocompletion/help looks like for PlayStaffCard:
image

That's very clear. I know that i should, after hitting tab, type a ( and then a String, like in the type inlay.

Here's the struct variant on the other hand:
image

It shows something similar, representing its data as a tuple! That's fine from a memory standpoint, but it means:

  1. I can't trust any tuple autocomplete anymore, since they might be a struct, so I'll have to click in and check
  2. I can't know which struct enum variants there are, so I'll need to go to reference for them as well.

I'm not sure what a better enum variant hint here would look like, but i suspect merely changing the type hint to just {String, String} as opposed to (String, String) would be good enough. Perhaps a real:

    AdvanceAnIssue {
        issue_name: String,
        staff_card_name: String,
    }

popping up would be the best long term solution, along with perhaps an assist in making all the fields if desired, but just the small change would be good enough I think.

Thank you!

bors bot added a commit that referenced this issue Feb 17, 2020
3169: Show record field names in Enum completion r=flodiebold a=adamrk

Adresses #2947.
Previously the details shown when autocompleting an Enum variant would look like the variant was a tuple even if it was a record:
![2020-02-16-15:59:32_crop](https://user-images.githubusercontent.com/16367467/74607233-64f21980-50d7-11ea-99db-e973e29c71d7.png)

This change will show the names of the fields for a record and use curly braces instead of parentheses:
![2020-02-16-15:33:00_crop](https://user-images.githubusercontent.com/16367467/74607251-8ce17d00-50d7-11ea-9d4d-38d198a4aec0.png)

This required exposing the type `adt::StructKind` from `ra_hir` and adding a function 
```
kind(self, db: &impl HirDatabase) -> StructKind
```
in the `impl` of `EnumVariant`. 

There was also a previously existing function `is_unit(self, db: &impl HirDatabase) -> bool` for `EnumVariant` which I removed because it seemed redundant after adding `kind`.

Co-authored-by: adamrk <[email protected]>
@matklad
Copy link
Member

matklad commented Feb 17, 2020

closed by #3169

@matklad matklad closed this as completed Feb 17, 2020
lnicola pushed a commit to lnicola/rust-analyzer that referenced this issue Apr 7, 2024
Try to fix cronjob PR creation

The main tests use `bash` several times, maybe that is the issue here, too?
RalfJung pushed a commit to RalfJung/rust-analyzer that referenced this issue Apr 27, 2024
Try to fix cronjob PR creation

The main tests use `bash` several times, maybe that is the issue here, too?
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

No branches or pull requests

2 participants