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: Add refactor "move to a new file" #65707

Open
golopot opened this issue Feb 14, 2024 · 4 comments · May be fixed by golang/tools#479
Open

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

golopot opened this issue Feb 14, 2024 · 4 comments · May be fixed by golang/tools#479
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

@golopot
Copy link

golopot commented Feb 14, 2024

gopls version

Build info ---------- golang.org/x/tools/gopls v0.14.2 golang.org/x/tools/gopls@v0.14.2 h1:sIw6vjZiuQ9S7s0auUUkHlWgsCkKZFWDHmrge8LYsnc= github.com/BurntSushi/toml@v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak= github.com/google/go-cmp@v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38= github.com/sergi/go-diff@v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0= golang.org/x/exp/typeparams@v0.0.0-20221212164502-fae10dda9338 h1:2O2DON6y3XMJiQRAS1UWU+54aec2uopH3x7MAiqGW6Y= golang.org/x/mod@v0.14.0 h1:dGoOF9QVLYng8IHTm7BAyWqCqSheQ5pYWGhzW00YJr0= golang.org/x/sync@v0.4.0 h1:zxkM55ReGkDlKSM+Fu41A+zmbZuaPVbGMzvvdUPznYQ= golang.org/x/sys@v0.14.0 h1:Vz7Qs629MkJkGyHxUlRHizWJRG2j8fbQKjELVSNhy7Q= golang.org/x/telemetry@v0.0.0-20231114163143-69313e640400 h1:brbkEFfGwNGAEkykUOcryE/JiHUMMJouzE0fWWmz/QU= golang.org/x/text@v0.13.0 h1:ablQoSUd0tRdKxZewP80B+BaqeKJuVhuRxj/dkrun3k= golang.org/x/tools@v0.14.1-0.20231114185516-c9d3e7de13fd h1:Oku7E+OCrXHyst1dG1z10etCTxewCHXNFLRlyMPbh3w= golang.org/x/vuln@v1.0.1 h1:KUas02EjQK5LTuIx1OylBQdKKZ9jeugs+HiqO5HormU= honnef.co/go/tools@v0.4.5 h1:YGD4H+SuIOOqsyoLOpZDWcieM28W47/zRO7f+9V3nvo= mvdan.cc/gofumpt@v0.4.0 h1:JVf4NN1mIpHogBj7ABpgOyZc65/UUOkKQFkoURsz4MM= mvdan.cc/xurls/v2@v2.4.0 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 golopot 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 Feb 14, 2024
@gopherbot gopherbot added this to the Unreleased milestone Feb 14, 2024
@findleyr findleyr added Refactoring Issues related to refactoring tools FeatureRequest labels Feb 14, 2024
@findleyr findleyr modified the milestones: Unreleased, gopls/backlog Feb 14, 2024
@findleyr
Copy link
Contributor

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

@golopot
Copy link
Author

golopot commented Feb 15, 2024

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.

@adonovan
Copy link
Member

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.

golopot added a commit to golopot/tools that referenced this issue Feb 22, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
golopot added a commit to golopot/tools that referenced this issue Feb 22, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
golopot added a commit to golopot/tools that referenced this issue Feb 22, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
@gopherbot
Copy link

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

golopot added a commit to golopot/tools that referenced this issue Feb 22, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
golopot added a commit to golopot/tools that referenced this issue Feb 23, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
golopot added a commit to golopot/tools that referenced this issue Feb 28, 2024
This code action moves selected code sections to a newly created file
within the same package. The created filename is chosen as the first
{function, type, const, var} name encountered. In addition, import
declarations are added or removed as needed.

Fixes golang/go#65707
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

Successfully merging a pull request may close this issue.

4 participants