Skip to content

Commit 4320446

Browse files
committed
internal/lsp: permit renaming symbols declared in other packages
Also, add a test for it. Fixes golang/go#36743 Change-Id: I750ea36189a4ec5c9dc8553e4739b0238c2b29c8 Reviewed-on: https://go-review.googlesource.com/c/tools/+/216306 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Heschi Kreinick <[email protected]>
1 parent bcecb1f commit 4320446

File tree

5 files changed

+24
-9
lines changed

5 files changed

+24
-9
lines changed

internal/lsp/source/rename.go

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
8888

8989
obj := qos[0].obj
9090
pkg := qos[0].pkg
91-
sourcePkg := qos[0].sourcePkg
9291

9392
if obj.Name() == newName {
9493
return nil, errors.Errorf("old and new names are the same: %s", newName)
@@ -103,10 +102,6 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
103102
if pkg == nil || pkg.IsIllTyped() {
104103
return nil, errors.Errorf("package for %s is ill typed", f.Identity().URI)
105104
}
106-
// Do not rename identifiers declared in another package.
107-
if pkg != sourcePkg {
108-
return nil, errors.Errorf("rename failed because %q is declared in a different package (%s)", obj.Name(), pkg.PkgPath())
109-
}
110105

111106
refs, err := References(ctx, s, f, pp, true)
112107
if err != nil {

internal/lsp/testdata/rename/crosspkg/crosspkg.go.golden

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import "golang.org/x/tools/internal/lsp/rename/crosspkg"
1515

1616
func Other() {
1717
crosspkg.Bar
18-
crosspkg.Dolphin()
18+
crosspkg.Dolphin() //@rename("Foo", "Flamingo")
1919
}
2020

2121
-- Tomato-rename --
@@ -35,6 +35,6 @@ import "golang.org/x/tools/internal/lsp/rename/crosspkg"
3535

3636
func Other() {
3737
crosspkg.Tomato
38-
crosspkg.Foo()
38+
crosspkg.Foo() //@rename("Foo", "Flamingo")
3939
}
4040

internal/lsp/testdata/rename/crosspkg/other/other.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ import "golang.org/x/tools/internal/lsp/rename/crosspkg"
44

55
func Other() {
66
crosspkg.Bar
7-
crosspkg.Foo()
7+
crosspkg.Foo() //@rename("Foo", "Flamingo")
88
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
-- Flamingo-rename --
2+
crosspkg.go:
3+
package crosspkg
4+
5+
func Flamingo() { //@rename("Foo", "Dolphin")
6+
7+
}
8+
9+
var Bar int //@rename("Bar", "Tomato")
10+
11+
other.go:
12+
package other
13+
14+
import "golang.org/x/tools/internal/lsp/rename/crosspkg"
15+
16+
func Other() {
17+
crosspkg.Bar
18+
crosspkg.Flamingo() //@rename("Foo", "Flamingo")
19+
}
20+

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ DefinitionsCount = 43
1515
TypeDefinitionsCount = 2
1616
HighlightsCount = 52
1717
ReferencesCount = 8
18-
RenamesCount = 22
18+
RenamesCount = 23
1919
PrepareRenamesCount = 8
2020
SymbolsCount = 1
2121
SignaturesCount = 22

0 commit comments

Comments
 (0)