1
1
global !p
2
2
import uuid
3
3
import re
4
+ import textwrap
4
5
5
6
mod_re = re.compile(' angular.module\(["\' ]*(.*?)["\' \),]' )
6
7
type_re = re.compile(' \.(controller|directive|service|factory|provider|value)\(' )
7
8
component_re = re.compile(' \.(?:controller|directive|service|factory|provider)\(["\' ](.*?)["\' ]' )
8
9
DEFAULT_NAME = ' appName'
9
10
DEFAULT_TYPE_NAME = ' type'
10
11
DEFAULT_COMPONENT_NAME = ' componentName'
11
- CHECK_LINES = 20
12
12
13
13
def bufText ():
14
- return ' \n ' .join(vim.current.window.buffer[:CHECK_LINES ])
14
+ return ' \n ' .join(vim.current.window.buffer[:vim.current.window.cursor[ 0 ] + 1 ])
15
15
16
- def get_ng_component_of (snip ):
16
+ def get_ng_component_of ():
17
17
text = bufText()
18
- get_ng_module(snip, text = text)
19
- snip.rv += ' .'
20
- get_ng_type(snip, text = text)
21
- snip.rv += ' :'
22
- get_ng_component(snip, text = text)
23
-
24
- def get_ng_type (snip , ** kwargs ):
25
- text = kwargs.get(' text' , bufText())
18
+ return " %s .%s :%s " % (get_ng_module(text),
19
+ get_ng_type(text),
20
+ get_ng_component(text))
21
+
22
+ def get_ng_type (text = bufText()):
26
23
res = type_re.findall(text)
27
24
if len (res) > 0 :
28
- snip.rv += res[0 ]
25
+ return res[len (res) - 1 ]
29
26
else :
30
- snip.rv += DEFAULT_TYPE_NAME
27
+ return DEFAULT_TYPE_NAME
31
28
32
- def get_ng_component (snip , ** kwargs ):
33
- text = kwargs.get(' text' , bufText())
29
+ def get_ng_component (text = bufText()):
34
30
res = component_re.findall(text)
35
31
if len (res) > 0 :
36
- snip.rv += res[0 ]
32
+ return res[len (res) - 1 ]
37
33
else :
38
- snip.rv += DEFAULT_COMPONENT_NAME
34
+ return DEFAULT_COMPONENT_NAME
35
+
36
+ def get_ng_module (text = bufText()):
37
+ explicit_name = vim.vars.get(' ngdoc_module_name' , None )
38
+ if explicit_name:
39
+ return explicit_name.decode(' utf-8' )
39
40
40
- def get_ng_module (snip , ** kwargs ):
41
- text = kwargs.get(' text' , bufText())
42
41
res = mod_re.findall(text)
43
42
if len (res) > 0 :
44
- snip.rv += res[0 ]
43
+ return res[len (res) - 1 ]
45
44
else :
46
- snip.rv += DEFAULT_NAME
45
+ return DEFAULT_NAME
46
+
47
+ def injector_phrase (v ):
48
+ return ' %s = $injector.get(\' %s \' )' % (v, v)
49
+
50
+ def trim_list (p ):
51
+ while p[- 1 ] in (' ,' , ' ;' ): p = p[:- 1 ]
52
+ return ' \n ' .join(v for v in p.split(' \n ' )
53
+ if len (v.strip()) > 0
54
+ and v.strip() not in (' ,' , ' ;' )
55
+ and v[:2 ] != ' //' )
56
+
57
+
58
+ def set_injectors (var_list , pre = ' ' ):
59
+ var_list = trim_list(var_list)
60
+ if len (var_list) == 0 : return ' '
61
+
62
+ phrase = textwrap.dedent(
63
+ '''
64
+ beforeEach(inject(function($injector) {
65
+ %s
66
+ }));
67
+ ''' )
68
+
69
+ injectors = ' ;\n\t ' .join(injector_phrase(v) for v in var_list.split(' , ' ))
70
+
71
+ phrase = phrase % (injectors,)
72
+
73
+ if len (pre) > 0 :
74
+ return (' \n ' + pre).join(phrase.split(' \n ' ))
75
+
76
+ return phrase
47
77
endglobal
48
78
49
79
snippet ngc " Define a new Angular Controller. You can change the controller name and parameters."
@@ -236,23 +266,23 @@ endsnippet
236
266
snippet /s " NGDoc service" !b
237
267
/**
238
268
* @ngdoc service
239
- * @name ${1: `!p get_ng_module(snip )` } .service:${2: `!p get_ng_component(snip )` }
269
+ * @name ${1: `!p snip.rv += get_ng_module()` } .service:${2: `!p snip.rv += get_ng_component()` }
240
270
* @description \` $2 \` is a service ${0: to do $2 }
241
271
*/
242
272
endsnippet
243
273
244
- snippet /c " NGDoc service " !b
274
+ snippet /c " NGDoc controller " !b
245
275
/**
246
276
* @ngdoc controller
247
- * @name ${1: `!p get_ng_module(snip )` } .controller:${2: `!p get_ng_component(snip )` }
277
+ * @name ${1: `!p snip.rv += get_ng_module()` } .controller:${2: `!p snip.rv += get_ng_component()` }
248
278
* @description \` $2 \` is a controller ${0: to do $2 }
249
279
*/
250
280
endsnippet
251
281
252
282
snippet /m " NGDoc Method" !b
253
283
/**
254
284
* @ngdoc
255
- * @methodOf ${1: `!p get_ng_component_of(snip )` }
285
+ * @methodOf ${1: `!p snip.rv += get_ng_component_of()` }
256
286
* @name $1 #${2: name }
257
287
* @description ${0: \` $2 \` is a method to }
258
288
*/
@@ -261,7 +291,7 @@ endsnippet
261
291
snippet /p " NGDoc Property" !b
262
292
/**
263
293
* @ngdoc
264
- * @propertyOf ${1: `!p get_ng_component_of(snip )` }
294
+ * @propertyOf ${1: `!p snip.rv += get_ng_component_of()` }
265
295
* @name $1 #${2: name }
266
296
* @description ${0: \` $2 \` is a property for }
267
297
*/
@@ -270,19 +300,61 @@ endsnippet
270
300
snippet /d " NGDoc directive" !b
271
301
/**
272
302
* @ngdoc directive
273
- * @name ${1: `!p get_ng_module(snip )`.directive:`!p get_ng_component(snip )` }
303
+ * @name ${1: `!p snip.rv += get_ng_module()`.directive:`!p snip.rv += get_ng_component()` }
274
304
* @param {${2: Type } } ${3: name } ${4: description }
275
305
* @description ${0: Description }
276
306
* @restrict ECA
277
307
*/
278
308
endsnippet
279
309
280
- snippet */ex " Example comment"
310
+ snippet */ex " Example comment"
281
311
* @example
282
312
* <example>
283
313
* <file name="${1: index.html } ">
284
314
* </file>
285
315
* </example>
286
316
endsnippet
287
317
318
+ snippet r$q " return $q(function ..." !b
319
+ return \$ q(function(resolve, reject) {
320
+ ${1: //asynchronously resolve() }
321
+ });$0
322
+ endsnippet
323
+
324
+ # TESTING snippets
325
+
326
+ snippet inj " angular inject() function" !b
327
+ inject(function(${1: thingsToInject } ) {
328
+ ${0: //Do something with $1 }
329
+ });
330
+ endsnippet
331
+
332
+ snippet $ig " $injector.get" !b
333
+ ${1: something } = \$ injector.get('${2: $1 } ');
334
+ endsnippet
335
+
336
+ snippet mod " " !b
337
+ beforeEach(module(function(${1: \$ provide } ) {
338
+ ${0: \$ provide.service('TestThing', function(/*injected*/) {
339
+ this.foo = 'bar';
340
+ \} ); }
341
+ }));
342
+ endsnippet
343
+
344
+ snippet injg " Set a local variable via injection" !b
345
+ var ${1: injectable } ; inject(function(_$1 _) { $1 = _$1 _; });
346
+ endsnippet
347
+
348
+ snippet test " karma angular test scaffold" !b
349
+ describe('${1: something to be tested } ', function() {
350
+ //Setup
351
+ beforeEach(module('${2: someModule } '));
352
+
353
+ var ${3: //Injectables } `!p snip += set_injectors(t[3 ], ' \t ' ) `
354
+
355
+ //Tests
356
+ $0
357
+ });
358
+ endsnippet
359
+
288
360
# vim:ft=snippets:
0 commit comments