aboutsummaryrefslogtreecommitdiff
path: root/vim/bundle/vim-snippets/UltiSnips/java.snippets
blob: 54d026dfbf508fd728f589e93e09c953e0cdcdc2 (plain)
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
priority -50

# Many of the snippets here use a global option called
# "g:ultisnips_java_brace_style" which, if set to "nl" will put a newline
# before '{' braces.
# Setting "g:ultisnips_java_junit" will change how the test method snippet
# looks, it is defaulted to junit4, setting this option to 3 will remove the
# @Test annotation from the method

global !p
def junit(snip):
	if snip.opt("g:ultisnips_java_junit", "") == "3":
		snip += ""
	else:
		snip.rv += "@Test\n\t"

def nl(snip):
	if snip.opt("g:ultisnips_java_brace_style", "") == "nl":
		snip += ""
	else:
		snip.rv += " "
def getArgs(group):
	import re
	word = re.compile('[a-zA-Z0-9><.]+ \w+')
	return [i.split(" ") for i in word.findall(group) ]

def camel(word):
	if not word: return ''
	return word[0].upper() + word[1:]

def mixedCase(word):
	if not word: return ''
	return word[0].lower() + word[1:]

endglobal

snippet sleep "try sleep catch" b
try {
	Thread.sleep(${1:1000});
} catch (InterruptedException e){
	e.printStackTrace();
}
endsnippet

snippet /i|n/ "new primitive or int" br
${1:int} ${2:i} = ${3:1};
$0
endsnippet

snippet /o|v/ "new Object or variable" br
${1:Object} ${2:var} = new $1(${3});
endsnippet

snippet f "field" b
${1:private} ${2:String} ${3:`!p snip.rv = t[2].lower()`};
endsnippet

snippet ab "abstract" b
abstract $0
endsnippet

snippet as "assert" b
assert ${1:test}${2/(.+)/(?1: \: ")/}${2:Failure message}${2/(.+)/(?1:")/};
endsnippet

snippet at "assert true" b
assertTrue(${1:actual});
endsnippet

snippet af "assert false" b
assertFalse(${1:actual});
endsnippet

snippet ae "assert equals" b
assertEquals(${1:expected}, ${2:actual});
endsnippet

snippet br "break"
break;

endsnippet

snippet cs "case" b
case $1:
	$2
$0
endsnippet

snippet ca "catch" b
catch (${1:Exception} ${2:e})`!p nl(snip)`{
	$0
}
endsnippet

snippet cle "class extends" b
public class ${1:`!p
snip.rv = snip.basename or "untitled"`} ${2:extends ${3:Parent} }${4:implements ${5:Interface} }{
	$0
}
endsnippet

snippet clc "class with constructor, fields, setter and getters" b
public class `!p
snip.rv = snip.basename or "untitled"` {
`!p
args = getArgs(t[1])
if len(args) == 0: snip.rv = ""
for i in args:
	snip.rv += "\n\tprivate " + i[0] + " " + i[1]+ ";"
if len(args) > 0:
	snip.rv += "\n"`
	public `!p snip.rv = snip.basename or "unknown"`($1) {`!p
args = getArgs(t[1])
for i in args:
	snip.rv += "\n\t\tthis." + i[1] + " = " + i[1] + ";"
if len(args) == 0:
	snip.rv += "\n"`
	}$0
`!p
args = getArgs(t[1])
if len(args) == 0: snip.rv = ""
for i in args:
	snip.rv += "\n\tpublic void set" + camel(i[1]) + "(" + i[0] + " " + i[1] + ") {\n" + "\
	\tthis." + i[1] + " = " + i[1] + ";\n\t}\n"

	snip.rv += "\n\tpublic " + i[0] + " get" + camel(i[1]) + "() {\n\
	\treturn " + i[1] + ";\n\t}\n"
`
}
endsnippet

snippet clc "class with constructor, with field names" b
public class `!p
snip.rv = snip.basename or "untitled"` {
`!p
args = getArgs(t[1])
for i in args:
	snip.rv += "\n\tprivate " + i[0] + " " + i[1]+ ";"
if len(args) > 0:
	snip.rv += "\n"`
	public `!p snip.rv = snip.basename or "unknown"`($1) {`!p
args = getArgs(t[1])
for i in args:
	snip.rv += "\n\t\tthis.%s = %s;" % (i[1], i[1])
if len(args) == 0:
	snip.rv += "\n"`
	}
}
endsnippet

snippet clc "class and constructor" b
public class `!p
snip.rv = snip.basename or "untitled"` {

	public `!p snip.rv = snip.basename or "untitled"`($2) {
		$0
	}
}
endsnippet

snippet cl "class" b
public class ${1:`!p
snip.rv = snip.basename or "untitled"`} {
	$0
}
endsnippet

snippet cos "constant string" b
public static final String ${1:var} = "$2";$0
endsnippet

snippet co "constant" b
public static final ${1:String} ${2:var} = $3;$0
endsnippet

snippet de "default" b
default:
	$0
endsnippet

snippet elif "else if"
else if ($1)`!p nl(snip)`{
	$0${VISUAL}
}
endsnippet

snippet el "else" w
else`!p nl(snip)`{
	$0${VISUAL}
}
endsnippet

snippet fi "final" b
final $0
endsnippet

snippet fore "for (each)" b
for ($1 : $2)`!p nl(snip)`{
	$0
}
endsnippet

snippet fori "for" b
for (int ${1:i} = 0; $1 < ${2:10}; $1++)`!p nl(snip)`{
	$0
}
endsnippet

snippet for "for" b
for ($1; $2; $3)`!p nl(snip)`{
	$0
}
endsnippet

snippet if "if" b
if ($1)`!p nl(snip)`{
	$0${VISUAL}
}
endsnippet

snippet imt "import junit_framework_TestCase;"  b
import junit.framework.TestCase;
$0
endsnippet

snippet im "import"  b
import ${1:java}.${2:util}.$0;
endsnippet

snippet in "interface" b
interface ${1:`!p snip.rv = snip.basename or "untitled"`} ${2:extends ${3:Parent} }{
	$0
}
endsnippet

snippet cc "constructor call or setter body"
this.${1:var} = $1;
endsnippet

snippet list "Collections List" b
List<${1:String}> ${2:list} = new ${3:Array}List<$1>();
endsnippet

snippet map "Collections Map" b
Map<${1:String}, ${2:String}> ${3:map} = new ${4:Hash}Map<$1, $2>();
endsnippet

snippet set "Collections Set" b
Set<${1:String}> ${2:set} = new ${3:Hash}Set<$1>();
endsnippet

snippet /Str?|str/ "String" br
String $0
endsnippet

snippet cn "Constructor" b
public `!p snip.rv = snip.basename or "untitled"`(${1:}) {
	$0
}
endsnippet

snippet cn "constructor, \w fields + assigments" b
	`!p
args = getArgs(t[1])
for i in args:
	snip.rv += "\n\tprivate " + i[0] + " " + i[1]+ ";"
if len(args) > 0:
	snip.rv += "\n"`
public `!p snip.rv = snip.basename or "unknown"`($1) {`!p
args = getArgs(t[1])
for i in args:
	snip.rv += "\n\t\tthis.%s = %s;" % (i[1], i[1])
if len(args) == 0:
	snip.rv += "\n"`
}
endsnippet

snippet j.b "java_beans_" i
java.beans.
endsnippet

snippet j.i "java_io" i
java.io.
endsnippet

snippet j.m "java_math" i
java.math.
endsnippet

snippet j.n "java_net_" i
java.net.
endsnippet

snippet j.u "java_util_"  i
java.util.
endsnippet

snippet main "method (main)" b
public static void main(String[] args)`!p nl(snip)`{
	$0
}
endsnippet

snippet try "try/catch" b
try {
	$1${VISUAL}
} catch(${2:Exception} ${3:e}){
	${4:e.printStackTrace();}
}
endsnippet

snippet mt "method throws" b
${1:private} ${2:void} ${3:method}(${4}) ${5:throws $6 }{
	$0
}
endsnippet

snippet m  "method" b
${1:private} ${2:void} ${3:method}(${4}) {
	$0
}
endsnippet

snippet md "Method With javadoc" b
/**
 * ${7:Short Description}`!p
for i in getArgs(t[4]):
	snip.rv += "\n\t * @param " + i[1] + " usage..."`
 *`!p
if "throws" in t[5]:
	snip.rv = "\n\t * @throws " + t[6]
else:
	snip.rv = ""``!p
if not "void" in t[2]:
	snip.rv = "\n\t * @return object"
else:
	snip.rv = ""`
 **/
${1:public} ${2:void} ${3:method}($4) ${5:throws $6 }{
	$0
}
endsnippet

snippet /get(ter)?/ "getter" br
public ${1:String} get${2:Name}() {
	return `!p snip.rv = mixedCase(t[2])`;
}
endsnippet

snippet /set(ter)?/ "setter" br
public void set${1:Name}(${2:String} `!p snip.rv = mixedCase(t[1])`) {
	this.`!p snip.rv = mixedCase(t[1])` = `!p snip.rv = mixedCase(t[1])`;
}
endsnippet

snippet /se?tge?t|ge?tse?t|gs/ "setter and getter" br
public void set${1:Name}(${2:String} `!p snip.rv = mixedCase(t[1])`) {
	this.`!p snip.rv = mixedCase(t[1])` = `!p snip.rv = mixedCase(t[1])`;
}`!p snip.rv += "\n"`
public $2 get$1() {
	return `!p snip.rv = mixedCase(t[1])`;
}
endsnippet

snippet pa "package" b
package $0
endsnippet

snippet p "print" b
System.out.print($1);$0
endsnippet

snippet pl "println"  b
System.out.println($1);$0
endsnippet

snippet pr "private" b
private $0
endsnippet

snippet po "protected" b
protected $0
endsnippet

snippet pu "public" b
public $0
endsnippet

snippet re "return" b
return $0
endsnippet

snippet st "static"
static $0
endsnippet

snippet sw "switch" b
switch ($1)`!p nl(snip)`{
	case $2: $0
}
endsnippet

snippet sy "synchronized"
synchronized $0
endsnippet

snippet tc "test case"
public class ${1:`!p snip.rv = snip.basename or "untitled"`} extends ${2:TestCase}`!p nl(snip)`{
	$0
}
endsnippet

snippet t "test" b
`!p junit(snip)`public void test${1:Name}() {
	$0
}
endsnippet

snippet tt "test throws" b
`!p junit(snip)`public void test${1:Name}() ${2:throws Exception }{
	$0
}
endsnippet

snippet th "throw" b
throw new $0
endsnippet

snippet wh "while" b
while ($1)`!p nl(snip)`{
	$0
}
endsnippet

# vim:ft=snippets: