Skip to content

Commit 8865782

Browse files
committed
internal/lsp: add text edits for unkeyed literals
Add text edits that a user can accept to make the unkeyed composite literals keyed from the inlay hints. The text edits modify all of the unkeyed fields in a composite literal, since a mixture of keyed and unkeyed fields are not allowed. Change-Id: I0683fbaa5e22bc004b91c98fc09e495e797826ee Reviewed-on: https://go-review.googlesource.com/c/tools/+/414855 TryBot-Result: Gopher Robot <[email protected]> gopls-CI: kokoro <[email protected]> Reviewed-by: Jamal Carvalho <[email protected]> Run-TryBot: Suzy Mueller <[email protected]>
1 parent 1a196f0 commit 8865782

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

internal/lsp/protocol/tsprotocol.go

Lines changed: 8 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/lsp/source/inlay_hint.go

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ func compositeLiteralFields(node ast.Node, tmap *lsppos.TokenMapper, info *types
344344
}
345345

346346
var hints []protocol.InlayHint
347-
347+
var allEdits []protocol.TextEdit
348348
for i, v := range compLit.Elts {
349349
if _, ok := v.(*ast.KeyValueExpr); !ok {
350350
start, ok := tmap.Position(v.Pos())
@@ -360,8 +360,17 @@ func compositeLiteralFields(node ast.Node, tmap *lsppos.TokenMapper, info *types
360360
Kind: protocol.Parameter,
361361
PaddingRight: true,
362362
})
363+
allEdits = append(allEdits, protocol.TextEdit{
364+
Range: protocol.Range{Start: start, End: start},
365+
NewText: strct.Field(i).Name() + ": ",
366+
})
363367
}
364368
}
369+
// It is not allowed to have a mix of keyed and unkeyed fields, so
370+
// have the text edits add keys to all fields.
371+
for i := range hints {
372+
hints[i].TextEdits = allEdits
373+
}
365374
return hints
366375
}
367376

0 commit comments

Comments
 (0)