1package snippets
2
3import (
4	"bytes"
5	"net/http"
6	"sort"
7
8	"golang.org/x/tools/internal/lsp/foo"
9)
10
11func _() {
12	[]int{}        //@item(litIntSlice, "[]int{}", "", "var")
13	&[]int{}       //@item(litIntSliceAddr, "&[]int{}", "", "var")
14	make([]int, 0) //@item(makeIntSlice, "make([]int, 0)", "", "func")
15
16	var _ *[]int = in //@snippet(" //", litIntSliceAddr, "&[]int{$0\\}", "&[]int{$0\\}")
17	var _ **[]int = in //@complete(" //")
18
19	var slice []int
20	slice = i //@snippet(" //", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
21	slice = m //@snippet(" //", makeIntSlice, "make([]int, ${1:})", "make([]int, ${1:0})")
22}
23
24func _() {
25	type namedInt []int
26
27	namedInt{}        //@item(litNamedSlice, "namedInt{}", "", "var")
28	make(namedInt, 0) //@item(makeNamedSlice, "make(namedInt, 0)", "", "func")
29
30	var namedSlice namedInt
31	namedSlice = n //@snippet(" //", litNamedSlice, "namedInt{$0\\}", "namedInt{$0\\}")
32	namedSlice = m //@snippet(" //", makeNamedSlice, "make(namedInt, ${1:})", "make(namedInt, ${1:0})")
33}
34
35func _() {
36	make(chan int) //@item(makeChan, "make(chan int)", "", "func")
37
38	var ch chan int
39	ch = m //@snippet(" //", makeChan, "make(chan int)", "make(chan int)")
40}
41
42func _() {
43	map[string]struct{}{}     //@item(litMap, "map[string]struct{}{}", "", "var")
44	make(map[string]struct{}) //@item(makeMap, "make(map[string]struct{})", "", "func")
45
46	var m map[string]struct{}
47	m = m //@snippet(" //", litMap, "map[string]struct{\\}{$0\\}", "map[string]struct{\\}{$0\\}")
48	m = m //@snippet(" //", makeMap, "make(map[string]struct{\\})", "make(map[string]struct{\\})")
49
50	struct{}{} //@item(litEmptyStruct, "struct{}{}", "", "var")
51
52	m["hi"] = s //@snippet(" //", litEmptyStruct, "struct{\\}{\\}", "struct{\\}{\\}")
53}
54
55func _() {
56	type myStruct struct{ i int }
57
58	myStruct{}  //@item(litStruct, "myStruct{}", "", "var")
59	&myStruct{} //@item(litStructPtr, "&myStruct{}", "", "var")
60
61	var ms myStruct
62	ms = m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
63
64	var msPtr *myStruct
65	msPtr = m //@snippet(" //", litStructPtr, "&myStruct{$0\\}", "&myStruct{$0\\}")
66
67	msPtr = &m //@snippet(" //", litStruct, "myStruct{$0\\}", "myStruct{$0\\}")
68}
69
70type myImpl struct{}
71
72func (myImpl) foo() {}
73
74func (*myImpl) bar() {}
75
76type myBasicImpl string
77
78func (myBasicImpl) foo() {}
79
80func _() {
81	type myIntf interface {
82		foo()
83	}
84
85	myImpl{} //@item(litImpl, "myImpl{}", "", "var")
86
87	var mi myIntf
88	mi = m //@snippet(" //", litImpl, "myImpl{\\}", "myImpl{\\}")
89
90	myBasicImpl() //@item(litBasicImpl, "myBasicImpl()", "string", "var")
91
92	mi = m //@snippet(" //", litBasicImpl, "myBasicImpl($0)", "myBasicImpl($0)")
93
94	// only satisfied by pointer to myImpl
95	type myPtrIntf interface {
96		bar()
97	}
98
99	&myImpl{} //@item(litImplPtr, "&myImpl{}", "", "var")
100
101	var mpi myPtrIntf
102	mpi = m //@snippet(" //", litImplPtr, "&myImpl{\\}", "&myImpl{\\}")
103}
104
105func _() {
106	var s struct{ i []int } //@item(litSliceField, "i", "[]int", "field")
107	var foo []int
108	// no literal completions after selector
109	foo = s.i //@complete(" //", litSliceField)
110}
111
112func _() {
113	type myStruct struct{ i int } //@item(litMyStructType, "myStruct", "struct{...}", "struct")
114	myStruct{} //@item(litMyStruct, "myStruct{}", "", "var")
115
116	foo := func(s string, args ...myStruct) {}
117	// Don't give literal slice candidate for variadic arg.
118	// Do give literal candidates for variadic element.
119	foo("", myStruct) //@complete(")", litMyStruct, litMyStructType)
120}
121
122func _() {
123	Buffer{} //@item(litBuffer, "Buffer{}", "", "var")
124
125	var b *bytes.Buffer
126	b = bytes.Bu //@snippet(" //", litBuffer, "Buffer{\\}", "Buffer{\\}")
127}
128
129func _() {
130	_ = "func(...) {}" //@item(litFunc, "func(...) {}", "", "var")
131
132	sort.Slice(nil, fun) //@complete(")", litFunc),snippet(")", litFunc, "func(i, j int) bool {$0\\}", "func(i, j int) bool {$0\\}")
133
134	http.HandleFunc("", f) //@snippet(")", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
135
136	// no literal "func" completions
137	http.Handle("", fun) //@complete(")")
138
139	http.HandlerFunc() //@item(handlerFunc, "http.HandlerFunc()", "", "var")
140	http.Handle("", h) //@snippet(")", handlerFunc, "http.HandlerFunc($0)", "http.HandlerFunc($0)")
141	http.Handle("", http.HandlerFunc()) //@snippet("))", litFunc, "", "func(${1:rw} http.ResponseWriter, ${2:r} *http.Request) {$0\\}")
142
143	var namedReturn func(s string) (b bool)
144	namedReturn = f //@snippet(" //", litFunc, "func(s string) (b bool) {$0\\}", "func(s string) (b bool) {$0\\}")
145
146	var multiReturn func() (bool, int)
147	multiReturn = f //@snippet(" //", litFunc, "func() (bool, int) {$0\\}", "func() (bool, int) {$0\\}")
148
149	var multiNamedReturn func() (b bool, i int)
150	multiNamedReturn = f //@snippet(" //", litFunc, "func() (b bool, i int) {$0\\}", "func() (b bool, i int) {$0\\}")
151
152	var duplicateParams func(myImpl, int, myImpl)
153	duplicateParams = f //@snippet(" //", litFunc, "", "func(${1:mi} myImpl, ${2:_} int, ${3:_} myImpl) {$0\\}")
154}
155
156func _() {
157	StructFoo{} //@item(litStructFoo, "StructFoo{}", "struct{...}", "struct")
158
159	var sfp *foo.StructFoo
160	// Don't insert the "&" before "StructFoo{}".
161	sfp = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
162
163	var sf foo.StructFoo
164	sf = foo.Str //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
165	sf = foo. //@snippet(" //", litStructFoo, "StructFoo{$0\\}", "StructFoo{$0\\}")
166}
167
168func _() {
169	float64() //@item(litFloat64, "float64()", "float64", "var")
170
171	// don't complete to "&float64()"
172	var _ *float64 = float64 //@complete(" //")
173
174	var f float64
175	f = fl //@complete(" //", litFloat64),snippet(" //", litFloat64, "float64($0)", "float64($0)")
176
177	type myInt int
178	myInt() //@item(litMyInt, "myInt()", "", "var")
179
180	var mi myInt
181	mi = my //@snippet(" //", litMyInt, "myInt($0)", "myInt($0)")
182}
183
184func _() {
185	type ptrStruct struct {
186		p *ptrStruct
187	}
188
189	ptrStruct{} //@item(litPtrStruct, "ptrStruct{}", "", "var")
190
191	ptrStruct{
192		p: &ptrSt, //@rank(",", litPtrStruct)
193	}
194}
195