internal/lsp: add constant values to hovers

Change-Id: Ic0c497f4b1644f915db850be3fbacda8e2f85e9a
Reviewed-on: https://go-review.googlesource.com/c/tools/+/197818
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Ian Cottrell <iancottrell@google.com>
This commit is contained in:
Rebecca Stambler 2019-09-27 21:10:58 -04:00
parent 030b2cf115
commit e461004dd0
4 changed files with 39 additions and 3 deletions

View File

@ -6,6 +6,7 @@ package source
import ( import (
"context" "context"
"fmt"
"go/ast" "go/ast"
"go/doc" "go/doc"
"go/format" "go/format"
@ -51,12 +52,12 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) {
} }
h.Signature = b.String() h.Signature = b.String()
case types.Object: case types.Object:
h.Signature = types.ObjectString(x, i.qf) h.Signature = objectString(x, i.qf)
} }
// Set the documentation. // Set the documentation.
if i.Declaration.obj != nil { if i.Declaration.obj != nil {
h.SingleLine = types.ObjectString(i.Declaration.obj, i.qf) h.SingleLine = objectString(i.Declaration.obj, i.qf)
} }
if h.comment != nil { if h.comment != nil {
h.FullDocumentation = h.comment.Text() h.FullDocumentation = h.comment.Text()
@ -65,6 +66,17 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) {
return h, nil return h, nil
} }
// objectString is a wrapper around the types.ObjectString function.
// It handles adding more information to the object string.
func objectString(obj types.Object, qf types.Qualifier) string {
str := types.ObjectString(obj, qf)
switch obj := obj.(type) {
case *types.Const:
str = fmt.Sprintf("%s = %s", str, obj.Val())
}
return str
}
func (d Declaration) hover(ctx context.Context) (*HoverInformation, error) { func (d Declaration) hover(ctx context.Context) (*HoverInformation, error) {
ctx, done := trace.StartSpan(ctx, "source.hover") ctx, done := trace.StartSpan(ctx, "source.hover")
defer done() defer done()

View File

@ -33,3 +33,5 @@ func Bar() {
var _ *myFoo.StructFoo //@godef("myFoo", myFoo) var _ *myFoo.StructFoo //@godef("myFoo", myFoo)
} }
const X = 0 //@mark(X, "X"),godef("X", X)

View File

@ -244,6 +244,28 @@ godef/a/a.go:9:6-11: defined here as func a.Stuff()
-- Stuff-hover -- -- Stuff-hover --
func a.Stuff() func a.Stuff()
-- X-definition --
godef/b/b.go:37:7-8: defined here as const X untyped int = 0
-- X-definition-json --
{
"span": {
"uri": "file://godef/b/b.go",
"start": {
"line": 37,
"column": 7,
"offset": 812
},
"end": {
"line": 37,
"column": 8,
"offset": 813
}
},
"description": "const X untyped int = 0"
}
-- X-hover --
const X untyped int = 0
-- myFoo-definition -- -- myFoo-definition --
godef/b/b.go:4:2-7: defined here as package myFoo ("golang.org/x/tools/internal/lsp/foo") godef/b/b.go:4:2-7: defined here as package myFoo ("golang.org/x/tools/internal/lsp/foo")
-- myFoo-definition-json -- -- myFoo-definition-json --

View File

@ -11,7 +11,7 @@ FoldingRangesCount = 2
FormatCount = 6 FormatCount = 6
ImportCount = 2 ImportCount = 2
SuggestedFixCount = 1 SuggestedFixCount = 1
DefinitionsCount = 37 DefinitionsCount = 38
TypeDefinitionsCount = 2 TypeDefinitionsCount = 2
HighlightsCount = 2 HighlightsCount = 2
ReferencesCount = 6 ReferencesCount = 6