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: "replace all occurrences" variants of refactor.{inline,extract}.variable #70085

Open
xzbdmw opened this issue Oct 29, 2024 · 7 comments
Labels
FeatureRequest gopls Issues related to the Go language server, gopls. 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 29, 2024

Original title: codeaction: replace every use-case of a variable with its defined expression, extract every identical expression within function to a new variable

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-build1846322804=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

iShot_2024-10-29_11.52.53.mp4

the first half part is :Rafactor inline_var, and second part is :Rafactor extract_var new_var, it is from a nvim plugin refactoring.nvim using treesitter under the hood, I use this two quite frequently to eliminate unnecessary variable as well as introduce new variable for some repeated expression, the problem is it fails (or not accurate) in complex situations, so I wish gopls would support this directly.

What did you see happen?

Currently gopls supports extract a single expression to a new variable, I'd say extract every repeated expression under selection would be more useful, since the latter contains the former.

What did you expect to see?

see above

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 29, 2024
@gopherbot gopherbot added this to the Unreleased milestone Oct 29, 2024
@gopherbot
Copy link
Contributor

Change https://go.dev/cl/623637 mentions this issue: gopls/internal/golang: use correct imports in HTML pkg doc links

@adonovan adonovan changed the title x/tools/gopls: codeaction to replace every use-case of a variable with its defined expression, extract every identical expression within function to a new variable x/tools/gopls: "replace all occurrences" variants of refactor.{inline,extract}.variable Oct 30, 2024
@adonovan adonovan added the Refactoring Issues related to refactoring tools label Oct 30, 2024
@xzbdmw
Copy link
Author

xzbdmw commented Oct 31, 2024

Hmm, how could I know two expression is identical regardless their tok.Pos value? Am I supposed to write a util function similar to types.Identical()?

@gopherbot
Copy link
Contributor

Change https://go.dev/cl/624035 mentions this issue: gopls/internal/codeaction: replace all occurrences of expression (refactor.extract.variable.all)

@xzbdmw
Copy link
Author

xzbdmw commented Nov 13, 2024

@adonovan Hey Alan, may you take a look? The logic is relatively simple :)

@adonovan
Copy link
Member

Sorry for the delay and thanks for the contribution, but I am overwhelmed with reviews at the moment and am also out of office at a Go conference. I will attend to this next week.

@xzbdmw
Copy link
Author

xzbdmw commented Nov 13, 2024

Thanks! No rush—enjoy the conference!

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. Refactoring Issues related to refactoring tools Tools This label describes issues relating to any tools in the x/tools repository.
Projects
None yet
Development

No branches or pull requests

5 participants