forked from ryboe/CSS3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcss3_completions.py
224 lines (169 loc) · 9.67 KB
/
css3_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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
from CSS3.completions import at_rules
from CSS3.completions import descriptors
from CSS3.completions import functions
from CSS3.completions import properties
from CSS3.completions import selectors
from CSS3.completions import types
from CSS3.completions import util
import sublime
import sublime_plugin
class CSS3Completions(sublime_plugin.EventListener):
def on_query_completions(self, view, prefix, locations):
"""Populate the completions menu based on the current cursor location.
Args:
view (sublime.View): A Sublime API object that contains the
match_selector() method for detecting if the current scope has
completions, and the substr() method for getting text from the
document.
prefix (str): The first part of the text that triggered the
completions menu, e.g. "tex" for "text-decoration".
locations (list: int): The current integer positions of the cursors.
Returns:
[(<label>, <completion), ...], inhibit_flag
<label> is what will appear in the completions menu. <completion> is
the snippet that will be inserted. inhibit_flag controls whether
"word completions" are offered as well. "Word completions" are
a list of every word in the current file greater than four
characters long.
"""
if not view.match_selector(locations[0], "source.css"):
return []
if view.match_selector(locations[0], "comment.block.css"):
return []
# If there's multiple cursors, we can't offer completions.
# body {
# foo: |<- cursor
# bar: |<- second cursor
# }
# Which values do we offer? foo's or bar's?
if len(locations) > 1:
return [], sublime.INHIBIT_WORD_COMPLETIONS
# start determines which completions are offered.
# |--prefix--|
# start ->text-decorat|<- current cursor location
start = locations[0] - len(prefix)
# INSIDE FUNCTIONS
if view.match_selector(start, "source.css meta.function."):
return handle_function_completions(view, start)
# PROPERTY VALUES
if view.match_selector(start - 1, "source.css meta.property-value."):
# The current character position could be at the semicolon, which
# is one character past the "meta.property-value." content scope:
# text-decoration: |;
# ^ meta.property-value.text-decoration.css
# So we need to step back one character (start - 1).
return handle_property_value_completions(view, start - 1)
# PROPERTY NAMES
if view.match_selector(start, "meta.declaration-list.css -meta.selector.css -meta.property-value."):
return properties.names, sublime.INHIBIT_WORD_COMPLETIONS
# @-RULES
if view.substr(start - 1) == "@":
return handle_at_rule_completions(view, start)
# @IMPORT
if view.match_selector(start, "meta.at-rule.import.css -meta.media-feature.css"):
return types.media_types + [types.string, types.url], sublime.INHIBIT_WORD_COMPLETIONS
# DESCRIPTOR VALUES
if view.match_selector(start - 1, "source.css meta.descriptor."):
return handle_descriptor_value_completions(view, start - 1)
# @PAGE PROPERTY NAMES
if view.match_selector(start, "meta.at-rule.page.block.css"):
return properties.names, sublime.INHIBIT_WORD_COMPLETIONS
# @PAGE SELECTORS, e.g. :left, :recto
if view.match_selector(start, "meta.selector.page.css"):
return selectors.at_page, sublime.INHIBIT_WORD_COMPLETIONS
# @FONT-FACE DESCRIPTOR NAMES
if view.match_selector(start, "meta.at-rule.font-face.block.css -meta.descriptor.font-face."):
return descriptors.font_face, sublime.INHIBIT_WORD_COMPLETIONS
# @FONT-PALETTE-VALUES DESCRIPTOR NAMES
if view.match_selector(start, "meta.at-rule.font-palette-values.block.css -meta.descriptor.font-palette-values."):
return descriptors.font_palette_values, sublime.INHIBIT_WORD_COMPLETIONS
# @VIEWPORT DESCRIPTOR NAMES
if view.match_selector(start, "meta.at-rule.viewport.block.css -meta.descriptor.viewport."):
return descriptors.viewport, sublime.INHIBIT_WORD_COMPLETIONS
# @COUNTER-STYLE DESCRIPTOR NAMES
if view.match_selector(start, "meta.at-rule.counter-style.block.css -meta.descriptor.counter-style."):
return descriptors.counter_style, sublime.INHIBIT_WORD_COMPLETIONS
# @COLOR-PROFILE DESCRIPTOR NAMES
if view.match_selector(start, "meta.at-rule.color-profile.block.css -meta.descriptor.color-profile."):
return descriptors.color_profile, sublime.INHIBIT_WORD_COMPLETIONS
# @NAMESPACE VALUES
if view.match_selector(start, "meta.at-rule.namespace.css"):
return at_rules.namespace_values
# MEDIA FEATURES
if view.match_selector(start, "meta.media-feature.css -meta.property-value."):
return properties.media_features, sublime.INHIBIT_WORD_COMPLETIONS
# MEDIA TYPES
# FIXME: This will not offer completions for media types nested in other
# @media blocks because of the "-meta.at-rule.media.block.css" selector.
if view.match_selector(start, "meta.at-rule.media.css -meta.at-rule.media.block.css"):
return types.media_types, sublime.INHIBIT_WORD_COMPLETIONS
# @SUPPORTS CONDITIONS
if view.match_selector(start, "meta.supports-condition.css"):
return properties.supports_conditions, sublime.INHIBIT_WORD_COMPLETIONS
# @SUPPORTS CONDITION OPERATORS
if view.match_selector(start, "meta.at-rule.supports.css -meta.at-rule.supports.block.css"):
return types.supports_condition_operator, sublime.INHIBIT_WORD_COMPLETIONS
# KEYFRAMES SELECTOR
# Special case: Keyframes selectors are not scoped with "meta.selector.css"
if view.match_selector(start, "meta.at-rule.keyframes.block.css -meta.declaration-list.css"):
return selectors.keyframes, sublime.INHIBIT_WORD_COMPLETIONS
# SELECTORS
if view.match_selector(start, "meta.selector.css"):
return handle_selector_completions(view, start)
def handle_at_rule_completions(view, location):
if view.match_selector(location, "meta.at-rule.page.block.css"):
return at_rules.page_margin_boxes, sublime.INHIBIT_WORD_COMPLETIONS
if view.match_selector(location, "meta.at-rule.font-feature-values.block.css -meta.font-feature-type-block.css"):
return at_rules.font_feature_types, sublime.INHIBIT_WORD_COMPLETIONS
if at_rules.supports_nested(view, location):
return at_rules.nestable, sublime.INHIBIT_WORD_COMPLETIONS
if view.match_selector(location, "source.css -meta.at-rule."):
return at_rules.all_rules, sublime.INHIBIT_WORD_COMPLETIONS
return [], sublime.INHIBIT_WORD_COMPLETIONS
def handle_descriptor_value_completions(view, location):
scopes = util.get_scopes(view, location)
# >>> scopes = ["source.css", "meta.descriptor.viewport.zoom.css"]
# >>> util.get_scope_that_starts_with(scopes, starts_with="meta.descriptor.")
# 'meta.descriptor.viewport.zoom.css'
scope = util.get_scope_that_starts_with(scopes, starts_with="meta.descriptor.")
# "meta.descriptor.viewport.zoom.css" ->
# ['meta', 'descriptor', 'viewport', 'zoom', 'css']
# type name
type_index, name_index = 2, 3
descriptor_type, descriptor_name = scope.split(".")[type_index:name_index+1]
return descriptors.get_values(descriptor_type, descriptor_name)
def handle_function_completions(view, location):
scopes = util.get_scopes(view, location)
# >>> scopes = ["source.css", "meta.function.ca.c.css"]
# >>> util.get_scope_that_starts_with(scopes, starts_with="meta.function.")
# 'meta.function.calc.css'
scope = util.get_scope_that_starts_with(scopes, starts_with="meta.function.")
# "meta.function.calc.css" ->
# ["meta", "function", "calc", "css"]
# name
name_index = -2
func_name = scope.split(".")[name_index]
return functions.get_completions(func_name)
def handle_property_value_completions(view, location):
scopes = util.get_scopes(view, location)
# >>> scopes = ["source.css", "meta.property-value.width.css"]
# >>> util.get_scope_that_starts_with(scopes, starts_with="meta.property-value.")
# 'meta.property-value.width.css'
scope = util.get_scope_that_starts_with(scopes, starts_with="meta.property-value.")
# "meta.property-value.width.css" ->
# ['meta', 'property-value', 'width', 'css']
# name
name_index = -2
property_name = scope.split(".")[name_index]
return properties.get_values(property_name)
def handle_selector_completions(view, location):
if view.match_selector(location - 1, "punctuation.definition.entity.pseudo-element.css"):
return selectors.pseudo_elements, sublime.INHIBIT_WORD_COMPLETIONS
if view.match_selector(location - 1, "punctuation.definition.entity.pseudo-class.css"):
return selectors.pseudo_classes, sublime.INHIBIT_WORD_COMPLETIONS
if view.match_selector(location, "meta.at-rule.keyframes.block.css -meta.declaration-list.css"):
return selectors.keyframes, sublime.INHIBIT_WORD_COMPLETIONS
# If we're not in a class, id, pseudo-class, or pseudo-element, offer HTML
# tags as completions.
if view.match_selector(location, "source.css -entity.other.attribute-name."):
return selectors.html_tags