forked from xlucas/go-vim-install
-
Notifications
You must be signed in to change notification settings - Fork 0
/
go.snippets
437 lines (365 loc) · 7.26 KB
/
go.snippets
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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
# Snippets for Go
priority -10
snippet come "Comment surrounded with equals"
//===========================
// $1
//===========================
endsnippet
snippet comel "Large comment surrounded with equals"
//===========================
//
// $1
//
//===========================
endsnippet
snippet comd "Comment surrounded with dashes"
//---------------------------
// $1
//---------------------------
endsnippet
snippet comdl "Large comment surrounded with dashes"
//---------------------------
//
// $1
//
//---------------------------
endsnippet
snippet todo "TODO comment"
// TODO: $1
endsnippet
snippet bug "BUG comment"
// BUG($1): $2
endsnippet
# shorthand variable declaration
snippet : "v := value"
${1} := ${0}
endsnippet
# anonymous function
snippet anon "fn := func() { ... }"
${1:fn} := func() {
${2:${VISUAL}}
}
${0}
endsnippet
# append
snippet ap "append(slice, value)"
append(${1:slice}, ${0:value})
endsnippet
# append assignment
snippet ap= "a = append(a, value)"
${1:slice} = append($1, ${0:value})
endsnippet
# break
snippet br "break"
break
endsnippet
# channel
snippet ch "chan Type"
chan ${0:int}
endsnippet
# case
snippet case "case ...:"
case ${1:value}:
${0:${VISUAL}}
endsnippet
# constant
snippet con "const XXX Type = ..."
const ${1:NAME} ${2:Type} = ${0:0}
endsnippet
# constants
snippet cons "const ( ... )"
const (
${1:NAME} ${2:Type} = ${3:value}
${0}
)
endsnippet
# constants with iota
snippet iota "const ( ... = iota )"
const (
${1:NAME} ${2:Type} = iota
${0}
)
endsnippet
# continue
snippet cn "continue"
continue
endsnippet
# default case
snippet default "default: ..."
default:
${0:${VISUAL}}
endsnippet
# defer
snippet df "defer someFunction()"
defer ${1:func}(${2})
${0}
endsnippet
snippet def "defer func() { ... }"
defer func() {
${0:${VISUAL}}
}()
endsnippet
# defer recover
snippet defr
defer func() {
if err := recover(); err != nil {
${0:${VISUAL}}
}
}()
endsnippet
# gpl
snippet gpl
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, see <http://www.gnu.org/licenses/>.
*
* Copyright (C) ${1:Author}, `strftime("%Y")`
*/
${0}
endsnippet
# import
snippet import "import ( ... )"
import (
"${1:package}"
)
endsnippet
# full interface snippet
snippet interface "interface I { ... }"
type ${1:Interface} interface {
${2:/* TODO: add methods */}
}
endsnippet
# if condition
snippet if "if ... { ... }"
if ${1:condition} {
${0:${VISUAL}}
}
endsnippet
# else snippet
snippet else
else {
${0:${VISUAL}}
}
endsnippet
# error snippet
snippet errn "Error return " !b
if err != nil {
return err
}
${0}
endsnippet
# error snippet
snippet errt "Error test fatal " !b
if err != nil {
t.Fatal(err)
}
${0}
endsnippet
snippet errn, "Error return with two return values" !b
if err != nil {
return ${1:nil}, err
}
${0}
endsnippet
snippet errh "Error handle and return" !b
if err != nil {
${1}
return
}
${0}
endsnippet
snippet json "\`json:key\`"
\`json:"${1:keyName}"\`
endsnippet
# fallthrough
snippet ft "fallthrough"
fallthrough
endsnippet
# for loop
snippet for "for ... { ... }"
for ${1} {
${0:${VISUAL}}
}
endsnippet
# for integer loop
snippet fori "for 0..N-1 { ... }"
for ${1:i} := 0; $1 < ${2:N}; $1++ {
${0:${VISUAL}}
}
endsnippet
# for range loop
snippet forr "for k, v := range items { ... }"
for ${2:k}, ${3:v} := range ${1} {
${0:${VISUAL}}
}
endsnippet
# function
snippet func "func Function(...) [error] { ... }"
func ${1:name}(${2:params})${3/(.+)/ /}`!p opening_par(snip, 3)`$3`!p closing_par(snip, 3)` {
${0:${VISUAL}}
}
endsnippet
# Fmt Printf debug
snippet ff "fmt.Printf(...)"
fmt.Printf("${1:${VISUAL}} = %+v\n", $1)
endsnippet
# Fmt Println debug
snippet fn "fmt.Println(...)"
fmt.Println("${1:${VISUAL}}")
endsnippet
# log printf
snippet lf "log.Printf(...)"
log.Printf("${1:${VISUAL}} = %+v\n", $1)
endsnippet
# log println
snippet ln "log.Println(...)"
log.Println("${1:${VISUAL}}")
endsnippet
# make
snippet make "make(Type, size)"
make(${1:[]string}, ${2:0})${0}
endsnippet
# map
snippet map "map[Type]Type"
map[${1:string}]${0:int}
endsnippet
# main()
snippet main "func main() { ... }"
func main() {
${0:${VISUAL}}
}
endsnippet
# method
snippet meth "func (self Type) Method(...) [error] { ... }"
func (${1:receiver} ${2:type}) ${3:name}(${4:params})${5/(.+)/ /}`!p opening_par(snip, 5)`$5`!p closing_par(snip, 5)` {
${0:${VISUAL}}
}
endsnippet
# ok
snippet ok "if !ok { ... }"
if !ok {
${0:${VISUAL}}
}
endsnippet
# package
snippet package "package ..."
// Package $1 provides ${2:...}
package ${1:main}
${0}
endsnippet
# panic
snippet pn "panic()"
panic("${0:msg}")
endsnippet
# return
snippet rt "return"
return ${0:${VISUAL}}
endsnippet
# select
snippet select "select { case a := <-chan: ... }"
select {
case ${1:v1} := <-${2:chan1}
${0}
}
endsnippet
# struct
snippet st "type T struct { ... }"
type ${1:Type} struct {
${0}
}
endsnippet
# switch
snippet switch "switch x { ... }"
switch ${1:var} {
case ${2:value1}:
${0}
}
endsnippet
# sprintf
snippet sp "fmt.Sprintf(...)"
fmt.Sprintf("%${1:s}", ${2:var})
endsnippet
# goroutine named function
snippet go "go someFunc(...)"
go ${1:funcName}(${0})
endsnippet
# goroutine anonymous function
snippet gof "go func() { ... }()"
go func() {
${1:${VISUAL}}
}()
${0}
endsnippet
# test function
snippet test "func TestXYZ(t *testing.T) { ... }"
func Test${1:Function}(t *testing.T) {
${0:${VISUAL}}
}
endsnippet
# quick test server
snippet tsrv "httptest.NewServer"
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintln(w, ${1:`response`})
}))
defer ts.Close()
${0:someUrl} = ts.URL
endsnippet
# test error handling
snippet ter "if err != nil { t.Errorf(...) }"
if err != nil {
t.Errorf("${0:message}")
}
endsnippet
# test fatal error
snippet terf "if err != nil { t.Fatalf(...) }"
if err != nil {
t.Fatalf("${0:message}")
}
endsnippet
# variable declaration
snippet var "var x Type [= ...]"
var ${1:x} ${2:Type}${3: = ${0:value}}
endsnippet
# variables declaration
snippet vars "var ( ... )"
var (
${1:x} ${2:Type}${3: = ${0:value}}
)
endsnippet
# equals fails the test if exp is not equal to act.
snippet eq "equals: test two identifiers with DeepEqual"
if !reflect.DeepEqual(${1:expected}, ${2:actual}) {
_, file, line, _ := runtime.Caller(0)
fmt.Printf("%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\n\n", filepath.Base(file), line, $1, $2)
t.FailNow()
}
endsnippet
global !p
import re
# Automatically wrap return types with parentheses
def return_values(s):
# remove everything wrapped in parentheses
s = re.sub("\(.*?\)|\([^)]*$", "", s)
return len(s.split(","))
def opening_par(snip, pos):
if return_values(t[pos]) > 1 and not t[pos].startswith("("):
snip.rv = "("
else:
snip.rv = ""
def closing_par(snip, pos):
if return_values(t[pos]) > 1:
snip.rv = ")"
else:
snip.rv = ""
endglobal
# vim:ft=snippets: