Skip to content

Commit

Permalink
gopls/internal/golang: provide version info for stdlib fields
Browse files Browse the repository at this point in the history
For golang/go#67159

Change-Id: I8b8b12949566857f29460675b9dc4d9c6804ff1e
Reviewed-on: https://go-review.googlesource.com/c/tools/+/595336
Auto-Submit: Hongxiang Jiang <hxjiang@golang.org>
Reviewed-by: Robert Findley <rfindley@google.com>
LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
  • Loading branch information
h9jiang authored and gopherbot committed Jun 27, 2024
1 parent bc15dd8 commit 2e239ad
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 11 deletions.
8 changes: 7 additions & 1 deletion gopls/doc/release/v0.17.0.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ displayed by v0.16.0 and its diagnostics were confusing.
## Extract declarations to new file
Gopls now offers another code action, "Extract declarations to new file",
which moves selected code sections to a newly created file within the
same package. The created filename is chosen as the first {function, type,
same package. The created filename is chosen as the first {function, type,
const, var} name encountered. In addition, import declarations are added or
removed as needed.

Expand All @@ -22,3 +22,9 @@ or by selecting a whole declaration or multiple declrations.

In order to avoid ambiguity and surprise about what to extract, some kinds
of paritial selection of a declration cannot invoke this code action.

## Standard library version information in Hover

Hovering over a standard library symbol now displays information about the first
Go release containing the symbol. For example, hovering over `errors.As` shows
"Added in go1.13".
42 changes: 33 additions & 9 deletions gopls/internal/golang/hover.go
Original file line number Diff line number Diff line change
Expand Up @@ -1209,18 +1209,19 @@ func formatHover(h *hoverJSON, options *settings.Options, pkgURL func(path Packa
// StdSymbolOf returns the std lib symbol information of the given obj.
// It returns nil if the input obj is not an exported standard library symbol.
func StdSymbolOf(obj types.Object) *stdlib.Symbol {
if !obj.Exported() {
if !obj.Exported() || obj.Pkg() == nil {
return nil
}

// Symbols that not defined in standard library should return early.
// TODO(hxjiang): The returned slices is binary searchable.
symbols := stdlib.PackageSymbols[obj.Pkg().Path()]
if symbols == nil {
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 symbols {
if s.Kind == stdlib.Method || s.Kind == stdlib.Field {
continue
Expand All @@ -1236,7 +1237,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
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()] {
for _, s := range symbols {
if s.Kind != stdlib.Method {
continue
}
Expand All @@ -1249,7 +1250,30 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
}
}

// TODO(hxjiang): handle exported fields of package level types.
// Handle Field.
if v, _ := obj.(*types.Var); v != nil && v.IsField() {
for _, s := range symbols {
if s.Kind != stdlib.Field {
continue
}

typeName, fieldName := s.SplitField()
if fieldName != v.Name() {
continue
}

typeObj := obj.Pkg().Scope().Lookup(typeName)
if typeObj == nil {
continue
}

if fieldObj, _, _ := types.LookupFieldOrMethod(typeObj.Type(), true, obj.Pkg(), fieldName); obj == fieldObj {
return &s
}
}
return nil
}

return nil
}

Expand Down
6 changes: 5 additions & 1 deletion gopls/internal/test/integration/misc/hover_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -670,6 +670,7 @@ import "fmt"
import "context"
import "crypto"
import "regexp"
import "go/doc/comment"
type testRegexp = *regexp.Regexp
Expand All @@ -686,6 +687,9 @@ func _() {
copy := re.Copy()
var testRE testRegexp
testRE.Longest()
var pr comment.Printer
pr.HeadingID = func(*comment.Heading) string { return "" }
}
`

Expand All @@ -702,7 +706,7 @@ func _() {
{"SHA512_224", true, "go1.5"}, // package-level const
{"Copy", true, "go1.6"}, // method
{"Longest", true, "go1.1"}, // method with alias receiver
// TODO(hxjiang): add test for symbol type Field.
{"HeadingID", true, "go1.19"}, // field
}

Run(t, src, func(t *testing.T, env *Env) {
Expand Down

0 comments on commit 2e239ad

Please sign in to comment.