From 11a475a590acad44f466dde7d3e988cf52bad01c Mon Sep 17 00:00:00 2001 From: Muir Manders Date: Thu, 5 Mar 2020 19:33:48 -0800 Subject: [PATCH] internal/lsp/source: fix literal completions in variadic args We now properly offer literal completions when completing the variadic parameter. For example: func foo(...[]int) {} foo(<>) // now offers "[]int{}" Updates golang/go#37656. Change-Id: I91c47d455ae3f8051078c82a308f2b5ad4b3d4cd Reviewed-on: https://go-review.googlesource.com/c/tools/+/222199 Run-TryBot: Muir Manders TryBot-Result: Gobot Gobot Reviewed-by: Rebecca Stambler --- internal/lsp/source/completion.go | 10 ++++++++-- .../lsp/primarymod/snippets/literal_snippets.go.in | 5 +++++ internal/lsp/testdata/lsp/summary.txt.golden | 2 +- 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/internal/lsp/source/completion.go b/internal/lsp/source/completion.go index e103e26018..41830f08fa 100644 --- a/internal/lsp/source/completion.go +++ b/internal/lsp/source/completion.go @@ -1052,12 +1052,18 @@ func (c *completer) lexical() error { } } - if c.inference.objType != nil { + if t := c.inference.objType; t != nil { + // Use variadic element type if we are completing variadic position. + if c.inference.variadicType != nil { + t = c.inference.variadicType + } + + t = deref(t) + // If we have an expected type and it is _not_ a named type, see // if an object literal makes a good candidate. For example, if // our expected type is "[]int", this will add a candidate of // "[]int{}". - t := deref(c.inference.objType) if _, named := t.(*types.Named); !named { c.literal(t, nil) } diff --git a/internal/lsp/testdata/lsp/primarymod/snippets/literal_snippets.go.in b/internal/lsp/testdata/lsp/primarymod/snippets/literal_snippets.go.in index ffaa125073..69b8cdf173 100644 --- a/internal/lsp/testdata/lsp/primarymod/snippets/literal_snippets.go.in +++ b/internal/lsp/testdata/lsp/primarymod/snippets/literal_snippets.go.in @@ -192,3 +192,8 @@ func _() { p: &ptrSt, //@rank(",", litPtrStruct) } } + +func _() { + f := func(...[]int) {} + f() //@snippet(")", litIntSlice, "[]int{$0\\}", "[]int{$0\\}") +} diff --git a/internal/lsp/testdata/lsp/summary.txt.golden b/internal/lsp/testdata/lsp/summary.txt.golden index 9dc1e2a52d..b7bdc3bafc 100644 --- a/internal/lsp/testdata/lsp/summary.txt.golden +++ b/internal/lsp/testdata/lsp/summary.txt.golden @@ -1,7 +1,7 @@ -- summary -- CodeLensCount = 2 CompletionsCount = 237 -CompletionSnippetCount = 74 +CompletionSnippetCount = 75 UnimportedCompletionsCount = 11 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8