go icon indicating copy to clipboard operation
go copied to clipboard

x/tools/gopls: Add refactor "move to a new file"

Open golopot opened this issue 1 year ago • 3 comments

gopls version

Build info ---------- golang.org/x/tools/gopls v0.14.2 golang.org/x/tools/[email protected] h1:sIw6vjZiuQ9S7s0auUUkHlWgsCkKZFWDHmrge8LYsnc= github.com/BurntSushi/[email protected] h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/[email protected] h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/sergi/[email protected] h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/exp/[email protected] h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/[email protected] h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/[email protected] h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/[email protected] h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/[email protected] h1:brbkEFfGwNGAEkykUOcryE/JiHUMMJouzE0fWWmz/QU= golang.org/x/[email protected] h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/[email protected] h1:Oku7E+OCrXHyst1dG1z10etCTxewCHXNFLRlyMPbh3w= golang.org/x/[email protected] h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/[email protected] h1:YGD4H+SuIOOqsyoLOpZDWcieM28W47/zRO7f+9V3nvo= mvdan.cc/[email protected] h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= mvdan.cc/xurls/[email protected] h1:tzxjVAj+wSBmDcF6zBB7/myTy3gX9xvi8Tyr28AuQgc= go: go1.21.6

go env

go env
GO111MODULE=''
GOARCH='arm64'
GOBIN=''
GOCACHE='/Users/chiawenchen/Library/Caches/go-build'
GOENV='/Users/chiawenchen/Library/Application Support/go/env'
GOEXE=''
GOEXPERIMENT=''
GOFLAGS=''
GOHOSTARCH='arm64'
GOHOSTOS='darwin'
GOINSECURE=''
GOMODCACHE='/Users/chiawenchen/go/pkg/mod'
GONOPROXY=''
GONOSUMDB=''
GOOS='darwin'
GOPATH='/Users/chiawenchen/go'
GOPRIVATE=''
GOPROXY='https://proxy.golang.org,direct'
GOROOT='/opt/homebrew/Cellar/go/1.21.6/libexec'
GOSUMDB='sum.golang.org'
GOTMPDIR=''
GOTOOLCHAIN='auto'
GOTOOLDIR='/opt/homebrew/Cellar/go/1.21.6/libexec/pkg/tool/darwin_arm64'
GOVCS=''
GOVERSION='go1.21.6'
GCCGO='gccgo'
AR='ar'
CC='cc'
CXX='c++'
CGO_ENABLED='1'
GOMOD='/Users/chiawenchen/testgopls/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 -arch arm64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -ffile-prefix-map=/var/folders/ls/sjfzwn0x09g0_ckr_7m5ybpc0000gn/T/go-build2538955256=/tmp/go-build -gno-record-gcc-switches -fno-common'

What did you do?

.

What did you see happen?

.

What did you expect to see?

When selecting severals lines containing functions, types, etc, offer a refactor for moving these things to a new file in the same package. The exact changes this refactor does is:

  1. Create a new file in the the same package. The name of the new file will be the name of the first entity selected. If a file of the same name already exists, append underscores until it doesn't.
  2. Add line "package packagename" to the new file
  3. Add selected lines to new file
  4. Add needed imports for the new file
  5. Remove selected lines from the old file
  6. Remove redundant imports from the source file

Editor and settings

No response

Logs

No response

golopot avatar Feb 14 '24 13:02 golopot

I agree this is a good and simple refactoring to apply. However we should fix AST comment handling (#20744) first -- something that we have on our radar for this year.

CC @adonovan @suzmue

findleyr avatar Feb 14 '24 21:02 findleyr

However we should fix AST comment handling (#20744) first

Is this issue somewhat depended on AST comment handling changes, or is it just a priority issue? in that case I can do the implementation.

golopot avatar Feb 15 '24 17:02 golopot

The feature is unlikely to work well until comment support is improved, but comment support will improve this year, so I don't think it should stop you from contributing this feature in the meantime, if that's something you're interested in doing.

adonovan avatar Feb 15 '24 18:02 adonovan

Change https://go.dev/cl/565895 mentions this issue: gopls/internal: add code action "move to a new file"

gopherbot avatar Feb 22 '24 01:02 gopherbot

Thanks @golopot for contributing this feature--and sorry for the review process took so long!

adonovan avatar Jun 26 '24 14:06 adonovan