1
0
mirror of https://github.com/golang/go synced 2024-11-19 05:34:40 -07:00
go/internal/lsp/testdata/snippets/literal_snippets.go.in
Muir Manders 89082a3841 internal/lsp/source: fix some invalid literal candidates
We were marking all literal candidates as addressable so we were
getting invalid candidates like "&int()". Fix it to only mark literal
struct, array, slice and map types as addressable.

I also fixed the unnamed literal candidate to pass the dereferenced
expected type. For example, if the expected type was "*[]int" we were
passing a literal type of "*[]int" which wasn't working anymore. Now
we pass "[]int" and take its address as "&[]int{}".

Change-Id: I5d0ee074d3cc91c39dd881630583e31be5a05579
Reviewed-on: https://go-review.googlesource.com/c/tools/+/212677
Run-TryBot: Muir Manders <muir@mnd.rs>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Rebecca Stambler <rstambler@golang.org>
2020-01-08 20:36:44 +00:00

183 lines
5.6 KiB
Go

package snippets
import (
"bytes"
"net/http"
"sort"
"golang.org/x/tools/internal/lsp/foo"
)
func _() {
[]int{} //@item(litIntSlice, "[]int{}", "", "var")
&[]int{} //@item(litIntSliceAddr, "&[]int{}", "", "var")
make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
var _ *[]int = in //@snippet(" //", litIntSliceAddr, "&[]int{$0\\}", "&[]int{$0\\}")
var _ **[]int = in //@complete(" //")
var slice []int
slice = i //@snippet(" //", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
slice = m //@snippet(" //", makeIntSlice, "make([]int, ${1:})", "make([]int, ${1:0})")
}
func _() {
type namedInt []int
namedInt{} //@item(litNamedSlice, "namedInt{}", "", "var")
make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
var namedSlice namedInt
namedSlice = n //@snippet(" //", litNamedSlice, "namedInt{$0\\}", "namedInt{$0\\}")
namedSlice = m //@snippet(" //", makeNamedSlice, "make(namedInt, ${1:})", "make(namedInt, ${1:0})")
}
func _() {
make(chan int) //@item(makeChan, "make(chan int)", "", "func")
var ch chan int
ch = m //@snippet(" //", makeChan, "make(chan int)", "make(chan int)")
}
func _() {
map[string]struct{}{} //@item(litMap, "map[string]struct{}{}", "", "var")
make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
var m map[string]struct{}
m = m //@snippet(" //", litMap, "map[string]struct{\\}{$0\\}", "map[string]struct{\\}{$0\\}")
m = m //@snippet(" //", makeMap, "make(map[string]struct{\\})", "make(map[string]struct{\\})")
struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
m["hi"] = s //@snippet(" //", litEmptyStruct, "struct{\\}{\\}", "struct{\\}{\\}")
}
func _() {
type myStruct struct{ i int }
myStruct{} //@item(litStruct, "myStruct{}", "", "var")
&myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
var ms myStruct
ms = m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
var msPtr *myStruct
msPtr = m //@snippet(" //", litStructPtr, "&myStruct{$0\\}", "&myStruct{$0\\}")
msPtr = &m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
}
type myImpl struct{}
func (myImpl) foo() {}
func (*myImpl) bar() {}
type myBasicImpl string
func (myBasicImpl) foo() {}
func _() {
type myIntf interface {
foo()
}
myImpl{} //@item(litImpl, "myImpl{}", "", "var")
var mi myIntf
mi = m //@snippet(" //", litImpl, "myImpl{\\}", "myImpl{\\}")
myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
mi = m //@snippet(" //", litBasicImpl, "myBasicImpl($0)", "myBasicImpl($0)")
// only satisfied by pointer to myImpl
type myPtrIntf interface {
bar()
}
&myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
var mpi myPtrIntf
mpi = m //@snippet(" //", litImplPtr, "&myImpl{\\}", "&myImpl{\\}")
}
func _() {
var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
var foo []int
// no literal completions after selector
foo = s.i //@complete(" //", litSliceField)
}
func _() {
type myStruct struct{ i int } //@item(litMyStructType, "myStruct", "struct{...}", "struct")
myStruct{} //@item(litMyStruct, "myStruct{}", "", "var")
foo := func(s string, args ...myStruct) {}
// Don't give literal slice candidate for variadic arg.
// Do give literal candidates for variadic element.
foo("", myStruct) //@complete(")", litMyStruct, litMyStructType)
}
func _() {
Buffer{} //@item(litBuffer, "Buffer{}", "", "var")
var b *bytes.Buffer
b = bytes.Bu //@snippet(" //", litBuffer, "Buffer{\\}", "Buffer{\\}")
}
func _() {
_ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
sort.Slice(nil, fun) //@complete(")", litFunc),snippet(")", litFunc, "func(i, j int) bool {$0\\}", "func(i, j int) bool {$0\\}")
http.HandleFunc("", f) //@snippet(")", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
// no literal "func" completions
http.Handle("", fun) //@complete(")")
http.HandlerFunc() //@item(handlerFunc, "http.HandlerFunc()", "", "var")
http.Handle("", h) //@snippet(")", handlerFunc, "http.HandlerFunc($0)", "http.HandlerFunc($0)")
http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
var namedReturn func(s string) (b bool)
namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}", "func(s string) (b bool) {$0\\}")
var multiReturn func() (bool, int)
multiReturn = f //@snippet(" //", litFunc, "func() (bool, int) {$0\\}", "func() (bool, int) {$0\\}")
var multiNamedReturn func() (b bool, i int)
multiNamedReturn = f //@snippet(" //", litFunc, "func() (b bool, i int) {$0\\}", "func() (b bool, i int) {$0\\}")
var duplicateParams func(myImpl, int, myImpl)
duplicateParams = f //@snippet(" //", litFunc, "", "func(${1:mi} myImpl, ${2:_} int, ${3:_} myImpl) {$0\\}")
}
func _() {
StructFoo{} //@item(litStructFoo, "StructFoo{}", "struct{...}", "struct")
var sfp *foo.StructFoo
// Don't insert the "&" before "StructFoo{}".
sfp = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
var sf foo.StructFoo
sf = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
sf = foo. //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
}
func _() {
float64() //@item(litFloat64, "float64()", "float64", "var")
// don't complete to "&float64()"
var _ *float64 = float64 //@complete(" //")
var f float64
f = fl //@complete(" //", litFloat64),snippet(" //", litFloat64, "float64($0)", "float64($0)")
type myInt int
myInt() //@item(litMyInt, "myInt()", "", "var")
var mi myInt
mi = my //@snippet(" //", litMyInt, "myInt($0)", "myInt($0)")
}