Skip to content

Commit 9b156ee

Browse files
fzippfindleyr
authored andcommitted
internal/lsp/source: provide full documentation of builtin types
This change adds support for full documentation of builtin types in the hover info, consisting of the type declaration, a link to pkg.go.dev, and the text of the doc comments in the `builtin` pseudo-package. Full documentation for builtin functions was already supported. Removes the special case for the `error` interface, which is not needed and didn't provide the full documentation anyway, only the type declaration. The code has to determine the parent ast.GenDecl (which holds the doc comments) for the ast.TypeSpec of a builtin type. Fixes golang/go#50196 Change-Id: I51a054cd7e864300ba2e51152311744b7bcf3e0c Reviewed-on: https://go-review.googlesource.com/c/tools/+/383614 Trust: Robert Findley <[email protected]> Run-TryBot: Robert Findley <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Gopher Robot <[email protected]> Reviewed-by: Robert Findley <[email protected]> Trust: Hyang-Ah Hana Kim <[email protected]>
1 parent caecc2b commit 9b156ee

File tree

3 files changed

+25
-6
lines changed

3 files changed

+25
-6
lines changed

internal/lsp/source/hover.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,8 @@ func HoverIdentifier(ctx context.Context, i *IdentifierInfo) (*HoverInformation,
309309
h.LinkPath = strings.Replace(h.LinkPath, mod, mod+"@"+version, 1)
310310
}
311311
return h, nil
312-
case *types.Builtin:
312+
}
313+
if obj.Parent() == types.Universe {
313314
h.importPath = "builtin"
314315
h.LinkPath = h.importPath
315316
h.LinkAnchor = obj.Name()
@@ -520,10 +521,8 @@ func HoverInfo(ctx context.Context, s Snapshot, pkg Package, obj types.Object, p
520521
}
521522
case *ast.TypeSpec:
522523
if obj.Parent() == types.Universe {
523-
if obj.Name() == "error" {
524-
info = &HoverInformation{source: node}
525-
} else {
526-
info = &HoverInformation{source: node.Name} // comments not needed for builtins
524+
if genDecl, ok := fullDecl.(*ast.GenDecl); ok {
525+
info = formatTypeSpec(node, genDecl)
527526
}
528527
}
529528
case *ast.FuncDecl:

internal/lsp/source/identifier.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,10 @@ func findIdentifier(ctx context.Context, snapshot Snapshot, pkg Package, pgf *Pa
217217
return nil, errors.Errorf("no declaration for %s", result.Name)
218218
}
219219
result.Declaration.node = decl
220+
if typeSpec, ok := decl.(*ast.TypeSpec); ok {
221+
// Find the GenDecl (which has the doc comments) for the TypeSpec.
222+
result.Declaration.fullDecl = findGenDecl(builtin.File, typeSpec)
223+
}
220224

221225
// The builtin package isn't in the dependency graph, so the usual
222226
// utilities won't work here.
@@ -314,6 +318,18 @@ func findIdentifier(ctx context.Context, snapshot Snapshot, pkg Package, pgf *Pa
314318
return result, nil
315319
}
316320

321+
// findGenDecl determines the parent ast.GenDecl for a given ast.Spec.
322+
func findGenDecl(f *ast.File, spec ast.Spec) *ast.GenDecl {
323+
for _, decl := range f.Decls {
324+
if genDecl, ok := decl.(*ast.GenDecl); ok {
325+
if genDecl.Pos() <= spec.Pos() && genDecl.End() >= spec.End() {
326+
return genDecl
327+
}
328+
}
329+
}
330+
return nil
331+
}
332+
317333
// fullNode tries to extract the full spec corresponding to obj's declaration.
318334
// If the package was not parsed in full, the declaration file will be
319335
// re-parsed to ensure it has complete syntax.

internal/lsp/testdata/godef/a/a.go.golden

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,12 @@ func(t Type, size ...IntegerType) Type
164164
The make built\-in function allocates and initializes an object of type slice, map, or chan \(only\)\.
165165
-- string-hoverdef --
166166
```go
167-
string
167+
type string string
168168
```
169+
170+
[`string` on pkg.go.dev](https://pkg.go.dev/builtin?utm_source=gopls#string)
171+
172+
string is the set of all strings of 8\-bit bytes, conventionally but not necessarily representing UTF\-8\-encoded text\.
169173
-- typesImport-hoverdef --
170174
```go
171175
package types ("go/types")

0 commit comments

Comments
 (0)