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: feature: offer quickfix for variable name that is incorrectly initialized #69980

Open
xzbdmw opened this issue Oct 22, 2024 · 8 comments · May be fixed by golang/tools#545
Open
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FeatureRequest gopls/analysis Issues related to running analysis in gopls gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Milestone

Comments

@xzbdmw
Copy link

xzbdmw commented Oct 22, 2024

gopls version

Build info

golang.org/x/tools/gopls (devel)
golang.org/x/tools/gopls@(devel)
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:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0=
golang.org/x/[email protected] h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ=
golang.org/x/[email protected] h1:PfPrmVDHfPgLVpiYnf2R1uL8SCXBjkqT51+f/fQHR6Q=
golang.org/x/[email protected] h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM=
golang.org/x/[email protected] => ../
golang.org/x/[email protected] h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I=
honnef.co/go/[email protected] h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs=
mvdan.cc/[email protected] h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU=
mvdan.cc/xurls/[email protected] h1:lyBNOm8Wo71UknhUs4QTFUNNMyxy2JEIaKKo0RWOh+8=
go: go1.23.2

go env

GO111MODULE='auto'
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/xzb/Library/Caches/go-build'
GOENV='/Users/xzb/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/xzb/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/xzb/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/usr/local/go'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/usr/local/go/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.23.2'
GODEBUG=''
GOTELEMETRY='local'
GOTELEMETRYDIR='/Users/xzb/Library/Application Support/go/telemetry'
GCCGO='gccgo'
GOARM64='v8.0'
AR='ar'
CC='clang'
CXX='clang++'
CGO_ENABLED='1'
GOMOD=''
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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/gv/r110hgbx1gbgzp95kf_q71x40000gn/T/go-build2905454119=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

func main() {
	foo = 1  // incorrectly initialized
	bar := foo + 1  // no completion candidates item for `foo`
}

What did you see happen?

image

What did you expect to see?

offer completion for foo in second line bar := foo + 1 , offer a quickfix that add the missing : in first line.

Since an unused variable is always a error in go, the first line will have an error within expectation when writing from top to bottom, regardless this is a incorrectly initialized error rather than unused variable, which makes the lack of completion candidates surprising to me, further, the fix is annoy, because go-to-def does not work, I need to manually locate the first line where I miss a :.

If this quickfix exists, one could stop writing : to initialize variables, which requires two keystroke : shift+;, and let code action do the thing where it is later used. Even better, gopls can automatically add the missing : when user confirm the completion item.

Editor and settings

No response

Logs

No response

@xzbdmw xzbdmw 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 Oct 22, 2024
@gopherbot gopherbot added this to the Unreleased milestone Oct 22, 2024
@xzbdmw xzbdmw changed the title x/tools/gopls: feature: offer completion for variable name that is incorrectly initialized x/tools/gopls: feature: offer completion and quickfix for variable name that is incorrectly initialized Oct 22, 2024
@adonovan
Copy link
Member

adonovan commented Oct 22, 2024

The quickfix seems like a reasonable extension to gopls' existing https://pkg.go.dev/golang.org/x/tools/gopls/internal/analysis/undeclaredname analyzer.

@adonovan adonovan added the gopls/analysis Issues related to running analysis in gopls label Oct 22, 2024
@xzbdmw
Copy link
Author

xzbdmw commented Oct 22, 2024

How about auto adding missing : similar to auto-import when resolving completion, so that we no longer have to write colons anymore 😄

@findleyr
Copy link
Contributor

In order to do the completion aspect of this, we'd probably need to extract undeclared names from type errors, and make them available to lexical completion. This is doable.

This is really two separate issues. I'll file a separate issue for the completion issue.

@findleyr findleyr added FeatureRequest Refactoring Issues related to refactoring tools labels Oct 22, 2024
@findleyr findleyr changed the title x/tools/gopls: feature: offer completion and quickfix for variable name that is incorrectly initialized x/tools/gopls: feature: offer quickfix for variable name that is incorrectly initialized Oct 22, 2024
@findleyr findleyr added Analysis Issues related to static analysis (vet, x/tools/go/analysis) help wanted labels Oct 22, 2024
@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Oct 22, 2024
@findleyr
Copy link
Contributor

Filed #69993 for the completion request. As I said there, I think we're much more likely to add the quickfix, than we are to inject undeclared names into completion results.

@shashank-priyadarshi
Copy link

@findleyr I would like to take this up. However, having not contributed to the Go language before, I would require a bit of guidance on the objective.

@xzbdmw
Copy link
Author

xzbdmw commented Oct 24, 2024

@shashank-priyadarshi One useful tip is using goland to debug, I remember there is a test file called fix_test.go, construct a similar test for missing function, and set breakpoints in undeclaredname.go.

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/629895 mentions this issue: gopls/internal/undeclaredname: CodeAction: Add missing colon when appropriate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Analysis Issues related to static analysis (vet, x/tools/go/analysis) FeatureRequest gopls/analysis Issues related to running analysis in gopls gopls Issues related to the Go language server, gopls. help wanted Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants