1
- import sublime , itertools
1
+ import sublime
2
+
3
+ # Settings file names
4
+ ANGULARJS_SUBLIME_PACKAGE = 'AngularJS-sublime-package.sublime-settings'
5
+ ANGULARJS_JS_COMPLETIONS = 'AngularJS-js-completions.sublime-settings'
2
6
3
7
4
8
def js_disabled ():
5
- package_settings = sublime .load_settings ('AngularJS-sublime-package.sublime-settings' )
9
+ package_settings = sublime .load_settings (ANGULARJS_SUBLIME_PACKAGE )
6
10
return package_settings .get ('disable_default_js_completions' , False )
7
11
8
12
9
- def global_completions (word = None ):
10
- js_completions = sublime .load_settings ('AngularJS-js-completions.sublime-settings' )
13
+ def global_completions (view , prefix , locations , project_index ):
11
14
if js_disabled ():
12
15
return []
16
+
17
+ selector = 'meta.brace.round.js, meta.delimiter.object.comma.js'
18
+ if view .score_selector (locations [0 ]- 2 , selector ):
19
+ return DI_completions (prefix , project_index )
20
+
21
+ js_completions = sublime .load_settings (ANGULARJS_JS_COMPLETIONS )
22
+ word = None
23
+ if prefix == '' :
24
+ word = view .substr (view .word (locations [0 ] - 2 ))
25
+ # Check if we're possibly at a directive attrs param
26
+ if 'attrs' in word .lower ():
27
+ word = 'attrs'
28
+ # Check if we're possibly at a scope var
29
+ if 'scope' in word .lower ():
30
+ word = '$rootScope'
13
31
if word :
14
32
symbol = js_completions .get (word , [])
15
- # in the settings we have strings, instead of a list of completions
33
+ # in the settings we can have strings, instead of a list, for completions
16
34
# since multiple symbols can have the same set of completions
17
35
# which we prefix with '_'
18
36
if len (symbol ) and symbol [0 ] == '_' :
@@ -23,31 +41,54 @@ def global_completions(word=None):
23
41
24
42
25
43
def in_string_completions (prefix , project_index ):
26
- js_completions = sublime .load_settings ('AngularJS-js-completions.sublime-settings' )
27
44
if js_disabled ():
28
45
return []
29
- events = []
30
- injectables_list = []
46
+ events = event_completions (prefix , project_index )
47
+ injectables = DI_completions (prefix , project_index )
48
+ return events + injectables
49
+
50
+
51
+ def DI_completions (prefix , project_index ):
52
+ js_completions = sublime .load_settings (ANGULARJS_JS_COMPLETIONS )
53
+ # TODO: Add option to turn off DI inject completions
54
+ if js_disabled ():
55
+ return []
56
+ DIs = []
31
57
if prefix == '$' :
32
- events = list (js_completions .get ('events' , []))
33
- events = [tuple (event ) for event in events ]
34
- injectables = list (js_completions .get ('js_completions' , []))
58
+ DIs_list = list (js_completions .get ('js_completions' , []))
35
59
# filter out the js_completions list so that we only include
36
60
# items prefixed with '$' and use their simple form for the completion
37
61
# Also, suffix them with (DI) to signify that they're a Dependency Injection
38
- for injectable in injectables :
39
- if injectable [0 ][0 ] == '$' :
40
- injectables_list .append ((injectable [0 ] + '(DI)' , '\\ ' + injectable [0 ].split ('\t ' )[0 ]))
41
- custom_list = get (('constant' , 'factory' , 'service' , 'value' ), project_index )
42
- return events + injectables_list + custom_list
62
+ for DI in DIs_list :
63
+ if DI [0 ].startswith ('$' ):
64
+ DIs .append ((DI [0 ] + '(DI)' , '\\ ' + DI [0 ].split ('\t ' )[0 ]))
65
+ # Now look for any injectable items that have been indexed
66
+ injectables = ('constant' , 'factory' , 'service' , 'value' )
67
+ custom_DIs = get (injectables , project_index , '(DI)' )
68
+ return DIs + custom_DIs
69
+
70
+
71
+ def event_completions (prefix , project_index ):
72
+ js_completions = sublime .load_settings (ANGULARJS_JS_COMPLETIONS )
73
+ # TODO: Add option to turn off event completions
74
+ if js_disabled ():
75
+ return []
76
+ events = []
77
+ if prefix == '$' :
78
+ events = list (js_completions .get ('events' , []))
79
+ events = [tuple (event ) for event in events ]
80
+ return events
43
81
44
82
45
- def get (type , project_index ):
83
+ def get (type , project_index , suffix = '' ):
46
84
all_defs = project_index .get ('definitions' )
47
85
types = []
48
86
for completion in all_defs :
49
87
if completion [0 ].startswith (type ):
50
- trigger = completion [0 ].split (': ' )[1 ].replace ('.' , '_' ) + '\t AngularJS'
51
- result = completion [0 ].split (': ' )[1 ]
88
+ # Split based on 'Type: name'
89
+ trigger = completion [0 ].split (': ' )[1 ]
90
+ result = trigger
91
+ # Ensure we remove any '.' to prevent breaking ST completions
92
+ trigger = trigger .replace ('.' , '_' ) + '\t AngularJS' + suffix
52
93
types .append ((trigger , result ))
53
94
return list (set (types ))
0 commit comments