rust-lang / rust-analyzer

A Rust compiler front-end for IDEs
https://rust-analyzer.github.io/
Apache License 2.0
14.24k stars 1.6k forks source link

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

Open Zerowalker opened 2 years ago

Zerowalker commented 2 years ago

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 commented 2 years ago

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

feniljain commented 2 years ago

@rustbot claim