Skip to content

Commit 0c506a2

Browse files
danishprakashstamblerre
authored andcommitted
internal/lsp/source: evaluate bin/hex literal on hover
We currently support evaluating int literals on hover if it's a const declaration but not if it's a var. This change adds support for the same for var. Fixes golang/go#45802 Change-Id: I3c4f6024b4b58fed38a5111253aa9e2ac30249fb Reviewed-on: https://go-review.googlesource.com/c/tools/+/330309 Reviewed-by: Rebecca Stambler <[email protected]> Trust: Rebecca Stambler <[email protected]> Trust: Peter Weinberger <[email protected]> Run-TryBot: Rebecca Stambler <[email protected]> gopls-CI: kokoro <[email protected]> TryBot-Result: Go Bot <[email protected]>
1 parent 46d1522 commit 0c506a2

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

gopls/internal/regtest/misc/hover_test.go

+32
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"testing"
1010

1111
. "golang.org/x/tools/internal/lsp/regtest"
12+
"golang.org/x/tools/internal/testenv"
1213
)
1314

1415
func TestHoverUnexported(t *testing.T) {
@@ -56,3 +57,34 @@ func main() {
5657
}
5758
})
5859
}
60+
61+
func TestHoverIntLiteral(t *testing.T) {
62+
testenv.NeedsGo1Point(t, 13)
63+
const source = `
64+
-- main.go --
65+
package main
66+
67+
var (
68+
bigBin = 0b1001001
69+
)
70+
71+
var hex = 0xe34e
72+
73+
func main() {
74+
}
75+
`
76+
Run(t, source, func(t *testing.T, env *Env) {
77+
env.OpenFile("main.go")
78+
hexExpected := "58190"
79+
got, _ := env.Hover("main.go", env.RegexpSearch("main.go", "hex"))
80+
if got != nil && !strings.Contains(got.Value, hexExpected) {
81+
t.Errorf("Hover: missing expected field '%s'. Got:\n%q", hexExpected, got.Value)
82+
}
83+
84+
binExpected := "73"
85+
got, _ = env.Hover("main.go", env.RegexpSearch("main.go", "bigBin"))
86+
if got != nil && !strings.Contains(got.Value, binExpected) {
87+
t.Errorf("Hover: missing expected field '%s'. Got:\n%q", binExpected, got.Value)
88+
}
89+
})
90+
}

internal/lsp/source/hover.go

+19
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ func HoverIdentifier(ctx context.Context, i *IdentifierInfo) (*HoverInformation,
117117
}
118118
h.Signature = prefix + h.Signature
119119
}
120+
121+
// Check if the variable is an integer whose value we can present in a more
122+
// user-friendly way, i.e. `var hex = 0xe34e` becomes `var hex = 58190`
123+
if spec, ok := x.(*ast.ValueSpec); ok && len(spec.Values) > 0 {
124+
if lit, ok := spec.Values[0].(*ast.BasicLit); ok && len(spec.Names) > 0 {
125+
val := constant.MakeFromLiteral(types.ExprString(lit), lit.Kind, 0)
126+
h.Signature = fmt.Sprintf("var %s = %s", spec.Names[0], val)
127+
}
128+
}
129+
120130
case types.Object:
121131
// If the variable is implicitly declared in a type switch, we need to
122132
// manually generate its object string.
@@ -454,6 +464,15 @@ func formatVar(node ast.Spec, obj types.Object, decl *ast.GenDecl) *HoverInforma
454464
if comment == nil {
455465
comment = spec.Comment
456466
}
467+
468+
// We need the AST nodes for variable declarations of basic literals with
469+
// associated values so that we can augment their hover with more information.
470+
if _, ok := obj.(*types.Var); ok && spec.Type == nil && len(spec.Values) > 0 {
471+
if _, ok := spec.Values[0].(*ast.BasicLit); ok {
472+
return &HoverInformation{source: spec, comment: comment}
473+
}
474+
}
475+
457476
return &HoverInformation{source: obj, comment: comment}
458477
}
459478

0 commit comments

Comments
 (0)