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 <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
This commit is contained in:
Muir Manders 2020-03-05 19:33:48 -08:00 committed by Rebecca Stambler
parent 613a0345a2
commit 11a475a590
3 changed files with 14 additions and 3 deletions

View File

@ -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)
}

View File

@ -192,3 +192,8 @@ func _() {
p: &ptrSt, //@rank(",", litPtrStruct)
}
}
func _() {
f := func(...[]int) {}
f() //@snippet(")", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
}

View File

@ -1,7 +1,7 @@
-- summary --
CodeLensCount = 2
CompletionsCount = 237
CompletionSnippetCount = 74
CompletionSnippetCount = 75
UnimportedCompletionsCount = 11
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8