Skip to content

Commit 9065c18

Browse files
committed
internal/lsp: fix bug renaming local vars with test files present
This fix adds all packages to the renamer packages map. Renaming performs checks on each package to make sure there are no conflicts. If there are multiple packages, each package needs to be checked. The packages were being incorrectly added to the map and were all being put under a single key. Fixes golang/go#33664 Change-Id: I68466ce34ac49b700ce6d14ce0b53e2795926fa9 Reviewed-on: https://go-review.googlesource.com/c/tools/+/190399 Reviewed-by: Rebecca Stambler <[email protected]>
1 parent ea41424 commit 9065c18

File tree

5 files changed

+18
-7
lines changed

5 files changed

+18
-7
lines changed

internal/lsp/source/rename.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
type renamer struct {
2424
ctx context.Context
2525
fset *token.FileSet
26-
pkg Package // the package containing the declaration of the ident
2726
refs []*ReferenceInfo
2827
objsToUpdate map[types.Object]bool
2928
hadConflicts bool
@@ -66,20 +65,19 @@ func (i *IdentifierInfo) Rename(ctx context.Context, newName string) (map[span.U
6665
r := renamer{
6766
ctx: ctx,
6867
fset: i.File.FileSet(),
69-
pkg: i.pkg,
7068
refs: refs,
7169
objsToUpdate: make(map[types.Object]bool),
7270
from: i.Name,
7371
to: newName,
7472
packages: make(map[*types.Package]Package),
7573
}
7674
for _, from := range refs {
77-
r.packages[i.pkg.GetTypes()] = from.pkg
75+
r.packages[from.pkg.GetTypes()] = from.pkg
7876
}
7977

8078
// Check that the renaming of the identifier is ok.
81-
for _, from := range refs {
82-
r.check(from.obj)
79+
for _, ref := range refs {
80+
r.check(ref.obj)
8381
}
8482
if r.hadConflicts {
8583
return nil, errors.Errorf(r.errors)
@@ -139,7 +137,7 @@ func (r *renamer) update() (map[span.URI][]TextEdit, error) {
139137
continue
140138
}
141139

142-
doc := r.docComment(r.pkg, ref.ident)
140+
doc := r.docComment(ref.pkg, ref.ident)
143141
if doc == nil {
144142
continue
145143
}

internal/lsp/testdata/rename/testy/testy.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,5 @@ package testy
33
type tt int //@rename("tt", "testyType")
44

55
func a() {
6+
foo := 42 //@rename("foo", "bar")
67
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
1+
-- bar-rename --
2+
testy.go:
3+
package testy
4+
5+
type tt int //@rename("tt", "testyType")
6+
7+
func a() {
8+
bar := 42 //@rename("foo", "bar")
9+
}
10+
111
-- testyType-rename --
212
testy.go:
313
package testy
414

515
type testyType int //@rename("tt", "testyType")
616

717
func a() {
18+
foo := 42 //@rename("foo", "bar")
819
}
920

internal/lsp/testdata/rename/testy/testy_test.go.golden

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package testy
55
type tt int //@rename("tt", "testyType")
66

77
func b() {
8+
foo := 42 //@rename("foo", "bar")
89
}
910

1011
testy_test.go:

internal/lsp/tests/tests.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ const (
3535
ExpectedTypeDefinitionsCount = 2
3636
ExpectedHighlightsCount = 2
3737
ExpectedReferencesCount = 5
38-
ExpectedRenamesCount = 16
38+
ExpectedRenamesCount = 17
3939
ExpectedSymbolsCount = 1
4040
ExpectedSignaturesCount = 21
4141
ExpectedLinksCount = 4

0 commit comments

Comments
 (0)