forked from subhaze/CSS-Extended
-
Notifications
You must be signed in to change notification settings - Fork 0
/
css_style_completions.py
189 lines (159 loc) · 6.59 KB
/
css_style_completions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import sublime, sublime_plugin, os
ST2 = int(sublime.version()) < 3000
if ST2:
# ST 2
import completions
import cache
import settings
import location
import style_parser
else:
# ST 3
from . import completions
from . import cache
from . import settings
from . import location
from . import style_parser
def plugin_loaded():
cache.load()
style_parser.init_file_loading()
class CssStyleCompletionPruneCacheCommand(sublime_plugin.WindowCommand):
"""Removes missing files from the cache"""
def run(self):
cache.prune_cache()
class CssStyleCompletionDeleteCacheCommand(sublime_plugin.WindowCommand):
"""Deletes all cache that plugin has created"""
def run(self):
cache.remove_cache()
cache.projects_cache = {}
class AddToCacheCommand(sublime_plugin.WindowCommand):
def run(self, paths=[], name="", file_type="*.*"):
import glob
current_delay = 500
for path in paths:
if os.path.isdir(path):
sublime.set_timeout(lambda: style_parser.load_files(
glob.glob(path + os.path.sep + file_type),
as_scratch=False
), current_delay)
current_delay = current_delay + 100
class CssStyleCompletionEvent(sublime_plugin.EventListener):
#TODO: DRY up the sync/async logic
def on_post_save(self, view):
if not ST2:
return
file_name = view.file_name()
if not file_name:
return
style_parser.load_linked_files(view)
style_parser.parse_view(view)
def on_post_save_async(self, view):
file_name = view.file_name()
if not file_name:
return
style_parser.load_linked_files(view)
style_parser.parse_view(view)
def on_load(self, view):
if settings.get('auto_trigger_emmet_completions', False):
emmet_trigger = {'selector': 'text.html', 'characters': '.#'}
triggers = view.settings().get('auto_complete_triggers')
triggers.append(emmet_trigger)
view.settings().set('auto_complete_triggers', triggers)
def on_query_completions(self, view, prefix, locations):
# inside HTML scope completions
html_attr_scope = view.match_selector(
locations[0], settings.get("html_attribute_scope")
)
class_attr = location.at_html_attribute('class', view, locations)
if class_attr and html_attr_scope:
return (
completions.returnSymbolCompletions(view, 'class'),
sublime.INHIBIT_WORD_COMPLETIONS
)
id_attr = location.at_html_attribute('id', view, locations)
if id_attr and html_attr_scope:
return (
completions.returnSymbolCompletions(view, 'id'),
sublime.INHIBIT_WORD_COMPLETIONS
)
# inside HTML with Emmet completions
if settings.get("use_emmet"):
if location.at_style_symbol('.', settings.get("emmet_scope"), view, locations):
return (completions.returnSymbolCompletions(view, 'class'), sublime.INHIBIT_WORD_COMPLETIONS)
if location.at_style_symbol('#', settings.get("emmet_scope"), view, locations):
return (completions.returnSymbolCompletions(view, 'id'), sublime.INHIBIT_WORD_COMPLETIONS)
# inside CSS scope pseudo completions
if location.at_style_symbol(':', settings.get("css_completion_scope"), view, locations):
return (completions.returnPseudoCompletions(), sublime.INHIBIT_WORD_COMPLETIONS)
# inside CSS scope symbol completions
if location.at_style_symbol('.', settings.get("css_completion_scope"), view, locations):
return (completions.returnSymbolCompletions(view, 'class'), sublime.INHIBIT_WORD_COMPLETIONS)
if location.at_style_symbol('#', settings.get("css_completion_scope"), view, locations):
return (completions.returnSymbolCompletions(view, 'id'), sublime.INHIBIT_WORD_COMPLETIONS)
# inside LESS scope symbol completions
if location.at_style_symbol(
'@', 'source.less',
view, locations
):
return (
completions.returnSymbolCompletions(
view, 'less_var'
), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
if location.at_style_symbol(
'.', 'source.less - parameter.less',
view, locations
):
return (
(completions.returnSymbolCompletions(
view, 'less_mixin'
) + completions.returnSymbolCompletions(
view, 'class'
)), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
# inside SCSS scope symbol completions
if location.at_style_symbol(
'.', 'source.scss',
view, locations
):
return (
completions.returnSymbolCompletions(
view, 'class'
), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
if location.at_style_symbol(
'%', 'source.scss',
view, locations
):
return (
completions.returnSymbolCompletions(
view, 'scss_placeholder'
), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
if location.at_style_symbol(
'$', 'source.scss, meta.property-value.scss',
view, locations
):
return (
completions.returnSymbolCompletions(
view, 'scss_var'
), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
if view.match_selector(
locations[0],
'meta.property-list.scss meta.at-rule.include.scss - punctuation.section.function.scss'
):
return (
completions.returnSymbolCompletions(
view , 'scss_mixin'
), sublime.INHIBIT_EXPLICIT_COMPLETIONS | sublime.INHIBIT_WORD_COMPLETIONS
)
# return element list
if view.match_selector(
locations[0],
'source.stylus, source.css - meta.property-value.css, source.less - meta.property-value.css, source.scss - meta.property-value.scss'
):
return (completions.returnElementCompletions(), sublime.INHIBIT_WORD_COMPLETIONS)
return None
if ST2:
plugin_loaded()