gopls禁用结构体自动填充。

huangapple go评论68阅读模式
英文:

gopls disable struct auto-fill

问题

我已经使用Neovim+nvim-cmp编写了Golang代码。语言服务器gopls总是自动填充结构体的初始值。例如:

a := &MyStruct{}

保存文件后,它会修改结构体为:

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

似乎只有在光标当前位于结构体内部时才会这样做,但是,有没有办法禁用这个功能?或者通过热键手动触发这个功能?

英文:

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?

答案1

得分: 2

有一个名为fillstruct的分析器选项,在这里有描述,它会导致问题,并且默认情况下是启用的。将其设置为false后问题得到解决:

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

看起来,VSCode也默认启用了这个选项,在这里有描述,但它不会自动填充。相反,它会显示一个黄色的灯泡,让用户点击进行填充。

英文:

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.

答案2

得分: 0

我没有看到这种行为,在使用最新的VSCode 1.66.1、Go 1.18和gopls 0.32.0(1)以及gopls v0.8.2(2)时。

首先,请检查一下,如果你移除了所有其他可能与你的VSCode有关的插件,问题是否仍然存在。

英文:

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.

huangapple
  • 本文由 发表于 2022年4月11日 04:32:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/71820343.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定