From 2536df952bc9bbd56b06d974f2310fd57f5e7978 Mon Sep 17 00:00:00 2001 From: Robert Findley Date: Wed, 23 Mar 2022 16:57:32 -0400 Subject: [PATCH] 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 Run-TryBot: Robert Findley gopls-CI: kokoro TryBot-Result: Gopher Robot Reviewed-by: Hyang-Ah Hana Kim --- internal/lsp/source/hover.go | 19 +++++++++++++++++-- internal/lsp/testdata/godef/a/a.go.golden | 2 +- 2 files changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index e3d66e24b3..b6fd9acf90 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -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 diff --git a/internal/lsp/testdata/godef/a/a.go.golden b/internal/lsp/testdata/godef/a/a.go.golden index 5f720b26aa..9f67a147d1 100644 --- a/internal/lsp/testdata/godef/a/a.go.golden +++ b/internal/lsp/testdata/godef/a/a.go.golden @@ -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\)\.