File: literal_snippets.go.in

package info (click to toggle)
golang-golang-x-tools 1%3A0.1.0%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 12,588 kB
  • sloc: javascript: 2,011; asm: 1,458; sh: 174; yacc: 155; makefile: 21; ansic: 17
file content (216 lines) | stat: -rw-r--r-- 7,034 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
package snippets

import (
	"bytes"
	"go/ast"
	"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(rw http.ResponseWriter, r *http.Request) {$0\\}", "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(rw http.ResponseWriter, r *http.Request) {$0\\}", "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(mi1 myImpl, i int, mi2 myImpl) {$0\\}", "func(${1:mi1} myImpl, ${2:i} int, ${3:mi2} myImpl) {$0\\}")

	type aliasImpl = myImpl
	var aliasParams func(aliasImpl) aliasImpl
	aliasParams = f //@snippet(" //", litFunc, "func(ai aliasImpl) aliasImpl {$0\\}", "func(${1:ai} aliasImpl) aliasImpl {$0\\}")

	const two = 2
	var builtinTypes func([]int, [two]bool, map[string]string, struct{ i int }, interface{ foo() }, <-chan int)
	builtinTypes = f //@snippet(" //", litFunc, "func(i1 []int, b [two]bool, m map[string]string, s struct{ i int \\}, i2 interface{ foo() \\}, c <-chan int) {$0\\}", "func(${1:i1} []int, ${2:b} [two]bool, ${3:m} map[string]string, ${4:s} struct{ i int \\}, ${5:i2} interface{ foo() \\}, ${6:c} <-chan int) {$0\\}")

	var _ func(ast.Node) = f //@snippet(" //", litFunc, "func(n ast.Node) {$0\\}", "func(${1:n} ast.Node) {$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)")
}

func _() {
	type ptrStruct struct {
		p *ptrStruct
	}

	ptrStruct{} //@item(litPtrStruct, "ptrStruct{}", "", "var")

	ptrStruct{
		p: &ptrSt, //@rank(",", litPtrStruct)
	}

	&ptrStruct{} //@item(litPtrStructPtr, "&ptrStruct{}", "", "var")

	&ptrStruct{
		p: ptrSt, //@rank(",", litPtrStructPtr)
	}
}

func _() {
	f := func(...[]int) {}
	f() //@snippet(")", litIntSlice, "[]int{$0\\}", "[]int{$0\\}")
}