gopls version
Build info
----------
golang.org/x/tools/gopls v0.16.1
golang.org/x/tools/gopls@v0.16.1 h1:1hO/dCeUvjEYx3V0rVvCtOkwnpEpqS29paE+Jw4dcAc=
github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
github.com/google/go-cmp@v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
golang.org/x/mod@v0.18.0 h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
golang.org/x/sync@v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
golang.org/x/telemetry@v0.0.0-20240607193123-221703e18637 h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
golang.org/x/text@v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/tools@v0.22.1-0.20240628205440-9c895dd76b34 h1:Kd+Z5Pm6uwYx3T2KEkeHMHUMZxDPb/q6b1m+zEcy62c=
golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/tools@v0.4.7 h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/gofumpt@v0.6.0 h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
mvdan.cc/xurls/v2@v2.5.0 h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.22.4
go env
GO111MODULE=''
GOARCH='amd64'
GOBIN=''
GOCACHE='/root/.cache/go-build'
GOENV='/root/.config/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='amd64'
GOHOSTOS='linux'
GOINSECURE=''
GOMODCACHE='/root/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='linux'
GOPATH='/root/go'
GOPRIVATE=''
GOPROXY='-- private --'
GOROOT='/usr/local/go'
GOSUMDB='-- private --'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/linux_amd64'
GOVCS=''
GOVERSION='go1.22.4'
GCCGO='gccgo'
GOAMD64='v1'
AR='ar'
CC='gcc'
CXX='g++'
CGO_ENABLED='1'
GOMOD='/data/project/go.mod'
GOWORK=''
CGO_CFLAGS='-O2 -g'
CGO_CPPFLAGS=''
CGO_CXXFLAGS='-O2 -g'
CGO_FFLAGS='-O2 -g'
CGO_LDFLAGS='-O2 -g'
PKG_CONFIG='pkg-config'
GOGCCFLAGS='-fPIC -m64 -pthread -Wl,--no-gc-sections -fmessage-length=0 -ffile-prefix-map=/tmp/go-build4255324789=/tmp/go-build -gno-record-gcc-switches'
What did you do?
There are two code actions from gopls
on every single line of the source code, which will drown out the truly useful ones:
What did you see happen?
Refer to the previous section.
What did you expect to see?
I would like to be able to disable certain specific code actions of gopls through configuration.
Editor and settings
No response
Logs
No response
Comment From: gabyhelp
Related Issues and Documentation
- x/tools/gopls: Implement action should ignore importing the current package #61830 (closed)
- x/tools/gopls: codeAction returns invalid text edits #35458 (closed)
- x/tools/gopls: Add refactor "move to a new file" #65707 (closed)
- x/tools/gopls: gopls falls behind user input in large repository #45089 (closed)
- x/tools/gopls: negated build flag causes error #59346
- x/tools/gopls: #52425 (closed)
- x/tools/gopls: Is there any way to filter errors? #68624 (closed)
- x/tools/gopls: completion often uses all of 1s time budget #68063
- x/tools/gopls: gopls becomes unresponsive from time to time #61969 (closed)
- x/tools/gopls: gopls walks entire filesystem of a go module #52993 (closed)
(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)
Comment From: hyangah
cc @adonovan
Comment From: adonovan
I would like to be able to disable certain specific code actions of gopls through configuration.
Such control is supported directly by the Language Server Protocol: a CodeAction request has a field, only
, that allows the client to choose a subset of the code action kinds supported by the server. The default set can be found in the gopls source at gopls/internal/settings/default.go (unfortunately this list is not yet directly reflected in the documentation). If you remove "source.doc" and "source.assembly", then the server will not return these code actions.
I'll leave this issue open to address the question of whether the new defaults are inappropriate for a significant fraction of our users.
Comment From: findleyr
@xtrats which editor and LSP client are you using? In VS Code, these actions are less obtrusive, only appearing in the nested source action menu. It may be that they are more distracting in your editor than in others.
Comment From: xtrats
@xtrats which editor and LSP client are you using? In VS Code, these actions are less obtrusive, only appearing in the nested source action menu. It may be that they are more distracting in your editor than in others.
I'm using neovim v0.10.1 with "neovim/nvim-lspconfig" plugin, using the "glepnir/lspsaga.nvim" and "aznhe21/actions-preview.nvim" plugins to discover and preview code actions. When there are available code actions for the current line where the cursor is located, a lightbulb icon will be displayed, so a lightbulb icon will be shown on every line.
Comment From: adonovan
When there are available code actions for the current line where the cursor is located, a lightbulb icon will be displayed, so a lightbulb icon will be shown on every line.
One possibility is for us to reduce the number of code actions that gopls offers based on cursor location alone, and instead require the user to select a non-empty region of text (e.g. an identifier or part of one) before offering code actions such as source.doc and source.assembly. I wonder whether there are any established UI conventions we can rely on here.
Comment From: gopherbot
Change https://go.dev/cl/604818 mentions this issue: gopls/internal/settings: use CodeActionKind hierarchy
Comment From: gopherbot
Change https://go.dev/cl/616837 mentions this issue: gopls/internal/server: CodeAction: interpret Only=[] as [QuickFix]
Comment From: xzbdmw
Maybe code actions fire from visual select should return all kinds? the new commit makes extract function
,extract method
disappear in neovim builtin lsp vim.lsp.buf.code_action()
This is the param it sends to gopls, missing only
field
{
context = {
diagnostics = {},
triggerKind = 1
},
range = {
["end"] = <1>{
character = 1,
line = 284
},
start = <table 1>
},
textDocument = {
uri = "file:///Users/xzb/Project/Go/gtools/gopls/internal/golang/stub.go"
}
}
Comment From: adonovan
the new commit makes extract function,extract method disappear in neovim builtin lsp vim.lsp.buf.code_action()
That's surprising; the missing "only" and the triggerKind=1 (invoked) should cause the server to return all code actions. I will investigate.
Comment From: xzbdmw
Ah, sorry, ignore this, I'm running an outdated branch..
Comment From: JasperSurmont
@xtrats which editor and LSP client are you using? In VS Code, these actions are less obtrusive, only appearing in the nested source action menu. It may be that they are more distracting in your editor than in others.
I'm using neovim v0.10.1 with "neovim/nvim-lspconfig" plugin, using the "glepnir/lspsaga.nvim" and "aznhe21/actions-preview.nvim" plugins to discover and preview code actions. When there are available code actions for the current line where the cursor is located, a lightbulb icon will be displayed, so a lightbulb icon will be shown on every line.
I am in the same boat, have you found a way to only select certain code actions (whether it be with the only setting or not?
Comment From: xtrats
I am in the same boat, have you found a way to only select certain code actions (whether it be with the only setting or not?
Sorry, still no solution. 😓
Comment From: JasperSurmont
@xtrats this might help. I was able to make it work using the actions_preview and lightbulb plugins.
You can add any code actions to be ignored to ignoreCodeActions
local ignoreCodeActions = {
["source.doc"] = true,
["source.freesymbols"] = true,
["source.addTest"] = true,
["source.assembly"] = true,
["gopls.doc.features"] = true,
["source.toggleCompilerOptDetails"] = true,
}
local code_actions = require'actions-preview'
code_actions.setup{}
-- The restrictive code actions
vim.keymap.set({ 'v', 'n'}, '<leader>ca', function()
code_actions.code_actions{
filter = function(ca)
return not ignoreCodeActions[ca.kind]
end
}
end)
-- All code actions
vim.keymap.set({ 'v', 'n'}, '<leader>aca', code_actions.code_actions)
require'nvim-lightbulb'.setup{
autocmd = { enabled = true },
filter = function(client, ca)
if client ~= "gopls" then
return true
end
return not ignoreCodeActions[ca.kind]
end
}
Comment From: xtrats
@xtrats this might help. I was able to make it work using the actions_preview and lightbulb plugins.
You can add any code actions to be ignored to
ignoreCodeActions
local ignoreCodeActions = { ["source.doc"] = true, ["source.freesymbols"] = true, ["source.addTest"] = true, ["source.assembly"] = true, ["gopls.doc.features"] = true, ["source.toggleCompilerOptDetails"] = true, }
local code_actions = require'actions-preview' code_actions.setup{}
-- The restrictive code actions vim.keymap.set({ 'v', 'n'}, '
ca', function() code_actions.code_actions{ filter = function(ca) return not ignoreCodeActions[ca.kind] end } end) -- All code actions vim.keymap.set({ 'v', 'n'}, '
aca', code_actions.code_actions) require'nvim-lightbulb'.setup{ autocmd = { enabled = true }, filter = function(client, ca) if client ~= "gopls" then return true end return not ignoreCodeActions[ca.kind] end }
Thanks, it works. 🙏
Comment From: Apollorion
If anyone is using navigator, this is how you can disable these actions there as well:
local ignoreCodeActions = {
["source.doc"] = true,
["source.freesymbols"] = true,
["source.addTest"] = true,
["source.assembly"] = true,
["gopls.doc.features"] = true,
["source.toggleCompilerOptDetails"] = true,
}
require("navigator").setup {
lsp = {
code_action = {
enable = true,
prompt = { virtual_text = true },
view = "split",
filter = function(client, ca)
if client ~= "gopls" then
return true
end
return not ignoreCodeActions[ca.kind]
end,
},
},
}