gopls disable struct auto-fill

1.2k views Asked by At

I've written Golang code using Neovim+nvim-cmp. The language server gopls always auto-fills structs with initial values. For example:

a := &MyStruct{}

After saving the file, it modifies the struct to:

a := &MyStruct{
  Int1: 0,
  String2: "",
  //...
  PointerN: nil,
}

Seems it only does this if the cursor is currently inside the struct, but still, any way to disable this feature? Or trigger this feature manually by hotkey?

2

There are 2 answers

0
VonC On

I do not see that behavior, using latest VSCode 1.66.1, Go 1.18 and gopls 0.32.0, with gopls v0.8.2.

Check first if the issue persists when you remove all other plugins you might have with your VSCode.

0
aj3423 On

There is an analyzer option fillstruct, described here, which causes the problem and is enabled by default. Problem solved after setting it to false:

lspconfig['gopls'].setup{
    cmd = {'gopls'},
    --...
    settings = {
        gopls = {
            analyses = {
                fillstruct = false,
            },
        },
    },
}

It seems that VSCode also enables this by default, described here, but it doesn't fill it automatically. Instead, it shows a yellow bulb to let user click to do the filling.