internal/lsp/source: include builtin name in hovered signature

Make builtin hover consistent with signatureHelp by reusing the
BuiltinSignature function. To simplify this, just precompute the builtin
signature. In later CLs we can pre-compute all signatures and eliminate
the signatureSource indirection.

Fixes golang/go#51811

Change-Id: I1babe64ba41f636bf455074a23ad6dac5539fb89
Reviewed-on: https://go-review.googlesource.com/c/tools/+/395294
Trust: Robert Findley <rfindley@google.com>
Run-TryBot: Robert Findley <rfindley@google.com>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Hyang-Ah Hana Kim <hyangah@gmail.com>
This commit is contained in:
Robert Findley 2022-03-23 16:57:32 -04:00
parent cd31eaad03
commit 2536df952b
2 changed files with 18 additions and 3 deletions

View File

@ -32,7 +32,8 @@ import (
type HoverContext struct {
// signatureSource is the object or node use to derive the hover signature.
//
// TODO(rfindley): can we pre-compute the signature, to avoid this indirection?
// It may also hold a precomputed string.
// TODO(rfindley): pre-compute all signatures to avoid this indirection.
signatureSource interface{}
// comment is the most relevant comment group associated with the hovered object.
@ -262,6 +263,9 @@ func HoverIdentifier(ctx context.Context, i *IdentifierInfo) (*HoverJSON, error)
fset := i.Snapshot.FileSet()
// Determine the symbol's signature.
switch x := hoverCtx.signatureSource.(type) {
case string:
h.Signature = x // a pre-computed signature
case *ast.TypeSpec:
x2 := *x
// Don't duplicate comments when formatting type specs.
@ -578,7 +582,18 @@ func FindHoverContext(ctx context.Context, s Snapshot, pkg Package, obj types.Ob
case *types.Func:
info = &HoverContext{signatureSource: obj, Comment: node.Doc}
case *types.Builtin:
info = &HoverContext{signatureSource: node.Type, Comment: node.Doc}
info = &HoverContext{Comment: node.Doc}
if sig, err := NewBuiltinSignature(ctx, s, obj.Name()); err == nil {
info.signatureSource = "func " + sig.name + sig.Format()
} else {
// Fall back on the object as a signature source.
// TODO(rfindley): refactor so that we can report bugs from the source
// package.
// debug.Bug(ctx, "invalid builtin hover", "did not find builtin signature: %v", err)
info.signatureSource = obj
}
case *types.Var:
// Object is a function param or the field of an anonymous struct
// declared with ':='. Skip the first one because only fields

View File

@ -156,7 +156,7 @@ const h untyped int = 2
Constant block\.
-- make-hoverdef --
```go
func(t Type, size ...IntegerType) Type
func make(t Type, size ...int) Type
```
The make built\-in function allocates and initializes an object of type slice, map, or chan \(only\)\.