Skip to content

Commit 0ae62ad

Browse files
committed
snippets: fix Ultisnips snippets, unify further #220
1 parent 48e62b8 commit 0ae62ad

File tree

2 files changed

+75
-69
lines changed

2 files changed

+75
-69
lines changed

gosnippets/UltiSnips/go.snippets

+64-62
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ endsnippet
7272
snippet df "defer someFunction()"
7373
defer ${1:func}(${2})
7474
${0}
75+
endsnippet
76+
7577
snippet def "defer func() { ... }"
7678
defer func() {
7779
${0}
@@ -90,22 +92,21 @@ endsnippet
9092
# gpl
9193
snippet gpl
9294
/*
93-
* This program is free software; you can redistribute it and/or modify
94-
* it under the terms of the GNU General Public License as published by
95-
* the Free Software Foundation; either version 2 of the License, or
96-
* (at your option) any later version.
97-
*
98-
* This program is distributed in the hope that it will be useful,
99-
* but WITHOUT ANY WARRANTY; without even the implied warranty of
100-
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
101-
* GNU General Public License for more details.
102-
*
103-
* You should have received a copy of the GNU General Public License
104-
* along with this program; if not, see <http://www.gnu.org/licenses/>.
105-
*
106-
* Copyright (C) ${1:Author}, `strftime("%Y")`
107-
*/
108-
95+
* This program is free software; you can redistribute it and/or modify
96+
* it under the terms of the GNU General Public License as published by
97+
* the Free Software Foundation; either version 2 of the License, or
98+
* (at your option) any later version.
99+
*
100+
* This program is distributed in the hope that it will be useful,
101+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
102+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
103+
* GNU General Public License for more details.
104+
*
105+
* You should have received a copy of the GNU General Public License
106+
* along with this program; if not, see <http://www.gnu.org/licenses/>.
107+
*
108+
* Copyright (C) ${1:Author}, `strftime("%Y")`
109+
*/
109110
${0}
110111
endsnippet
111112

@@ -131,23 +132,27 @@ if ${1:condition} {
131132
endsnippet
132133

133134
# else snippet
134-
abbr else { ... }
135135
snippet else
136136
else {
137137
${0}
138138
}
139139
endsnippet
140140

141141
# error snippet
142-
snippet er "if err != nil { ... }"
142+
snippet errn "Error return " !b
143143
if err != nil {
144-
${0}
144+
return err
145145
}
146-
snippet errn "if err != nil { return [...], err }"
147-
if ${1:err} != nil {
148-
return ${2}$1
146+
${0}
147+
endsnippet
148+
149+
snippet errn, "Error return with two return values" !b
150+
if err != nil {
151+
return ${1:nil}, err
149152
}
150153
${0}
154+
endsnippet
155+
151156
snippet json "\`json:key\`"
152157
\`json:"${1:keyName}"\`
153158
endsnippet
@@ -178,31 +183,6 @@ for ${2:k}, ${3:v} := range ${1} {
178183
}
179184
endsnippet
180185

181-
global !p
182-
183-
import re
184-
185-
# Automatically wrap return types with parentheses
186-
187-
def return_values(s):
188-
# remove everything wrapped in parentheses
189-
s = re.sub("\(.*?\)|\([^)]*$", "", s)
190-
return len(s.split(","))
191-
192-
def opening_par(snip, pos):
193-
if return_values(t[pos]) > 1 and not t[pos].startswith("("):
194-
snip.rv = "("
195-
else:
196-
snip.rv = ""
197-
198-
def closing_par(snip, pos):
199-
if return_values(t[pos]) > 1:
200-
snip.rv = ")"
201-
else:
202-
snip.rv = ""
203-
204-
endglobal
205-
206186
# function
207187
snippet func "func Function(...) [error] { ... }"
208188
func ${1:name}(${2:params})${3/(.+)/ /}`!p opening_par(snip, 3)`$3`!p closing_par(snip, 3)` {
@@ -250,8 +230,7 @@ func main() {
250230
endsnippet
251231

252232
# method
253-
snippet method "func (self Type) Method(...) [error] { ... }"
254-
regexp /^meth/
233+
snippet meth "func (self Type) Method(...) [error] { ... }"
255234
func (${1:receiver} ${2:type}) ${3:name}(${4:params})${5/(.+)/ /}`!p opening_par(snip, 5)`$5`!p closing_par(snip, 5)` {
256235
${0:${VISUAL}}
257236
}
@@ -260,7 +239,7 @@ endsnippet
260239
# ok
261240
snippet ok "if !ok { ... }"
262241
if !ok {
263-
${0}
242+
${0}
264243
}
265244
endsnippet
266245

@@ -272,16 +251,12 @@ ${0}
272251
endsnippet
273252

274253
# panic
275-
snippet panic
276-
alias pn
277-
abbr panic("...")
278-
panic("${0}")
254+
snippet pn "panic()"
255+
panic("${0:msg}")
279256
endsnippet
280257

281258
# return
282-
snippet return
283-
alias rt
284-
abbr return ...
259+
snippet rt "return"
285260
return ${0}
286261
endsnippet
287262

@@ -294,7 +269,7 @@ case ${1:v1} := <-${2:chan1}
294269
endsnippet
295270

296271
# struct
297-
snippet struct "type T struct { ... }"
272+
snippet st "type T struct { ... }"
298273
type ${1:Type} struct {
299274
${0}
300275
}
@@ -319,10 +294,10 @@ go ${1:funcName}(${0})
319294
endsnippet
320295

321296
# goroutine anonymous function
322-
snippet gof "go func(...) { ... }(...)"
323-
go func(${1}) {
324-
${3:/* TODO */}
325-
}(${2})
297+
snippet gof "go func() { ... }()"
298+
go func() {
299+
${1}
300+
}()
326301
endsnippet
327302

328303
# test function
@@ -344,4 +319,31 @@ ${1:x} ${2:Type}${3: = ${0:value\}}
344319
)
345320
endsnippet
346321
322+
global !p
323+
324+
import re
325+
326+
# Automatically wrap return types with parentheses
327+
328+
def return_values(s):
329+
# remove everything wrapped in parentheses
330+
s = re.sub("\(.*?\)|\([^)]*$", "", s)
331+
return len(s.split(","))
332+
333+
def opening_par(snip, pos):
334+
if return_values(t[pos]) > 1 and not t[pos].startswith("("):
335+
snip.rv = "("
336+
else:
337+
snip.rv = ""
338+
339+
def closing_par(snip, pos):
340+
if return_values(t[pos]) > 1:
341+
snip.rv = ")"
342+
else:
343+
snip.rv = ""
344+
345+
endglobal
346+
347+
347348
# vim:ft=snippets:
349+

gosnippets/snippets/go.snip

+11-7
Original file line numberDiff line numberDiff line change
@@ -115,17 +115,21 @@ snippet else
115115
${0}
116116
}
117117
# error snippet
118-
snippet er
118+
snippet errn
119119
abbr if err != nil { ... }
120120
if err != nil {
121-
${0}
121+
return err
122122
}
123-
snippet errn
123+
124+
# error snippet with two return values
125+
snippet errn,
124126
abbr if err != nil { return [...], err }
125-
if ${1:err} != nil {
126-
return ${2}$1
127+
if err != nil {
128+
return ${2}$1, err
127129
}
128130
${0}
131+
132+
# json snippet
129133
snippet json
130134
abbr \`json:key\`
131135
\`json:"${1:keyName}"\`
@@ -190,7 +194,7 @@ options head
190194
${0}
191195
}
192196
# method
193-
snippet method
197+
snippet meth
194198
abbr func (self Type) Method(...) [error] { ... }
195199
regexp /^meth/
196200
func (${1:self} ${2:Type}) ${3:Do}(${4}) ${5:error }{
@@ -226,7 +230,7 @@ abbr select { case a := <-chan: ... }
226230
${0}
227231
}
228232
# struct
229-
snippet struct
233+
snippet st
230234
abbr type T struct { ... }
231235
type ${1:Type} struct {
232236
${0}

0 commit comments

Comments
 (0)