~telemachus/vim-dotfiles

ref: 09d2ddfb1d58cbdf5cd15100d241fd4ee75e6a04 vim-dotfiles/UltiSnips/go.snippets -rw-r--r-- 5.9 KiB
09d2ddfbPeter Aronoff Update go snippets 4 months ago
                                                                                
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
# Snippets for go, mostly from vim-go

global !p

import re


# Thanks to https://github.com/fatih/vim-go/
# Automatically wrap multiple return values in parentheses
def return_values(s):
  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 = ""


# Thanks to https://vi.stackexchange.com/a/26882/7933
# Endless imports
def delline_exitimport(snip):
  cursorline = snip.cursor[0]
  del snip.buffer[cursorline]

  # Add a blank line after import.
  # NB: cursorline + 1 is the current line number.
  snip.buffer.append("\n", cursorline+1)

  # Place cursor on blank line after import.
  snip.cursor.set(cursorline+1, 0)


def in_import(snip):
  # Get position using searchpairpos('import (','',')','bnWz')
  beginpos = vim.eval("searchpairpos('^import (','','^)','bnWz')")
  endpos = vim.eval("searchpairpos('^import','','^)', 'nWz')")
  curpos = snip.cursor.to_vim_cursor()
  if beginpos == ['0','0'] or endpos == ['0','0']: return False
  if pos_comp(beginpos, curpos) < 0 and pos_comp(curpos, endpos) < 0:
    return True
  else:
    return False

def pos_comp(cur1, cur2):
  # Returns -1 if cur1 is before cur2, +1 if cur1 is after cur2, and 0 if
  # cur1 == cur2. cur1 and cur2 are lists of the form [row, col].
  cur1r = int(cur1[0])
  cur1c = int(cur1[1])
  cur2r = int(cur2[0])
  cur2c = int(cur2[1])
  if cur1r < cur2r: return -1
  elif cur1r > cur2r: return  1
  else:
    if cur1c < cur2c: return -1
    elif cur1c > cur2c: return  1
    else: return 0

endglobal

# package {main}
snippet pkg "package {main}"
package ${1:main}
${0}
endsnippet

# shorthand variable declaration
snippet : "v := value"
${1} := ${0}
endsnippet

# full variable declaration
snippet var "var name type = expression"
var ${1:name}${2/(.+)/ /}${2:type} = ${3:exp}
endsnippet

# defer
snippet df "defer someFunction()"
defer ${1:func}(${2})
${0}
endsnippet

snippet fmt "import fmt" b
import "fmt"${0}
endsnippet

# import
context "not in_import(snip)"
snippet import "import ( ... )"
import (
	"$1"
)
endsnippet

context "in_import(snip)"
snippet "^	\"\S+$" "create next import" r
`!p snip.rv=match.group(0)`"
	"${1}
endsnippet

context "in_import(snip)"
post_jump "delline_exitimport(snip)"
snippet "^	\"$" "exit import" r
endsnippet

# for loop
snippet for "for ... { ... }"
for ${1} {
	${2:${VISUAL}}
}${0}
endsnippet

# for integer loop
snippet fori "for 0..N-1 { ... }"
for ${1:i} := 0; $1 < ${2:N}; $1++ {
	${3:${VISUAL}}
}${0}
endsnippet

# for range loop
snippet forr "for k, v := range items { ... }"
for ${2:k}, ${3:v} := range ${1} {
	${4:${VISUAL}}
}${0}
endsnippet

# map
snippet map "map[Type]Type"
map[${1:string}]${2:int}${0}
endsnippet

# main()
snippet main "func main() { ... }" b
func main() {
	${0:${VISUAL}}
}
endsnippet

# I took these from https://github.com/honza/vim-snippets.
#
# Whoever wrote that converted several from
# https://github.com/AlanQuatermain/go-tmbundle
snippet /^cons/ "constants declaration" r
const (
	${1:constant}${2/(.+)/ /}${2:type} = ${0:value}
)
endsnippet

snippet /^con/ "constant declaration" r
const ${1:name}${2/(.+)/ /}${2:type} = ${0:value}
endsnippet

snippet iota "iota constant generator" b
const (
	${1:constant}${2/(.+)/ /}${2:type} = iota
)
endsnippet

snippet if "if statement" b
if ${1:condition}${1/(.+)/ /}{
	${2:${VISUAL}}
}${0}
endsnippet

snippet switch "switch statement" b
switch ${1:expression}${1/(.+)/ /}{
case$0
}
endsnippet

snippet struct "struct declaration" b
type ${1:name} struct {
	${2:${VISUAL}}
}${0}
endsnippet

# define a function
snippet func "func name(...) [error] { ... }"
func ${1:name}(${2:params})${3/(.+)/ /}`!p opening_par(snip, 3)`$3`!p closing_par(snip, 3)` {
	${0:${VISUAL}}
}
endsnippet

# define a 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

# assign an anonymous function to a variable
# (I need a better name here, but this is brief.)
snippet anon "name := func(...) [error] { ... }"
${1:name} := func(${2:params})${3/(.+)/ /}`!p opening_par(snip, 3)`$3`!p closing_par(snip, 3)` {
	${0:${VISUAL}}
}
endsnippet

# error handling
snippet err "simple error handling" b
if err != nil {
	${1:FIXME}
}${0}
endsnippet

# if with inline error
snippet ife "if with inline error"
if err := ${1:condition}; err != nil {
	${2:${VISUAL}}
}${0}
endsnippet

# if with inline error and t.Fatalf
snippet iferrtf "if err: = <something>; err != nil { t.Fatalf }"
if err := ${1:condition}; err != nil {
	t.Fatalf(${2})
}${0}
endsnippet

# test function
snippet test "func TestXYZ(t *testing.T) { ... }"
func Test${1:Function}(t *testing.T) {
	${2:${VISUAL}}
}${0}
endsnippet

# write a helper function for tests
snippet testHelper "func Name(t testing.TB) { ... }"
func ${1:name}(t testing.TB) {
	${2:t.Helper()}
	${3:code}
}${0}
endsnippet

# call t.Run with an anonymous function
snippet trun "t.Run(message, func(t *testing.T) { ... })"
t.Run(${1:message}, func(t *testing.T) {
	${2:test code}
})${0}
endsnippet

# call t.Run with a named function
snippet trunn "t.Run(message, functionName)"
t.Run("${1:message}", ${2:funcName})${0}
endsnippet

# benchmark function
snippet bench "func BenchmarkXYZ(b *testing.B) { ... }"
func Benchmark${1:Name}(b *testing.B) {
	for i := 0; i < b.N; i++ {
		${2:${VISUAL}}
	}
}${0}
endsnippet

# actual != expected
snippet nexp "if actual != expected { t.Errorf(...) }"
if actual != expected {
	t.Errorf("expected %${1:q}; actual %${2:q}${3:; more}", expected, actual${4:, more})
}${0}
endsnippet

# block to check if an item is in a map
snippet nok "if <key>, ok := <map>[<value>]; !ok { ... }"
if ${1:key}, ok := ${2:map}[${3:value}]; !ok {
	${0}
}
endsnippet

# vim:ft=snippets: