vscode-go icon indicating copy to clipboard operation
vscode-go copied to clipboard

"Add field to struct" code generation action

Open Quantumplation opened this issue 3 years ago • 1 comments

Is your feature request related to a problem? Please describe. When implementing a new feature, I often add a new field to a struct first by writing out the usage of it, then hunting down the struct and adding the field. This breaks he development flow of the new feature.

Describe the solution you'd like Many IDEs provide the ability to automatically add a new field to the struct. When a struct field is undefined, VS Code would provide a Code Action to add this field to the struct, ideally with an inferred type.

For example, if I have the following struct:

type Opts struct {
  Environment string
}

and in the body of my main function, I add

  server.Listen("0.0.0.0", opts.Port);

The code action would ideally infer that the parameter expects an integer, and modify the Opts struct as follows:

type Opts struct {
  Environment string
  Port int
}

Describe alternatives you've considered An alternative solution, if type inference is difficult to provide, would be to bring the editor to the struct with a field added and the cursor where the type would go (much like inserting a snippet), and after the type was provided and the user hit enter, bring them back to wherever they were editing before.

Quantumplation avatar May 26 '22 21:05 Quantumplation

Thanks for the suggestion! This seems like it would be a helpful feature.

findleyr avatar Jun 02 '22 16:06 findleyr