Skip to content

Commit 861515b

Browse files
committed
Add override for angular module name, documentation
1 parent 82c87a9 commit 861515b

File tree

3 files changed

+393
-26
lines changed

3 files changed

+393
-26
lines changed

2

Lines changed: 358 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,358 @@
1+
global !p
2+
import uuid
3+
import re
4+
import textwrap
5+
6+
mod_re = re.compile('angular.module\(["\']*(.*?)["\'\),]')
7+
type_re = re.compile('\.(controller|directive|service|factory|provider|value)\(')
8+
component_re = re.compile('\.(?:controller|directive|service|factory|provider)\(["\'](.*?)["\']')
9+
DEFAULT_NAME = 'appName'
10+
DEFAULT_TYPE_NAME = 'type'
11+
DEFAULT_COMPONENT_NAME = 'componentName'
12+
13+
def bufText():
14+
return '\n'.join(vim.current.window.buffer[:vim.current.window.cursor[0]+1])
15+
16+
def get_ng_component_of(snip):
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(text=bufText()):
25+
res = type_re.findall(text)
26+
if len(res) > 0:
27+
snip.rv += res[len(res)-1]
28+
else:
29+
snip.rv += DEFAULT_TYPE_NAME
30+
31+
def get_ng_component(text=bufText()):
32+
res = component_re.findall(text)
33+
if len(res) > 0:
34+
snip.rv += res[len(res)-1]
35+
else:
36+
snip.rv += DEFAULT_COMPONENT_NAME
37+
38+
def get_ng_module(text=bufText()):
39+
res = mod_re.findall(text)
40+
if len(res) > 0:
41+
snip.rv += res[len(res)-1]
42+
else:
43+
snip.rv += DEFAULT_NAME
44+
45+
def injector_phrase(v):
46+
return '%s = $injector.get(\'%s\')' % (v, v)
47+
48+
def trim_list(p):
49+
while p[-1] in (',', ';'): p = p[:-1]
50+
return '\n'.join(v for v in p.split('\n')
51+
if len(v.strip()) > 0
52+
and v.strip() not in (',', ';')
53+
and v[:2] != '//')
54+
55+
56+
def set_injectors(var_list, pre=''):
57+
var_list = trim_list(var_list)
58+
if len(var_list) == 0: return ''
59+
60+
phrase = textwrap.dedent(
61+
'''
62+
beforeEach(inject(function($injector) {
63+
%s
64+
}));
65+
''')
66+
67+
injectors = '\n\t'.join(injector_phrase(v) for v in var_list.split(', '))
68+
69+
phrase = phrase % (injectors,)
70+
71+
if len(pre) > 0:
72+
return ('\n' + pre).join(phrase.split('\n'))
73+
74+
return phrase
75+
endglobal
76+
77+
snippet ngc "Define a new Angular Controller. You can change the controller name and parameters."
78+
var ${1:controllerName} = function(${2:\$scope}, ${3:injectables}) {
79+
$0
80+
};
81+
endsnippet
82+
83+
snippet ngfor "angular.foreach loop"
84+
angular.forEach(${1:iterateOver}, function(value, key) {
85+
$0
86+
});
87+
endsnippet
88+
89+
90+
snippet ngm "A new angular module without a config function."
91+
angular.module('${1:moduleName}', [${2:moduleDependencies}]);
92+
$0
93+
endsnippet
94+
95+
96+
snippet ngma "A new angular module without a config function and a variable assignment."
97+
var ${1:moduleName} = angular.module('$1$', [${2:moduleDeps}]);
98+
$0
99+
endsnippet
100+
101+
102+
snippet ngmc "A new angular module with a config function"
103+
var ${1:moduleName} = angular.module('$1', [${2:moduleDeps}], function(${3:configDeps}) {
104+
$0
105+
});
106+
endsnippet
107+
108+
109+
snippet ngmfa "A factory in a module"
110+
factory('${1:factoryName}', function(${2:dependencies}) {
111+
$0
112+
});
113+
endsnippet
114+
115+
116+
snippet ngms "Define an Angular Module Service to be attached to a previously defined module. You can change the service name and service injectables."
117+
service('${1:serviceName}', function(${2:injectables}) {
118+
$0
119+
});
120+
endsnippet
121+
122+
123+
snippet ngmfi "Define an Angular Module Filter to be attached to a previously defined module. You can change the filter name."
124+
filter('${1:filterName}', function(${2:injectables}) {
125+
return function(input, ${3:args}) {
126+
$0
127+
};
128+
})
129+
endsnippet
130+
131+
132+
133+
# Route Based Snippets
134+
snippet ngrw "Defines a when condition of an AngularJS route."
135+
$routeProvider.when('${1:url}', {
136+
templateUrl: '${2:templateUrl}',
137+
controller: '${3:controller}'
138+
});
139+
$0
140+
endsnippet
141+
142+
143+
144+
snippet ngrwr "Defines a when condition of an AngularJS route with the resolve block."
145+
$routeProvider.when('${1:url}', {
146+
templateUrl: '${2:templateUrl}',
147+
controller: '${3:controller}',
148+
resolve: {
149+
$4
150+
}
151+
});
152+
$0
153+
endsnippet
154+
155+
156+
snippet ngro "Defines an otherwise condition of an AngularJS route."
157+
$routeProvider.otherwise({
158+
redirectTo: '${1:url}'
159+
});
160+
$0
161+
endsnippet
162+
163+
164+
# Scope Related Snippets
165+
snippet $f "Define a new $scope'd function (usually inside an AngularJS Controller). You can change the function name and arguments."
166+
$scope.${1:functionName} = function(${2:args}) {
167+
$0
168+
};
169+
endsnippet
170+
171+
172+
snippet $v "Defines a new $scope'd variable inside an AngularJS controller."
173+
$scope.${1:variable} = ${2:value};
174+
$0
175+
endsnippet
176+
177+
178+
snippet $va "Defines a new $scope'd variable inside an AngularJS controller and assigns a value from a constructor arguments."
179+
$scope.${1:variable} = ${2:variable};
180+
$0
181+
endsnippet
182+
183+
184+
snippet $w "Define a $watch for an expression. You can change the expression to be watched."
185+
$scope.$watch('${1:watchExpr}',function(newValue, oldValue) {
186+
$0
187+
});
188+
endsnippet
189+
190+
191+
snippet $on "Define a $on for a $broadcast/$emit on the $scope inside an Angular Controller. You can change the event name to listen on."
192+
$scope.$on('${1:eventName}', function(event, ${2:args}) {
193+
$0
194+
});
195+
endsnippet
196+
197+
198+
snippet $b "Define a $broadcast for a $scope inside an Angular Controller / Angular Controller Function. You can change the event name and optional event arguments."
199+
$scope.$broadcast('${1:eventName}', ${2:eventArgs});
200+
$0
201+
endsnippet
202+
203+
204+
snippet $e "Define an $emit for a $scope inside an Angular Controller / Angular Controller Function. You can change the event name and optional event arguments."
205+
$scope.$emit('${1:eventName}', ${2:eventArgs});
206+
$0
207+
endsnippet
208+
209+
210+
# Directive related snippets
211+
snippet ngdcf "A compile function"
212+
function compile(tElement, tAttrs, transclude) {
213+
return function (scope, element, attrs) {
214+
$0
215+
}
216+
}
217+
endsnippet
218+
219+
220+
snippet ngdlf "A linking function in a directive."
221+
function (scope, element, attrs${1:ctrl}) {
222+
$0
223+
}
224+
endsnippet
225+
226+
227+
snippet ngdc "A directive with a compile function"
228+
directive('${1:directiveName}', function factory(${2:injectables}) {
229+
var directiveDefinitionObject = {
230+
${3:directiveAttrs},
231+
compile: function compile(tElement, tAttrs, transclude) {
232+
return function (scope, element, attrs) {
233+
$0
234+
}
235+
}
236+
};
237+
return directiveDefinitionObject;
238+
});
239+
endsnippet
240+
241+
242+
snippet ngdl "A directive with a linking function only."
243+
.directive('${1:directiveName}', function(${2:directiveDeps}) {
244+
return function(scope, element, attrs${3:ctrl}) {
245+
$0
246+
}
247+
});
248+
endsnippet
249+
250+
snippet hget "$http.get" !b
251+
$http.get('${1:/uri}'${3:, ${2:query}})
252+
.success(function(${4:collection}) {
253+
${5:$scope.$4 = $4;}
254+
})
255+
.error(function(err) {
256+
${6:console.log(err);}
257+
})
258+
endsnippet
259+
260+
snippet $iget "Injector Get" !b
261+
${1:variable} = $injector.get('${2:$1}');
262+
endsnippet
263+
264+
snippet /s "NGDoc service" !b
265+
/**
266+
* @ngdoc service
267+
* @name ${1:`!p get_ng_module(snip)`}.service:${2:`!p get_ng_component(snip)`}
268+
* @description \`$2\` is a service ${0:to do $2}
269+
*/
270+
endsnippet
271+
272+
snippet /c "NGDoc controller" !b
273+
/**
274+
* @ngdoc controller
275+
* @name ${1:`!p get_ng_module(snip)`}.controller:${2:`!p get_ng_component(snip)`}
276+
* @description \`$2\` is a controller ${0:to do $2}
277+
*/
278+
endsnippet
279+
280+
snippet /m "NGDoc Method" !b
281+
/**
282+
* @ngdoc
283+
* @methodOf ${1:`!p get_ng_component_of(snip)`}
284+
* @name $1#${2:name}
285+
* @description ${0:\`$2\` is a method to}
286+
*/
287+
endsnippet
288+
289+
snippet /p "NGDoc Property" !b
290+
/**
291+
* @ngdoc
292+
* @propertyOf ${1:`!p get_ng_component_of(snip)`}
293+
* @name $1#${2:name}
294+
* @description ${0:\`$2\` is a property for}
295+
*/
296+
endsnippet
297+
298+
snippet /d "NGDoc directive" !b
299+
/**
300+
* @ngdoc directive
301+
* @name ${1:`!p get_ng_module(snip)`.directive:`!p get_ng_component(snip)`}
302+
* @param {${2: Type}} ${3: name} ${4: description}
303+
* @description ${0:Description}
304+
* @restrict ECA
305+
*/
306+
endsnippet
307+
308+
snippet */ex "Example comment"
309+
* @example
310+
* <example>
311+
* <file name="${1:index.html}">
312+
* </file>
313+
* </example>
314+
endsnippet
315+
316+
snippet r$q "return $q(function ..." !b
317+
return \$q(function(resolve, reject) {
318+
${1://asynchronously resolve()}
319+
});$0
320+
endsnippet
321+
322+
# TESTING snippets
323+
324+
snippet inj "angular inject() function" !b
325+
inject(function(${1:thingsToInject}) {
326+
${0://Do something with $1}
327+
});
328+
endsnippet
329+
330+
snippet $ig "$injector.get" !b
331+
${1:something} = \$injector.get('${2:$1}');
332+
endsnippet
333+
334+
snippet mod "" !b
335+
beforeEach(module(function(${1:\$provide}) {
336+
${0:\$provide.service('TestThing', function(/*injected*/) {
337+
this.foo = 'bar';
338+
\});}
339+
}));
340+
endsnippet
341+
342+
snippet injg "Set a local variable via injection" !b
343+
var ${1:injectable}; inject(function(_$1_) { $1 = _$1_; });
344+
endsnippet
345+
346+
snippet test "karma angular test scaffold" !b
347+
describe('${1:something to be tested}', function() {
348+
//Setup
349+
beforeEach(module('${2:someModule}'));
350+
351+
var ${3://Injectables}`!p snip += set_injectors(t[3], '\t')`
352+
353+
//Tests
354+
$0
355+
});
356+
endsnippet
357+
358+
# vim:ft=snippets:

0 commit comments

Comments
 (0)