Skip to content

Commit 2e239ad

Browse files
h9jianggopherbot
authored andcommitted
gopls/internal/golang: provide version info for stdlib fields
For golang/go#67159 Change-Id: I8b8b12949566857f29460675b9dc4d9c6804ff1e Reviewed-on: https://go-review.googlesource.com/c/tools/+/595336 Auto-Submit: Hongxiang Jiang <[email protected]> Reviewed-by: Robert Findley <[email protected]> LUCI-TryBot-Result: Go LUCI <[email protected]>
1 parent bc15dd8 commit 2e239ad

File tree

3 files changed

+45
-11
lines changed

3 files changed

+45
-11
lines changed

gopls/doc/release/v0.17.0.md

+7-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ displayed by v0.16.0 and its diagnostics were confusing.
1212
## Extract declarations to new file
1313
Gopls now offers another code action, "Extract declarations to new file",
1414
which moves selected code sections to a newly created file within the
15-
same package. The created filename is chosen as the first {function, type,
15+
same package. The created filename is chosen as the first {function, type,
1616
const, var} name encountered. In addition, import declarations are added or
1717
removed as needed.
1818

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

2323
In order to avoid ambiguity and surprise about what to extract, some kinds
2424
of paritial selection of a declration cannot invoke this code action.
25+
26+
## Standard library version information in Hover
27+
28+
Hovering over a standard library symbol now displays information about the first
29+
Go release containing the symbol. For example, hovering over `errors.As` shows
30+
"Added in go1.13".

gopls/internal/golang/hover.go

+33-9
Original file line numberDiff line numberDiff line change
@@ -1209,18 +1209,19 @@ func formatHover(h *hoverJSON, options *settings.Options, pkgURL func(path Packa
12091209
// StdSymbolOf returns the std lib symbol information of the given obj.
12101210
// It returns nil if the input obj is not an exported standard library symbol.
12111211
func StdSymbolOf(obj types.Object) *stdlib.Symbol {
1212-
if !obj.Exported() {
1212+
if !obj.Exported() || obj.Pkg() == nil {
1213+
return nil
1214+
}
1215+
1216+
// Symbols that not defined in standard library should return early.
1217+
// TODO(hxjiang): The returned slices is binary searchable.
1218+
symbols := stdlib.PackageSymbols[obj.Pkg().Path()]
1219+
if symbols == nil {
12131220
return nil
12141221
}
12151222

12161223
// Handle Function, Type, Const & Var.
12171224
if isPackageLevel(obj) {
1218-
// Symbols defined not in std lib package should return early.
1219-
symbols := stdlib.PackageSymbols[obj.Pkg().Path()]
1220-
if symbols == nil {
1221-
return nil
1222-
}
1223-
// TODO(hxjiang): This is binary searchable.
12241225
for _, s := range symbols {
12251226
if s.Kind == stdlib.Method || s.Kind == stdlib.Field {
12261227
continue
@@ -1236,7 +1237,7 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
12361237
if fn, _ := obj.(*types.Func); fn != nil {
12371238
isPtr, named := typesinternal.ReceiverNamed(fn.Type().(*types.Signature).Recv())
12381239
if isPackageLevel(named.Obj()) {
1239-
for _, s := range stdlib.PackageSymbols[obj.Pkg().Path()] {
1240+
for _, s := range symbols {
12401241
if s.Kind != stdlib.Method {
12411242
continue
12421243
}
@@ -1249,7 +1250,30 @@ func StdSymbolOf(obj types.Object) *stdlib.Symbol {
12491250
}
12501251
}
12511252

1252-
// TODO(hxjiang): handle exported fields of package level types.
1253+
// Handle Field.
1254+
if v, _ := obj.(*types.Var); v != nil && v.IsField() {
1255+
for _, s := range symbols {
1256+
if s.Kind != stdlib.Field {
1257+
continue
1258+
}
1259+
1260+
typeName, fieldName := s.SplitField()
1261+
if fieldName != v.Name() {
1262+
continue
1263+
}
1264+
1265+
typeObj := obj.Pkg().Scope().Lookup(typeName)
1266+
if typeObj == nil {
1267+
continue
1268+
}
1269+
1270+
if fieldObj, _, _ := types.LookupFieldOrMethod(typeObj.Type(), true, obj.Pkg(), fieldName); obj == fieldObj {
1271+
return &s
1272+
}
1273+
}
1274+
return nil
1275+
}
1276+
12531277
return nil
12541278
}
12551279

gopls/internal/test/integration/misc/hover_test.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,7 @@ import "fmt"
670670
import "context"
671671
import "crypto"
672672
import "regexp"
673+
import "go/doc/comment"
673674
674675
type testRegexp = *regexp.Regexp
675676
@@ -686,6 +687,9 @@ func _() {
686687
copy := re.Copy()
687688
var testRE testRegexp
688689
testRE.Longest()
690+
691+
var pr comment.Printer
692+
pr.HeadingID = func(*comment.Heading) string { return "" }
689693
}
690694
`
691695

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

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

0 commit comments

Comments
 (0)