From e461004dd03ddc1b89596568ce4119c0e8152ce6 Mon Sep 17 00:00:00 2001 From: Rebecca Stambler Date: Fri, 27 Sep 2019 21:10:58 -0400 Subject: [PATCH] internal/lsp: add constant values to hovers Change-Id: Ic0c497f4b1644f915db850be3fbacda8e2f85e9a Reviewed-on: https://go-review.googlesource.com/c/tools/+/197818 Run-TryBot: Rebecca Stambler TryBot-Result: Gobot Gobot Reviewed-by: Ian Cottrell --- internal/lsp/source/hover.go | 16 ++++++++++++++-- internal/lsp/testdata/godef/b/b.go | 2 ++ internal/lsp/testdata/godef/b/b.go.golden | 22 ++++++++++++++++++++++ internal/lsp/testdata/summary.txt.golden | 2 +- 4 files changed, 39 insertions(+), 3 deletions(-) diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go index 17fc7d192a..bfdb3b0288 100644 --- a/internal/lsp/source/hover.go +++ b/internal/lsp/source/hover.go @@ -6,6 +6,7 @@ package source import ( "context" + "fmt" "go/ast" "go/doc" "go/format" @@ -51,12 +52,12 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) { } h.Signature = b.String() case types.Object: - h.Signature = types.ObjectString(x, i.qf) + h.Signature = objectString(x, i.qf) } // Set the documentation. 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 { h.FullDocumentation = h.comment.Text() @@ -65,6 +66,17 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) { 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) { ctx, done := trace.StartSpan(ctx, "source.hover") defer done() diff --git a/internal/lsp/testdata/godef/b/b.go b/internal/lsp/testdata/godef/b/b.go index 0d756112a3..721f9e7c92 100644 --- a/internal/lsp/testdata/godef/b/b.go +++ b/internal/lsp/testdata/godef/b/b.go @@ -33,3 +33,5 @@ func Bar() { var _ *myFoo.StructFoo //@godef("myFoo", myFoo) } + +const X = 0 //@mark(X, "X"),godef("X", X) diff --git a/internal/lsp/testdata/godef/b/b.go.golden b/internal/lsp/testdata/godef/b/b.go.golden index 07b1333e15..f668f5f13a 100644 --- a/internal/lsp/testdata/godef/b/b.go.golden +++ b/internal/lsp/testdata/godef/b/b.go.golden @@ -244,6 +244,28 @@ godef/a/a.go:9:6-11: defined here as func a.Stuff() -- Stuff-hover -- 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 -- godef/b/b.go:4:2-7: defined here as package myFoo ("golang.org/x/tools/internal/lsp/foo") -- myFoo-definition-json -- diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index 2e92b1c182..5c1e1b2890 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -11,7 +11,7 @@ FoldingRangesCount = 2 FormatCount = 6 ImportCount = 2 SuggestedFixCount = 1 -DefinitionsCount = 37 +DefinitionsCount = 38 TypeDefinitionsCount = 2 HighlightsCount = 2 ReferencesCount = 6