golang / go

The Go programming language
https://go.dev
BSD 3-Clause "New" or "Revised" License
124.3k stars 17.7k forks source link

x/tools/gopls: feature: offer quickfix for variable name that is incorrectly initialized #69980

Open xzbdmw opened 1 month ago

xzbdmw commented 1 month ago

gopls version

Build info

golang.org/x/tools/gopls (devel) golang.org/x/tools/gopls@(devel) 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.21.0 h1:vvrHzRwRfVKSiLrG+d4FMl/Qi4ukBCE6kZlTUkDYRT0= golang.org/x/sync@v0.8.0 h1:3NFvSEYkUoMifnESzZl15y791HH1qU2xm6eCJU5ZPXQ= golang.org/x/telemetry@v0.0.0-20240927184629-19675431963b h1:PfPrmVDHfPgLVpiYnf2R1uL8SCXBjkqT51+f/fQHR6Q= golang.org/x/text@v0.19.0 h1:kTxAhCbGbxhK0IwgSKiMO5awPoDQ0RpfiVYBfK860YM= golang.org/x/tools@v0.21.1-0.20240508182429-e35e4ccd0d2d => ../ golang.org/x/vuln@v1.0.4 h1:SP0mPeg2PmGCu03V+61EcQiOjmpri2XijexKdzv8Z1I= honnef.co/go/tools@v0.4.7 h1:9MDAWxMoSnB6QoSqiVr7P5mtkT9pOc1kSxchzPCnqJs= mvdan.cc/gofumpt@v0.7.0 h1:bg91ttqXmi9y2xawvkuMXyvAA/1ZGJqYAEGjXuP0JXU= mvdan.cc/xurls/v2@v2.5.0 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

gabyhelp commented 1 month ago

Related Issues and Documentation

(Emoji vote if this was helpful or unhelpful; more detailed feedback welcome in this discussion.)

adonovan commented 1 month ago

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

xzbdmw commented 1 month ago

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

findleyr commented 1 month ago

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 commented 1 month ago

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 commented 1 month ago

@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 commented 1 month ago

@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 commented 1 week ago

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