Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

x/tools/gopls: disable code actions from gopls #68783

Closed
xtrats opened this issue Aug 8, 2024 · 11 comments
Closed

x/tools/gopls: disable code actions from gopls #68783

xtrats opened this issue Aug 8, 2024 · 11 comments
Assignees
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@xtrats
Copy link

xtrats commented Aug 8, 2024

gopls version

Build info
----------
golang.org/x/tools/gopls v0.16.1
    golang.org/x/tools/[email protected] h1:1hO/dCeUvjEYx3V0rVvCtOkwnpEpqS29paE+Jw4dcAc=
    github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak=
    github.com/google/[email protected] h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
    golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y=
    golang.org/x/[email protected] h1:5+9lSbEzPSdWkH32vYPBwEpX8KwDbM52Ud9xBUvNlb0=
    golang.org/x/[email protected] h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M=
    golang.org/x/[email protected] h1:3Wt8mZlbFwG8llny+t18kh7AXxyWePFycXMuVdHxnyM=
    golang.org/x/[email protected] h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
    golang.org/x/[email protected] h1:Kd+Z5Pm6uwYx3T2KEkeHMHUMZxDPb/q6b1m+zEcy62c=
    golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
    honnef.co/go/[email protected] h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
    mvdan.cc/[email protected] h1:G3QvahNDmpD+Aek/bNOLrFR2XC6ZAdo62dZu65gmwGo=
    mvdan.cc/xurls/[email protected] 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:

image

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

@xtrats xtrats added gopls Issues related to the Go language server, gopls. Tools This label describes issues relating to any tools in the x/tools repository. labels Aug 8, 2024
@gopherbot gopherbot added this to the Unreleased milestone Aug 8, 2024
@hyangah hyangah added FeatureRequest NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. labels Aug 8, 2024
@hyangah hyangah modified the milestones: Unreleased, gopls/backlog Aug 8, 2024
@hyangah
Copy link
Contributor

hyangah commented Aug 8, 2024

cc @adonovan

@adonovan
Copy link
Member

adonovan commented Aug 8, 2024

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.

@findleyr
Copy link
Contributor

findleyr commented Aug 8, 2024

@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.

@xtrats
Copy link
Author

xtrats commented Aug 9, 2024

@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.

image

@adonovan
Copy link
Member

adonovan commented Aug 9, 2024

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.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/604818 mentions this issue: gopls/internal/settings: use CodeActionKind hierarchy

gopherbot pushed a commit to golang/tools that referenced this issue Sep 12, 2024
This change causes all CodeActions returned by gopls to use a
specific leaf type such as refactor.inline.call, instead of a
general category such as refactor or refactor.inline. The
categories may continue to be specified in the "Only" parameter
of a CodeActions request, and they apply hierarchically.
This allows clients to be more specific in requesting a
particular Code Action.

Details of golang/codeaction.go (messy diff):
- don't separate "typed" from "syntax only" operations;
  in practice we always need types.
- enabled (func) replaces want (map). It is computed
  in server.CodeAction and plumbed down.
- the "add" helper wraps a Command in a CodeAction
  and adds it to the result.

Also:
- use camelCase names, following LSP's source.organizeImports.
- document the specific kinds in the user manual
  and add a release note.
- remove the CodeAction title regexp parameter from
  @codeaction{,err} marker tests since the kind is
  now sufficiently expressive.

Fixes golang/go#40438
Updates golang/go#68791
Updates golang/go#68783

Change-Id: I1898f79d8af441a0376ad5067d05e0621840d987
Reviewed-on: https://go-review.googlesource.com/c/tools/+/604818
Reviewed-by: Robert Findley <[email protected]>
Auto-Submit: Alan Donovan <[email protected]>
LUCI-TryBot-Result: Go LUCI <[email protected]>
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/616837 mentions this issue: gopls/internal/server: CodeAction: interpret Only=[] as [QuickFix]

@xzbdmw
Copy link

xzbdmw commented Oct 2, 2024

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"
  }
}

@adonovan
Copy link
Member

adonovan commented Oct 2, 2024

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.

@xzbdmw
Copy link

xzbdmw commented Oct 2, 2024

Ah, sorry, ignore this, I'm running an outdated branch..

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. NeedsDecision Feedback is required from experts, contributors, and/or the community before a change can be made. Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

7 participants