lsp/completion: offer candidates converting arrays to slices

For example:

    var b [4]byte
    var s []byte = <>

At <> we now prefer "b" and insert as "b[:]".

Fixes golang/go#40277.

Change-Id: I5fe9d153813dac7218edf31c7c33610130eef9bc
Reviewed-on: https://go-review.googlesource.com/c/tools/+/311169
Run-TryBot: Muir Manders <muir@mnd.rs>
Run-TryBot: Rebecca Stambler <rstambler@golang.org>
gopls-CI: kokoro <noreply+kokoro@google.com>
TryBot-Result: Go Bot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
Trust: Rebecca Stambler <rstambler@golang.org>
Trust: Robert Findley <rfindley@google.com>
This commit is contained in:
Muir Manders 2021-04-18 08:32:49 -07:00 committed by Rebecca Stambler
parent b3e5b99972
commit 4934781c1f
4 changed files with 21 additions and 1 deletions

View File

@ -386,6 +386,9 @@ type candidate struct {
// For example, dereference=2 turns "foo" into "**foo" when formatting.
dereference int
// takeSlice is true if obj is an array that should be converted to a slice.
takeSlice bool
// variadic is true if this candidate fills a variadic param and
// needs "..." appended.
variadic bool
@ -2464,6 +2467,13 @@ func (c *candidate) anyCandType(f func(t types.Type, addressable bool) bool) boo
return true
}
if array, ok := objType.Underlying().(*types.Array); ok {
if f(types.NewSlice(array.Elem()), false) {
c.takeSlice = true
return true
}
}
return false
}

View File

@ -150,6 +150,11 @@ func (c *completer) item(ctx context.Context, cand candidate) (CompletionItem, e
prefix = typeName + "(" + prefix
suffix = ")"
}
if cand.takeSlice {
suffix += "[:]"
}
// Add variadic "..." only if snippets if enabled or cand is not a function
if cand.variadic && (c.opts.snippets || !cand.expandFuncCall) {
suffix += "..."

View File

@ -41,3 +41,8 @@ func _() {
var ds [][]myInt
ds = [][]m //@complete(" //", atMyInt)
}
func _() {
var b [0]byte //@item(atByte, "b", "[0]byte", "var")
var _ []byte = b //@snippet(" //", atByte, "b[:]", "b[:]")
}

View File

@ -2,7 +2,7 @@
CallHierarchyCount = 2
CodeLensCount = 5
CompletionsCount = 262
CompletionSnippetCount = 94
CompletionSnippetCount = 95
UnimportedCompletionsCount = 5
DeepCompletionsCount = 5
FuzzyCompletionsCount = 8