Skip to content

Commit bd3f524

Browse files
author
Dylan Le
committed
internal/lsp: rename all the package names in the renamed package
This CL contains a partial implementation of package renaming. Currently gopls is only able to rename references to the renaming package within the renaming package itself. prepareRename is still expected to return an error for renaming a package. For golang/go#41567 Change-Id: I3683a0a7128bba7620ef30db528f45b753e6c08f Reviewed-on: https://go-review.googlesource.com/c/tools/+/420219 Reviewed-by: Robert Findley <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Run-TryBot: Dylan Le <[email protected]>
1 parent 9f65685 commit bd3f524

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

gopls/internal/regtest/misc/rename_test.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import (
1010

1111
"golang.org/x/tools/internal/lsp/protocol"
1212
. "golang.org/x/tools/internal/lsp/regtest"
13+
"golang.org/x/tools/internal/testenv"
1314
)
1415

1516
func TestPrepareRenamePackage(t *testing.T) {
@@ -51,6 +52,41 @@ func main() {
5152
})
5253
}
5354

55+
func TestRenamePackageInRenamedPackage(t *testing.T) {
56+
// Failed at Go 1.13; not investigated
57+
testenv.NeedsGo1Point(t, 14)
58+
const files = `
59+
-- go.mod --
60+
module mod.com
61+
62+
go 1.18
63+
-- main.go --
64+
package main
65+
66+
import (
67+
"fmt"
68+
"a.go"
69+
)
70+
71+
func main() {
72+
fmt.Println(a.C)
73+
}
74+
-- a.go --
75+
package main
76+
77+
const C = 1
78+
`
79+
Run(t, files, func(t *testing.T, env *Env) {
80+
env.OpenFile("main.go")
81+
pos := env.RegexpSearch("main.go", "main")
82+
env.Rename("main.go", pos, "pkg")
83+
84+
// Check if the new package name exists.
85+
env.RegexpSearch("main.go", "package pkg")
86+
env.RegexpSearch("a.go", "package pkg")
87+
})
88+
}
89+
5490
// Test for golang/go#47564.
5591
func TestRenameInTestVariant(t *testing.T) {
5692
const files = `

internal/lsp/source/rename.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,41 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
118118
ctx, done := event.Start(ctx, "source.Rename")
119119
defer done()
120120

121+
pgf, err := s.ParseGo(ctx, f, ParseFull)
122+
if err != nil {
123+
return nil, err
124+
}
125+
inPackageName, err := isInPackageName(ctx, s, f, pgf, pp)
126+
if err != nil {
127+
return nil, err
128+
}
129+
130+
if inPackageName {
131+
renamingPkg, err := s.PackageForFile(ctx, f.URI(), TypecheckAll, NarrowestPackage)
132+
if err != nil {
133+
return nil, err
134+
}
135+
136+
result := make(map[span.URI][]protocol.TextEdit)
137+
// Rename internal references to the package in the renaming package
138+
// Todo(dle): need more investigation on case when pkg.GoFiles != pkg.CompiledGoFiles if using cgo.
139+
for _, f := range renamingPkg.CompiledGoFiles() {
140+
pkgNameMappedRange := NewMappedRange(f.Tok, f.Mapper, f.File.Name.Pos(), f.File.Name.End())
141+
rng, err := pkgNameMappedRange.Range()
142+
if err != nil {
143+
return nil, err
144+
}
145+
result[f.URI] = []protocol.TextEdit{
146+
{
147+
Range: rng,
148+
NewText: newName,
149+
},
150+
}
151+
}
152+
153+
return result, nil
154+
}
155+
121156
qos, err := qualifiedObjsAtProtocolPos(ctx, s, f.URI(), pp)
122157
if err != nil {
123158
return nil, err
@@ -182,6 +217,7 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
182217
if err != nil {
183218
return nil, err
184219
}
220+
185221
result := make(map[span.URI][]protocol.TextEdit)
186222
for uri, edits := range changes {
187223
// These edits should really be associated with FileHandles for maximal correctness.

0 commit comments

Comments
 (0)