diff --git a/internal/lsp/source/completion/completion.go b/internal/lsp/source/completion/completion.go index aa1600fb8d..07ff03108c 100644 --- a/internal/lsp/source/completion/completion.go +++ b/internal/lsp/source/completion/completion.go @@ -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 } diff --git a/internal/lsp/source/completion/format.go b/internal/lsp/source/completion/format.go index 6d8299c7ab..f34a84e554 100644 --- a/internal/lsp/source/completion/format.go +++ b/internal/lsp/source/completion/format.go @@ -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 += "..." diff --git a/internal/lsp/testdata/arraytype/array_type.go.in b/internal/lsp/testdata/arraytype/array_type.go.in index a53ee74a66..7e9a96f7b0 100644 --- a/internal/lsp/testdata/arraytype/array_type.go.in +++ b/internal/lsp/testdata/arraytype/array_type.go.in @@ -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[:]") +} diff --git a/internal/lsp/testdata/summary.txt.golden b/internal/lsp/testdata/summary.txt.golden index f2fbc95b92..8c4c72a4c7 100644 --- a/internal/lsp/testdata/summary.txt.golden +++ b/internal/lsp/testdata/summary.txt.golden @@ -2,7 +2,7 @@ CallHierarchyCount = 2 CodeLensCount = 5 CompletionsCount = 262 -CompletionSnippetCount = 94 +CompletionSnippetCount = 95 UnimportedCompletionsCount = 5 DeepCompletionsCount = 5 FuzzyCompletionsCount = 8