1
1
import sublime , sublime_plugin , os , re , codecs , threading , json , time , glob , itertools
2
2
3
+ isST2 = int (sublime .version ()) < 3000
4
+
5
+ if isST2 :
6
+ import jscompletions
7
+ import viewlocation
8
+ import message
9
+ else :
10
+ from . import jscompletions
11
+ from . import viewlocation
12
+ from . import message
13
+
14
+
3
15
class AngularJS ():
4
16
def init (self , isST2 ):
5
17
self .isST2 = isST2
@@ -56,8 +68,8 @@ def get_current_project_indexes(self):
56
68
return self .projects_index_cache [self .get_index_key ()]
57
69
else :
58
70
return {'definitions' :[], 'attributes' : {}}
59
- def add_indexes_to_cache (self , indexes ):
60
71
72
+ def add_indexes_to_cache (self , indexes ):
61
73
self .projects_index_cache [self .get_index_key ()] = {
62
74
'definitions' : indexes [0 ],
63
75
'attributes' : indexes [1 ]
@@ -67,48 +79,12 @@ def add_indexes_to_cache(self, indexes):
67
79
j_data .write (json .dumps (self .projects_index_cache ))
68
80
j_data .close ()
69
81
70
- def at_html_attribute (self , attribute , locations ):
71
- view = self .active_view ()
72
- selector = view .match_selector (locations [0 ], 'text.html string' )
73
- if not selector : return False
74
- check_attribute = ''
75
- view_point = locations [0 ]
76
- char = ''
77
- while (char != ' ' and view_point > - 1 ):
78
- char = view .substr (view_point )
79
- if (char != ' ' ): check_attribute += char
80
- view_point -= 1
81
- check_attribute = check_attribute [::- 1 ]
82
- if check_attribute .startswith (attribute ):
83
- return True
84
- return False
85
-
86
- def find_word (self , region ):
87
- non_char = re .compile (self .settings .get ('non_word_chars' ))
88
- look_up_found = ""
89
- start_point = region .end ()
90
- begin_point = start_point - 1
91
- end_point = start_point + 1
92
-
93
- while (not non_char .search (self .active_view ().substr (sublime .Region (start_point , end_point )))
94
- and end_point ):
95
- end_point += 1
96
- while (not non_char .search (self .active_view ().substr (sublime .Region (begin_point , start_point )))):
97
- begin_point -= 1
98
-
99
- look_up_found = self .active_view ().substr (sublime .Region (begin_point + 1 , end_point - 1 ))
100
- self .alert ('Looking up: ' + look_up_found )
101
- return look_up_found
102
-
103
82
def handle_file_open_go_to (self , line ):
104
83
if not self .active_view ().is_loading ():
105
84
self .active_view ().run_command ('goto_line' , {'line' : line } )
106
85
else :
107
86
sublime .set_timeout (lambda : self .handle_file_open_go_to (line ), 100 )
108
87
109
- def alert (self , status_message ):
110
- sublime .status_message ('AngularJS: %s' % status_message )
111
-
112
88
#
113
89
# completions definitions/logic
114
90
#
@@ -188,6 +164,7 @@ def convertAttributesToSourceType(self, attrs):
188
164
# pattern to find multiple attributes within the completion
189
165
jadeAttrRegex = re .compile (r'([A-z-]+-\w+|\w+=)' )
190
166
hamlAttrRegex = re .compile (r'([A-z-]+-\w+.|\w+=)' )
167
+
191
168
def convertToHamlCompletion (attr ):
192
169
attrList = hamlAttrRegex .findall (attr )
193
170
if attrList :
@@ -216,7 +193,7 @@ def convertMultipleAttrExpantionToJade(attr):
216
193
if self .isSource ('source.jade' ):
217
194
return [(attr [0 ], convertMultipleAttrExpantionToJade (attr [1 ])) for attr in attrs ]
218
195
if self .isSource ('text.haml' ): return [(attr [0 ], convertToHamlCompletion (attr [1 ])) for attr in attrs ]
219
- return attrs ;
196
+ return attrs
220
197
221
198
def convertIndexedDirectiveToTag (self , directive ):
222
199
'''
@@ -227,17 +204,22 @@ def convertIndexedDirectiveToTag(self, directive):
227
204
return directive .replace ('="$1"$0' ,'' )+ '${1:($2)}$0'
228
205
elif self .isSource ('text.haml' ):
229
206
return '%' + directive .replace ('="$1"$0' ,'' )+ '${1:\\ {$2\\ }}$0'
230
- else : #assume HTML
207
+ else :
208
+ #assume HTML
231
209
return directive .replace ('="$1"$0' ,'' )+ '$1>$0</' + directive .replace ('="$1"$0' ,'' )+ '>'
232
210
233
211
def completions (self , view , prefix , locations , is_inside_tag ):
234
212
if is_inside_tag :
235
213
pt = locations [0 ] - len (prefix ) - 1
236
214
ch = view .substr (sublime .Region (pt , pt + 1 ))
237
215
238
- if (ch != '<'
239
- and not self .settings .get ('disable_default_directive_completions' )): attrs = self .attributes [:]
240
- else : attrs = []
216
+ if (
217
+ ch != '<'
218
+ and not self .settings .get ('disable_default_directive_completions' )
219
+ ):
220
+ attrs = self .attributes [:]
221
+ else :
222
+ attrs = []
241
223
attrs += self .get_isolate_completions (view , prefix , locations , pt )
242
224
attrs += self .add_indexed_directives ()
243
225
@@ -271,7 +253,8 @@ def completions(self, view, prefix, locations, is_inside_tag):
271
253
return []
272
254
273
255
def get_isolate_completions (self , view , prefix , locations , pt ):
274
- if self .settings .get ('disable_indexed_isolate_completions' ): return []
256
+ if self .settings .get ('disable_indexed_isolate_completions' ):
257
+ return []
275
258
276
259
# pulled lots from html_completions.py
277
260
SEARCH_LIMIT = 500
@@ -320,16 +303,10 @@ def filter_completions(self):
320
303
return []
321
304
322
305
def js_completions (self , word = None ):
323
- if self .settings .get ('disable_default_js_completions' ): return []
324
- if word :
325
- return [tuple (completion ) for completion in list (self .settings_js_completions .get (word , []))]
326
- else :
327
- return [tuple (completion ) for completion in list (self .settings_js_completions .get ('js_completions' , []))]
306
+ return jscompletions .global_completions (word )
328
307
329
- def js_event_completions (self , prefix ):
330
- if self .settings .get ('disable_default_js_completions' ): return []
331
- if prefix == '$' :
332
- return [tuple (completion ) for completion in list (self .settings_js_completions .get ('events' , []))]
308
+ def js_in_string_completions (self , prefix ):
309
+ return jscompletions .in_string_completions (prefix )
333
310
334
311
def add_indexed_directives (self ):
335
312
if self .settings .get ('disable_indexed_directive_completions' ): return []
@@ -379,10 +356,12 @@ def process_attributes(self):
379
356
if int (sublime .version ()) < 3000 :
380
357
ng .init (isST2 = True )
381
358
359
+
382
360
def plugin_loaded ():
383
361
global ng
384
362
ng .init (isST2 = False )
385
363
364
+
386
365
class AngularJSEventListener (sublime_plugin .EventListener ):
387
366
global ng
388
367
@@ -411,12 +390,10 @@ def on_query_completions(self, view, prefix, locations):
411
390
word = '$rootScope'
412
391
return ng .js_completions (word )
413
392
if (view .score_selector (_scope , 'source.js string.quoted' )):
414
- return ng .js_event_completions (prefix )
393
+ return ng .js_in_string_completions (prefix )
415
394
416
- if (ng .at_html_attribute ('ng-controller' , locations )):
417
- all_defs = ng .get_current_project_indexes ().get ('definitions' )
418
- controllers = [(completion [0 ].split (': ' )[1 ] + '\t AngularJS' , completion [0 ].split (': ' )[1 ]) for completion in all_defs if completion [0 ].startswith ('controller' )]
419
- return list (set (controllers ))
395
+ if (viewlocation .at_html_attribute (view , 'ng-controller' , locations )):
396
+ return jscompletions .controllers (ng .get_current_project_indexes ())
420
397
if (view .score_selector (_scope , ng .settings .get ('filter_scope' ))):
421
398
return ng .filter_completions ()
422
399
for selector in ng .settings .get ('attribute_avoided_scopes' ):
@@ -452,24 +429,26 @@ def on_post_save(self, view):
452
429
)
453
430
thread .start ()
454
431
432
+
455
433
class AngularjsDeleteCacheCommand (sublime_plugin .WindowCommand ):
456
434
global ng
457
435
458
436
def run (self ):
459
- ng .alert ('Deleting Cache' )
437
+ message .alert ('Deleting Cache' )
460
438
try :
461
439
os .remove (ng .index_cache_location )
462
440
except :
463
- ng .alert ('Deleting Cache: No cache file found.' )
441
+ message .alert ('Deleting Cache: No cache file found.' )
464
442
ng .projects_index_cache = {}
465
443
444
+
466
445
class AngularjsFileIndexCommand (sublime_plugin .WindowCommand ):
467
446
468
447
global ng
469
448
470
449
def run (self ):
471
450
if not ng .active_view ():
472
- ng .alert ('There was no active view found to process this command' )
451
+ message .alert ('There was no active view found to process this command' )
473
452
return
474
453
475
454
ng .is_indexing = True
@@ -487,13 +466,13 @@ def run(self):
487
466
self .track_walk_thread (thread )
488
467
489
468
def track_walk_thread (self , thread ):
490
- ng .alert ('indexing definitions' )
469
+ message .alert ('indexing definitions' )
491
470
492
471
if thread .is_alive ():
493
472
sublime .set_timeout (lambda : self .track_walk_thread (thread ), 1000 )
494
473
else :
495
474
ng .add_indexes_to_cache (thread .result )
496
- ng .alert ('indexing completed in ' + str (thread .time_taken ))
475
+ message .alert ('indexing completed in ' + str (thread .time_taken ))
497
476
ng .is_indexing = False
498
477
499
478
@@ -553,14 +532,13 @@ def handle_file_open_go_to(self, line):
553
532
554
533
555
534
class AngularjsGoToDefinitionCommand (sublime_plugin .WindowCommand ):
556
-
557
535
global ng
558
536
559
537
def run (self ):
560
538
self .active_view = ng .active_view ()
561
539
562
540
if not ng .get_current_project_indexes ().get ('definitions' ):
563
- ng .alert ('No indexing found for project' )
541
+ message .alert ('No indexing found for project' )
564
542
return
565
543
566
544
# grab first region
@@ -569,7 +547,7 @@ def run(self):
569
547
# no selection has been made
570
548
# so begin expanding to find word
571
549
if not region .size ():
572
- definition = ng .find_word (region )
550
+ definition = viewlocation .find_word (self . active_view , region )
573
551
else :
574
552
definition = self .active_view .substr (region )
575
553
@@ -585,11 +563,10 @@ def run(self):
585
563
self .active_view = ng .active_window ().open_file (item [1 ])
586
564
ng .handle_file_open_go_to (int (item [2 ]))
587
565
return
588
- ng .alert ('definition "%s" could not be found' % definition )
566
+ message .alert ('definition "%s" could not be found' % definition )
589
567
590
568
591
569
class AngularjsGoToDocumentationCommand (sublime_plugin .WindowCommand ):
592
-
593
570
global ng
594
571
595
572
def run (self ):
@@ -603,7 +580,7 @@ def run(self):
603
580
# no selection has been made
604
581
# so begin expanding to find word
605
582
if not region .size ():
606
- definition = ng .find_word (region )
583
+ definition = viewlocation .find_word (ng . active_view (), region )
607
584
else :
608
585
definition = self .active_view .substr (region )
609
586
@@ -619,7 +596,6 @@ def run(self):
619
596
620
597
621
598
class AngularJSThread (threading .Thread ):
622
-
623
599
global ng
624
600
625
601
def __init__ (self , ** kwargs ):
@@ -699,7 +675,7 @@ def reindex_file(self, index_key):
699
675
if (not file_path .endswith (tuple (self .kwargs ['exclude_file_suffixes' ]))
700
676
and index_key in ng .projects_index_cache
701
677
and not [skip for skip in self .kwargs ['exclude_dirs' ] if os .path .normpath (skip ) in file_path ]):
702
- ng .alert ('Reindexing ' + self .kwargs ['file_path' ])
678
+ message .alert ('Reindexing ' + self .kwargs ['file_path' ])
703
679
project_index = ng .get_project_indexes_at (index_key )
704
680
705
681
project_index [:] = [
0 commit comments