gopls/internal/golang: provide version info for stdlib methods

For golang/go#67159

Change-Id: If3d09a39e8bf0a5ab61dc0953d7250ad75525372
Reviewed-on: https://go-review.googlesource.com/c/tools/+/595135
Reviewed-by: Robert Findley <rfindley@google.com>
Reviewed-by: Alan Donovan <adonovan@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
This commit is contained in:
Hongxiang Jiang 2024-06-26 14:04:26 +00:00
Родитель dd9d554b34
Коммит b9a361aa9c
2 изменённых файлов: 36 добавлений и 3 удалений

Просмотреть файл

@ -1213,9 +1213,15 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
return nil
}
// Handle Function, Type, Const & Var.
if isPackageLevel(obj) {
// Symbols defined not in std lib package should return early.
symbols := stdlib.PackageSymbols[obj.Pkg().Path()]
if symbols == nil {
return nil
}
// TODO(hxjiang): This is binary searchable.
for _, s := range stdlib.PackageSymbols[obj.Pkg().Path()] {
for _, s := range symbols {
if s.Kind == stdlib.Method || s.Kind == stdlib.Field {
continue
}
@ -1223,9 +1229,27 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
return &s
}
}
return nil
}
// TODO(hxjiang): handle exported fields and methods of package level types.
// Handle Method.
if fn, _ := obj.(*types.Func); fn != nil {
isPtr, named := typesinternal.ReceiverNamed(fn.Type().(*types.Signature).Recv())
if isPackageLevel(named.Obj()) {
for _, s := range stdlib.PackageSymbols[obj.Pkg().Path()] {
if s.Kind != stdlib.Method {
continue
}
ptr, recv, name := s.SplitMethod()
if ptr == isPtr && recv == named.Obj().Name() && name == fn.Name() {
return &s
}
}
return nil
}
}
// TODO(hxjiang): handle exported fields of package level types.
return nil
}

Просмотреть файл

@ -669,6 +669,9 @@ package stdlib
import "fmt"
import "context"
import "crypto"
import "regexp"
type testRegexp = *regexp.Regexp
func _() {
var ctx context.Context
@ -678,6 +681,11 @@ func _() {
fmt.Printf("%v", crypto.SHA512_224)
}
_ := fmt.Appendf(make([]byte, 100), "world, %d", 23)
var re = regexp.MustCompile("\n{2,}")
copy := re.Copy()
var testRE testRegexp
testRE.Longest()
}
`
@ -692,7 +700,8 @@ func _() {
{"Canceled", true, "go1.7"}, // package-level var
{"Context", true, "go1.7"}, // package-level type
{"SHA512_224", true, "go1.5"}, // package-level const
// TODO(hxjiang): add test for symbol type Method.
{"Copy", true, "go1.6"}, // method
{"Longest", true, "go1.1"}, // method with alias receiver
// TODO(hxjiang): add test for symbol type Field.
}