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>
diff --git a/internal/lsp/source/hover.go b/internal/lsp/source/hover.go
index 17fc7d1..bfdb3b0 100644
--- a/internal/lsp/source/hover.go
+++ b/internal/lsp/source/hover.go
@@ -6,6 +6,7 @@
 
 import (
 	"context"
+	"fmt"
 	"go/ast"
 	"go/doc"
 	"go/format"
@@ -51,12 +52,12 @@
 		}
 		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 @@
 	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 0d75611..721f9e7 100644
--- a/internal/lsp/testdata/godef/b/b.go
+++ b/internal/lsp/testdata/godef/b/b.go
@@ -33,3 +33,5 @@
 
 	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 07b1333..f668f5f 100644
--- a/internal/lsp/testdata/godef/b/b.go.golden
+++ b/internal/lsp/testdata/godef/b/b.go.golden
@@ -244,6 +244,28 @@
 
 -- 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 2e92b1c..5c1e1b2 100644
--- a/internal/lsp/testdata/summary.txt.golden
+++ b/internal/lsp/testdata/summary.txt.golden
@@ -11,7 +11,7 @@
 FormatCount = 6
 ImportCount = 2
 SuggestedFixCount = 1
-DefinitionsCount = 37
+DefinitionsCount = 38
 TypeDefinitionsCount = 2
 HighlightsCount = 2
 ReferencesCount = 6