Skip to content

Commit e461004

Browse files
committed
internal/lsp: add constant values to hovers
Change-Id: Ic0c497f4b1644f915db850be3fbacda8e2f85e9a Reviewed-on: https://go-review.googlesource.com/c/tools/+/197818 Run-TryBot: Rebecca Stambler <[email protected]> TryBot-Result: Gobot Gobot <[email protected]> Reviewed-by: Ian Cottrell <[email protected]>
1 parent 030b2cf commit e461004

File tree

4 files changed

+39
-3
lines changed

4 files changed

+39
-3
lines changed

internal/lsp/source/hover.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package source
66

77
import (
88
"context"
9+
"fmt"
910
"go/ast"
1011
"go/doc"
1112
"go/format"
@@ -51,12 +52,12 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) {
5152
}
5253
h.Signature = b.String()
5354
case types.Object:
54-
h.Signature = types.ObjectString(x, i.qf)
55+
h.Signature = objectString(x, i.qf)
5556
}
5657

5758
// Set the documentation.
5859
if i.Declaration.obj != nil {
59-
h.SingleLine = types.ObjectString(i.Declaration.obj, i.qf)
60+
h.SingleLine = objectString(i.Declaration.obj, i.qf)
6061
}
6162
if h.comment != nil {
6263
h.FullDocumentation = h.comment.Text()
@@ -65,6 +66,17 @@ func (i *IdentifierInfo) Hover(ctx context.Context) (*HoverInformation, error) {
6566
return h, nil
6667
}
6768

69+
// objectString is a wrapper around the types.ObjectString function.
70+
// It handles adding more information to the object string.
71+
func objectString(obj types.Object, qf types.Qualifier) string {
72+
str := types.ObjectString(obj, qf)
73+
switch obj := obj.(type) {
74+
case *types.Const:
75+
str = fmt.Sprintf("%s = %s", str, obj.Val())
76+
}
77+
return str
78+
}
79+
6880
func (d Declaration) hover(ctx context.Context) (*HoverInformation, error) {
6981
ctx, done := trace.StartSpan(ctx, "source.hover")
7082
defer done()

internal/lsp/testdata/godef/b/b.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,5 @@ func Bar() {
3333

3434
var _ *myFoo.StructFoo //@godef("myFoo", myFoo)
3535
}
36+
37+
const X = 0 //@mark(X, "X"),godef("X", X)

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,28 @@ godef/a/a.go:9:6-11: defined here as func a.Stuff()
244244

245245
-- Stuff-hover --
246246
func a.Stuff()
247+
-- X-definition --
248+
godef/b/b.go:37:7-8: defined here as const X untyped int = 0
249+
-- X-definition-json --
250+
{
251+
"span": {
252+
"uri": "file://godef/b/b.go",
253+
"start": {
254+
"line": 37,
255+
"column": 7,
256+
"offset": 812
257+
},
258+
"end": {
259+
"line": 37,
260+
"column": 8,
261+
"offset": 813
262+
}
263+
},
264+
"description": "const X untyped int = 0"
265+
}
266+
267+
-- X-hover --
268+
const X untyped int = 0
247269
-- myFoo-definition --
248270
godef/b/b.go:4:2-7: defined here as package myFoo ("golang.org/x/tools/internal/lsp/foo")
249271
-- myFoo-definition-json --

internal/lsp/testdata/summary.txt.golden

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ FoldingRangesCount = 2
1111
FormatCount = 6
1212
ImportCount = 2
1313
SuggestedFixCount = 1
14-
DefinitionsCount = 37
14+
DefinitionsCount = 38
1515
TypeDefinitionsCount = 2
1616
HighlightsCount = 2
1717
ReferencesCount = 6

0 commit comments

Comments
 (0)