Skip to content
This repository was archived by the owner on May 25, 2019. It is now read-only.

Commit c320de8

Browse files
committed
refactors and bug fix
- Small refactor to jscompletions.py - Fixes issue where some DI injections in function params would expand improperly
1 parent 83bf04d commit c320de8

File tree

2 files changed

+61
-29
lines changed

2 files changed

+61
-29
lines changed

‎AngularJS-sublime-package.py

+1-10
Original file line numberDiff line numberDiff line change
@@ -354,16 +354,7 @@ def on_query_completions(self, view, prefix, locations):
354354
view.score_selector(_scope, ng.settings.get('js_scope'))
355355
and not view.substr(locations[0] - 1) in ng.settings.get('js_prefixes')
356356
):
357-
word = None
358-
if prefix == '':
359-
word = view.substr(view.word(locations[0] - 2))
360-
# Check if we're possibly at a directive attrs param
361-
if 'attrs' in word.lower():
362-
word = 'attrs'
363-
# Check if we're possibly at a scope var
364-
if 'scope' in word.lower():
365-
word = '$rootScope'
366-
return jscompletions.global_completions(word)
357+
return jscompletions.global_completions(view, prefix, locations, ng.get_current_project_indexes())
367358
if(view.score_selector(_scope, 'source.js string.quoted')):
368359
if viewlocation.at_line_with_module(view, locations):
369360
return jscompletions.get(('module'), ng.get_current_project_indexes())

‎jscompletions.py

+60-19
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,36 @@
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'
26

37

48
def js_disabled():
5-
package_settings = sublime.load_settings('AngularJS-sublime-package.sublime-settings')
9+
package_settings = sublime.load_settings(ANGULARJS_SUBLIME_PACKAGE)
610
return package_settings.get('disable_default_js_completions', False)
711

812

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):
1114
if js_disabled():
1215
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'
1331
if word:
1432
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
1634
# since multiple symbols can have the same set of completions
1735
# which we prefix with '_'
1836
if len(symbol) and symbol[0] == '_':
@@ -23,31 +41,54 @@ def global_completions(word=None):
2341

2442

2543
def in_string_completions(prefix, project_index):
26-
js_completions = sublime.load_settings('AngularJS-js-completions.sublime-settings')
2744
if js_disabled():
2845
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 = []
3157
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', []))
3559
# filter out the js_completions list so that we only include
3660
# items prefixed with '$' and use their simple form for the completion
3761
# 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
4381

4482

45-
def get(type, project_index):
83+
def get(type, project_index, suffix=''):
4684
all_defs = project_index.get('definitions')
4785
types = []
4886
for completion in all_defs:
4987
if completion[0].startswith(type):
50-
trigger = completion[0].split(': ')[1].replace('.', '_') + '\tAngularJS'
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('.', '_') + '\tAngularJS' + suffix
5293
types.append((trigger, result))
5394
return list(set(types))

0 commit comments

Comments
 (0)