Skip to content

Commit

Permalink
internal/lsp: add inlay hints for composite literal names
Browse files Browse the repository at this point in the history
For golang/go#52343.
For golang/vscode-go#1631.

Change-Id: I8fba5ddf0bd25ba0fc20f3305ce13868f426087c
Reviewed-on: https://go-review.googlesource.com/c/tools/+/411102
Run-TryBot: Jamal Carvalho <jamal@golang.org>
TryBot-Result: Gopher Robot <gobot@golang.org>
Reviewed-by: Suzy Mueller <suzmue@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
  • Loading branch information
jamalc committed Jun 10, 2022
1 parent 83b0675 commit 5e48d26
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 0 deletions.
33 changes: 33 additions & 0 deletions internal/lsp/source/inlay_hint.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ func InlayHint(ctx context.Context, snapshot Snapshot, fh FileHandle, _ protocol
hints = append(hints, rangeVariableTypes(n, tmap, info, &q)...)
case *ast.GenDecl:
hints = append(hints, constantValues(n, tmap, info)...)
case *ast.CompositeLit:
hints = append(hints, compositeLiterals(n, tmap, info)...)
}
return true
})
Expand Down Expand Up @@ -179,6 +181,37 @@ func constantValues(node *ast.GenDecl, tmap *lsppos.TokenMapper, info *types.Inf
return hints
}

func compositeLiterals(node *ast.CompositeLit, tmap *lsppos.TokenMapper, info *types.Info) []protocol.InlayHint {
typ := info.TypeOf(node)
if typ == nil {
return nil
}
strct, ok := typ.Underlying().(*types.Struct)
if !ok {
return nil
}

var hints []protocol.InlayHint
for i, v := range node.Elts {
if _, ok := v.(*ast.KeyValueExpr); !ok {
start, ok := tmap.Position(v.Pos())
if !ok {
continue
}
if i > strct.NumFields()-1 {
break
}
hints = append(hints, protocol.InlayHint{
Position: &start,
Label: buildLabel(strct.Field(i).Name() + ":"),
Kind: protocol.Parameter,
PaddingRight: true,
})
}
}
return hints
}

func buildLabel(s string) []protocol.InlayHintLabelPart {
label := protocol.InlayHintLabelPart{
Value: s,
Expand Down
15 changes: 15 additions & 0 deletions internal/lsp/testdata/inlay_hint/composite_literals.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package inlayHint //@inlayHint("package")

import "fmt"

func fieldNames() {
for _, c := range []struct {
in, want string
}{
{"Hello, world", "dlrow ,olleH"},
{"Hello, 世界", "界世 ,olleH"},
{"", ""},
} {
fmt.Println(c.in == c.want)
}
}
17 changes: 17 additions & 0 deletions internal/lsp/testdata/inlay_hint/composite_literals.go.golden
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
-- inlayHint --
package inlayHint //@inlayHint("package")

import "fmt"

func fieldNames() {
for _<int>, c<struct{in string; want strin...> := range []struct {
in, want string
}{
{<in:>"Hello, world", <want:>"dlrow ,olleH"},
{<in:>"Hello, 世界", <want:>"界世 ,olleH"},
{<in:>"", <want:>""},
} {
fmt.Println(<a...:>c.in == c.want)
}
}

0 comments on commit 5e48d26

Please sign in to comment.