11global !p
22import uuid
33import re
4+ import textwrap
45
56mod_re = re.compile(' angular.module\(["\' ]*(.*?)["\' \),]' )
67type_re = re.compile(' \.(controller|directive|service|factory|provider|value)\(' )
78component_re = re.compile(' \.(?:controller|directive|service|factory|provider)\(["\' ](.*?)["\' ]' )
89DEFAULT_NAME = ' appName'
910DEFAULT_TYPE_NAME = ' type'
1011DEFAULT_COMPONENT_NAME = ' componentName'
11- CHECK_LINES = 20
1212
1313def 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 ])
1515
16- def get_ng_component_of (snip ):
16+ def get_ng_component_of ():
1717 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()):
2623 res = type_re.findall(text)
2724 if len (res) > 0 :
28- snip.rv += res[0 ]
25+ return res[len (res) - 1 ]
2926 else :
30- snip.rv += DEFAULT_TYPE_NAME
27+ return DEFAULT_TYPE_NAME
3128
32- def get_ng_component (snip , ** kwargs ):
33- text = kwargs.get(' text' , bufText())
29+ def get_ng_component (text = bufText()):
3430 res = component_re.findall(text)
3531 if len (res) > 0 :
36- snip.rv += res[0 ]
32+ return res[len (res) - 1 ]
3733 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' )
3940
40- def get_ng_module (snip , ** kwargs ):
41- text = kwargs.get(' text' , bufText())
4241 res = mod_re.findall(text)
4342 if len (res) > 0 :
44- snip.rv += res[0 ]
43+ return res[len (res) - 1 ]
4544 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
4777endglobal
4878
4979snippet ngc " Define a new Angular Controller. You can change the controller name and parameters."
@@ -236,23 +266,23 @@ endsnippet
236266snippet /s " NGDoc service" !b
237267/**
238268 * @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()` }
240270 * @description \` $2 \` is a service ${0: to do $2 }
241271 */
242272endsnippet
243273
244- snippet /c " NGDoc service " !b
274+ snippet /c " NGDoc controller " !b
245275/**
246276 * @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()` }
248278 * @description \` $2 \` is a controller ${0: to do $2 }
249279 */
250280endsnippet
251281
252282snippet /m " NGDoc Method" !b
253283/**
254284 * @ngdoc
255- * @methodOf ${1: `!p get_ng_component_of(snip )` }
285+ * @methodOf ${1: `!p snip.rv += get_ng_component_of()` }
256286 * @name $1 #${2: name }
257287 * @description ${0: \` $2 \` is a method to }
258288 */
@@ -261,7 +291,7 @@ endsnippet
261291snippet /p " NGDoc Property" !b
262292/**
263293 * @ngdoc
264- * @propertyOf ${1: `!p get_ng_component_of(snip )` }
294+ * @propertyOf ${1: `!p snip.rv += get_ng_component_of()` }
265295 * @name $1 #${2: name }
266296 * @description ${0: \` $2 \` is a property for }
267297 */
@@ -270,19 +300,61 @@ endsnippet
270300snippet /d " NGDoc directive" !b
271301/**
272302 * @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()` }
274304 * @param {${2: Type } } ${3: name } ${4: description }
275305 * @description ${0: Description }
276306 * @restrict ECA
277307 */
278308endsnippet
279309
280- snippet */ex " Example comment"
310+ snippet */ex " Example comment"
281311* @example
282312* <example>
283313* <file name="${1: index.html } ">
284314* </file>
285315* </example>
286316endsnippet
287317
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+
288360# vim:ft=snippets:
0 commit comments