mirror of
https://github.com/golang/go
synced 2024-11-18 16:04:44 -07:00
internal/lsp: compare names when finding references
Objects for builtin types all have position token.NoPos. We do not want all objects that have position token.NoPos to be matched when we are looking for references for this object, so we need to compare the names of the objects as well. Fixes golang/go#32991 Change-Id: I67e7aba9909ebcbb246203ea5c572debf996c792 Reviewed-on: https://go-review.googlesource.com/c/tools/+/185247 Run-TryBot: Suzy Mueller <suzmue@golang.org> TryBot-Result: Gobot Gobot <gobot@golang.org> Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
parent
d255de4fdb
commit
e491332ed8
@ -56,8 +56,7 @@ func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, erro
|
||||
return nil, fmt.Errorf("package %s has no types info", pkg.PkgPath())
|
||||
}
|
||||
for ident, obj := range info.Defs {
|
||||
// TODO(suzmue): support the case where an identifier may have two different declarations.
|
||||
if obj == nil || obj.Pos() != i.decl.obj.Pos() {
|
||||
if obj == nil || !sameObj(obj, i.decl.obj) {
|
||||
continue
|
||||
}
|
||||
// Add the declarations at the beginning of the references list.
|
||||
@ -70,7 +69,7 @@ func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, erro
|
||||
}}, references...)
|
||||
}
|
||||
for ident, obj := range info.Uses {
|
||||
if obj == nil || obj.Pos() != i.decl.obj.Pos() {
|
||||
if obj == nil || !sameObj(obj, i.decl.obj) {
|
||||
continue
|
||||
}
|
||||
references = append(references, &ReferenceInfo{
|
||||
@ -85,3 +84,11 @@ func (i *IdentifierInfo) References(ctx context.Context) ([]*ReferenceInfo, erro
|
||||
|
||||
return references, nil
|
||||
}
|
||||
|
||||
// sameObj returns true if obj is the same as declObj.
|
||||
// Objects are the same if they have the some Pos and Name.
|
||||
func sameObj(obj, declObj types.Object) bool {
|
||||
// TODO(suzmue): support the case where an identifier may have two different
|
||||
// declaration positions.
|
||||
return obj.Pos() == declObj.Pos() && obj.Name() == declObj.Name()
|
||||
}
|
||||
|
11
internal/lsp/testdata/references/refs.go
vendored
Normal file
11
internal/lsp/testdata/references/refs.go
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
package refs
|
||||
|
||||
type i int //@mark(typeInt, "int"),refs("int", typeInt, argInt, returnInt)
|
||||
|
||||
func _(_ int) []bool { //@mark(argInt, "int")
|
||||
return nil
|
||||
}
|
||||
|
||||
func _(_ string) int { //@mark(returnInt, "int")
|
||||
return 0
|
||||
}
|
@ -33,7 +33,7 @@ const (
|
||||
ExpectedDefinitionsCount = 38
|
||||
ExpectedTypeDefinitionsCount = 2
|
||||
ExpectedHighlightsCount = 2
|
||||
ExpectedReferencesCount = 4
|
||||
ExpectedReferencesCount = 5
|
||||
ExpectedRenamesCount = 13
|
||||
ExpectedSymbolsCount = 1
|
||||
ExpectedSignaturesCount = 21
|
||||
|
Loading…
Reference in New Issue
Block a user