1
0
mirror of https://github.com/golang/go synced 2024-11-18 09:04:49 -07:00

internal/lsp: add changeMethods logic to rename check

This logic is directly copied from the refactor/rename package. See
https://github.com/golang/tools/blob/master/refactor/rename/rename.go#L321.

Fixes golang/go#39269

Change-Id: Ibe335aab37c495d2a960cb9da254b24b6fbac8e8
Reviewed-on: https://go-review.googlesource.com/c/tools/+/242158
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
This commit is contained in:
Rebecca Stambler 2020-07-12 00:26:29 -04:00
parent 01425d7016
commit f8240f79c3
7 changed files with 68 additions and 27 deletions

View File

@ -615,6 +615,7 @@ func (s *snapshot) isWorkspacePackage(id packageID) (packagePath, bool) {
scope, ok := s.workspacePackages[id]
return scope, ok
}
func (s *snapshot) FindFile(uri span.URI) source.FileHandle {
f, err := s.view.getFile(uri)
if err != nil {

View File

@ -104,6 +104,19 @@ func Rename(ctx context.Context, s Snapshot, f FileHandle, pp protocol.Position,
to: newName,
packages: make(map[*types.Package]Package),
}
// A renaming initiated at an interface method indicates the
// intention to rename abstract and concrete methods as needed
// to preserve assignability.
for _, ref := range refs {
if obj, ok := ref.obj.(*types.Func); ok {
recv := obj.Type().(*types.Signature).Recv()
if recv != nil && isInterface(recv.Type().Underlying()) {
r.changeMethods = true
break
}
}
}
for _, from := range refs {
r.packages[from.pkg.GetTypes()] = from.pkg
}

View File

@ -502,7 +502,6 @@ func (r *renamer) checkSelections(from types.Object) {
r.selectionConflict(from, delta, syntax, obj)
return
}
} else if sel.Obj().Name() == r.to {
if obj, indices, _ := types.LookupFieldOrMethod(sel.Recv(), isAddressable, from.Pkg(), from.Name()); obj == from {
// Renaming 'from' may cause this existing
@ -845,7 +844,7 @@ func someUse(info *types.Info, obj types.Object) *ast.Ident {
// The zero value is returned if not found.
//
func pathEnclosingInterval(fset *token.FileSet, pkg Package, start, end token.Pos) (resPkg Package, path []ast.Node, exact bool) {
var pkgs = []Package{pkg}
pkgs := []Package{pkg}
for _, f := range pkg.GetSyntax() {
for _, imp := range f.Imports {
if imp == nil {

View File

@ -1,27 +1,3 @@
-- error-rename --
package b
var c int //@rename("int", "uint")
func _() {
error := 1 //@rename("a", "error")
error = 2
_ = error
}
var (
// Hello there.
// Foo does the thing.
Foo int //@rename("Foo", "Bob")
)
/*
Hello description
*/
func Hello() {} //@rename("Hello", "Goodbye")
-- uint-rename --
builtin object "int"
-- Bob-rename --
package b
@ -66,3 +42,27 @@ Goodbye description
*/
func Goodbye() {} //@rename("Hello", "Goodbye")
-- error-rename --
package b
var c int //@rename("int", "uint")
func _() {
error := 1 //@rename("a", "error")
error = 2
_ = error
}
var (
// Hello there.
// Foo does the thing.
Foo int //@rename("Foo", "Bob")
)
/*
Hello description
*/
func Hello() {} //@rename("Hello", "Goodbye")
-- uint-rename --
builtin object "int"

View File

@ -0,0 +1,13 @@
package another
type (
I interface{ F() }
C struct{ I }
)
func (C) g()
func _() {
var x I = C{}
x.F() //@rename("F", "G")
}

View File

@ -0,0 +1,15 @@
-- G-rename --
package another
type (
I interface{ G() }
C struct{ I }
)
func (C) g()
func _() {
var x I = C{}
x.G() //@rename("F", "G")
}

View File

@ -16,7 +16,7 @@ DefinitionsCount = 53
TypeDefinitionsCount = 2
HighlightsCount = 69
ReferencesCount = 11
RenamesCount = 27
RenamesCount = 28
PrepareRenamesCount = 7
SymbolsCount = 3
WorkspaceSymbolsCount = 2