rust-lang / rust-lang/rust-analyzer

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

Open
#13,246 2 comments 0 reactions 1 assignee View on GitHub

@feniljain is already working on this.

Since Oct 7, 2022.

A-assists C-feature S-actionable
Dominant language
Rust
Stars
16.9k
Forks
2.2k
Avg merge
1d 12h
Merged PRs (30d)
72

Description

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!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.