Skip to content

Commit effd83e

Browse files
h9jianggopherbot
authored andcommitted
gopls/internal/golang: add type inlayhint for variable decl
Skip type inlayhint if the type is explicitly written like: var foo string to avoid duplicated information. Rename gomod inlayhint.go to inlay_hint.go for consistency. For golang/go#73946 Change-Id: I94944d2fde685ee0b57ce9fdb38cee31bb1038b2 Reviewed-on: https://go-review.googlesource.com/c/tools/+/677935 Auto-Submit: Hongxiang Jiang <hxjiang@golang.org> Reviewed-by: Alan Donovan <adonovan@google.com> LUCI-TryBot-Result: Go LUCI <golang-scoped@luci-project-accounts.iam.gserviceaccount.com>
1 parent 58e5e62 commit effd83e

File tree

4 files changed

+33
-9
lines changed

4 files changed

+33
-9
lines changed

gopls/internal/golang/inlay_hint.go

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -165,13 +165,33 @@ func funcTypeParams(info *types.Info, pgf *parsego.File, qual types.Qualifier, c
165165
}
166166

167167
func assignVariableTypes(info *types.Info, pgf *parsego.File, qual types.Qualifier, cur inspector.Cursor, add func(protocol.InlayHint)) {
168-
for curAssign := range cur.Preorder((*ast.AssignStmt)(nil)) {
169-
stmt := curAssign.Node().(*ast.AssignStmt)
170-
if stmt.Tok != token.DEFINE {
171-
continue
172-
}
173-
for _, v := range stmt.Lhs {
174-
variableType(info, pgf, qual, v, add)
168+
for node := range cur.Preorder((*ast.AssignStmt)(nil), (*ast.ValueSpec)(nil)) {
169+
switch n := node.Node().(type) {
170+
case *ast.AssignStmt:
171+
if n.Tok != token.DEFINE {
172+
continue
173+
}
174+
for _, v := range n.Lhs {
175+
variableType(info, pgf, qual, v, add)
176+
}
177+
case *ast.GenDecl:
178+
if n.Tok != token.VAR {
179+
continue
180+
}
181+
for _, v := range n.Specs {
182+
spec := v.(*ast.ValueSpec)
183+
// The type of the variable is written, skip showing type of this var.
184+
// ```go
185+
// var foo string
186+
// ```
187+
if spec.Type != nil {
188+
continue
189+
}
190+
191+
for _, v := range spec.Names {
192+
variableType(info, pgf, qual, v, add)
193+
}
194+
}
175195
}
176196
}
177197
}

gopls/internal/test/marker/testdata/inlayhints/inlayhints.txt

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -363,6 +363,8 @@ func SumNumbers[K comparable, V Number](m map[K]V) V {
363363
package inlayHint //@inlayhints(vartypes)
364364

365365
func assignTypes() {
366+
var x string
367+
var y = ""
366368
i, j := 0, len([]string{})-1
367369
println(i, j)
368370
}
@@ -385,6 +387,8 @@ func compositeLitType() {
385387
package inlayHint //@inlayhints(vartypes)
386388

387389
func assignTypes() {
390+
var x string
391+
var y = ""
388392
i< int>, j< int> := 0, len([]string{})-1
389393
println(i, j)
390394
}

gopls/internal/test/marker/testdata/inlayhints/issue67142.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,11 @@ go 1.21.9
2525
//@inlayhints(out)
2626
package p
2727

28-
var _ = rand.Float64()
28+
var _ = rand.Float64()
2929

3030
-- @out --
3131
//@inlayhints(out)
3232
package p
3333

34-
var _ = rand.Float64()
34+
var _ = rand.Float64()
3535

0 commit comments

Comments
 (0)