Skip to content

[Feature Request] Generate getter/setter for all/multiple fields #13246

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

Open
Zerowalker opened this issue Sep 16, 2022 · 2 comments
Open

[Feature Request] Generate getter/setter for all/multiple fields #13246

Zerowalker opened this issue Sep 16, 2022 · 2 comments
Assignees
Labels
A-assists C-feature Category: feature request S-actionable Someone could pick this issue up and work on it right now

Comments

@Zerowalker
Copy link

Zerowalker commented Sep 16, 2022

Not sure if this is "easy" to implement, tried looking at the code and it looks to be limited to 1 "item".
But will ask nevertheless in case I'm wrong:)

So say you got:

pub struct Foo {
    alpha: u32,
    bar: u32,
    beta: u32,
    gamma: u32,
}

It would be nice if you could have the action generate getter/setter for all fields and/or multiple fields,
perhaps selected in the editor, though it feels a bit hacky as they might not be in order.

So if you did generate getter/setter for all fields it would end up with asking you the: generate a X method,
and if we chose generate a getter method it would end up as:

pub struct Foo {
    alpha: u32,
    bar: u32,
    beta: u32,
    gamma: u32,
}

impl Foo {
    pub fn alpha(&self) -> u32 {
        self.alpha
    }

    pub fn bar(&self) -> u32 {
        self.bar
    }

    pub fn beta(&self) -> u32 {
        self.beta
    }

    pub fn gamma(&self) -> u32 {
        self.gamma
    }
}

It's just a "simple" convenience and i completely understand it's uses are small so I'm pushing hard for it just a simple request:)

Thanks everyone contributing to this project, all your work is very much appreciated!

@jonas-schievink
Copy link
Contributor

Assists can take the current selection into account, we could use that to avoid having 2 assists that do the same thing, and just make the existing one smarter

@jonas-schievink jonas-schievink added S-actionable Someone could pick this issue up and work on it right now A-assists C-feature Category: feature request labels Sep 16, 2022
@feniljain
Copy link
Contributor

@rustbot claim

bors added a commit that referenced this issue Oct 20, 2022
feat: add multiple getters mode in `generate_getter`

This commit adds two modes to generate_getter action.
First, the plain old working on single fields.
Second, working on a selected range of fields.

Should partially solve #13246
If this gets approved will create a separate PR for setters version of the same

### Points to help in review:

- `generate_getter_from_record_info` contains code which is mostly taken from assist before refactor
- Same goes for `parse_record_fields`
- There are changes in other assists, as one of the methods in utils named `find_struct_impl` is changed, before it used to accept a single `fn_name`, now it takes a list of function names to check against. All old impls are updated to create a small list to pass their single element.

### Assumptions:

- If any of the fields have an implementation, the action will quit.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-assists C-feature Category: feature request S-actionable Someone could pick this issue up and work on it right now
Projects
None yet
Development

No branches or pull requests

3 participants