-
Notifications
You must be signed in to change notification settings - Fork 6
/
TailwindCSSAutocomplete.py
212 lines (185 loc) · 8.25 KB
/
TailwindCSSAutocomplete.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
import sublime_plugin
import sublime
import os
import subprocess
import json
import re
class TailwindCSSAutocomplete(sublime_plugin.EventListener):
instances = {}
def get_completions(self, view, folder):
self.instances[folder] = {}
tw = self.find_file(
folder,
['tailwind.js', 'tailwind.config.js', 'tailwind-config.js', '.tailwindrc.js'],
exclude_dirs = ['node_modules']
)
tw_plugin = self.find_node_module(folder, 'tailwindcss')
if tw is not None and tw_plugin is not None:
try:
script = 'var sublime={config:"' + tw + '",plugin:"' + tw_plugin + '"};'
script += sublime.load_resource('Packages/TailwindCSSAutocomplete/dist/bundle.js') + '\n'
process = subprocess.Popen(
[view.settings().get('node_path', 'node')],
stdin = subprocess.PIPE,
stdout = subprocess.PIPE,
shell = sublime.platform() == 'windows'
)
output = process.communicate(input = script.encode('utf-8'), timeout = 15)[0]
path = output.decode('utf-8').splitlines()[-1]
class_names = json.loads(path)
self.instances[folder]['config_file'] = tw
self.instances[folder]['separator'] = class_names.get('separator')
self.instances[folder]['class_names'] = class_names.get('classNames')
self.instances[folder]['screens'] = class_names.get('screens')
self.instances[folder]['items'] = self.get_items_from_class_names(class_names.get('classNames'), class_names.get('screens'))
self.instances[folder]['config'] = class_names.get('config')
self.instances[folder]['config_items'] = self.get_config_items(class_names.get('config'))
except TimeoutExpired:
process.kill()
process.communicate()
except:
pass
def get_items_from_class_names(self, class_names, screens, keys = []):
if class_names is None:
return []
items = []
for class_name in list(class_names):
styles = class_names.get(class_name)
if isinstance(styles, str):
for k in keys:
styles = re.sub(':%s \{(.*?)\}' % k, r'\1', styles)
items = items + [('%s \t%s' % (class_name, styles), class_name)]
elif screens.get(class_name) is not None:
items = items + [('%s: \t@media (min-width: %s)' % (class_name, screens.get(class_name)), class_name + ':')]
else:
items = items + [('%s:' % class_name, class_name + ':')]
return items
def get_config_items(self, config):
items = []
exclude = ['modules', 'options', 'plugins']
for key in list(config):
value = config.get(key)
if isinstance(value, str):
items = items + [('%s \t%s' % (key, value), key)]
elif isinstance(value, list) and key not in exclude:
items = items + [('%s \t%s' % (key, ', '.join(value)), key)]
elif key not in exclude:
items = items + [(key, key + '.')]
return items
# there's a default snippet in sublime that prints a semi-colon when
# you type a colon within a CSS rule. e.g. "color:_" -> "color:_;"
# we override this if we are inside an @apply
def on_text_command(self, view, command_name, args):
cursor = view.sel()[0].begin()
isCss = view.match_selector(cursor, 'source.css meta.property-list.css')
if isCss == False:
return None
if command_name == 'insert_snippet' and args.get('contents') == ':$0;':
LIMIT = 250
start = max(0, cursor - LIMIT)
line = view.substr(sublime.Region(start, cursor))
match = re.match('.*?@apply ([^;}]*)$', line, re.DOTALL | re.IGNORECASE)
if match is None:
return None
return ('insert', { 'characters': ':' })
else:
return None
def on_activated_async(self, view, force = False):
if view.window() is None:
return
for folder in view.window().folders():
if view.file_name() is not None and view.file_name().startswith(os.path.abspath(folder) + os.sep):
if force == False and folder in self.instances:
break
else:
self.get_completions(view, folder)
break
def on_post_save_async(self, view):
for folder in self.instances:
if view.file_name() == self.instances[folder]['config_file']:
self.on_activated_async(view, force = True)
break
def on_query_completions(self, view, prefix, locations):
items = None
class_names = None
screens = None
for folder in self.instances:
if view.file_name() is not None and view.file_name().startswith(os.path.abspath(folder) + os.sep):
items = self.instances[folder]['items']
config_items = self.instances[folder]['config_items']
config = self.instances[folder]['config']
class_names = self.instances[folder]['class_names']
screens = self.instances[folder]['screens']
break
if items is None:
return []
isCss = view.match_selector(locations[0], 'source.css meta.property-list.css')
isHtml = view.match_selector(locations[0], 'text.html string.quoted') or view.match_selector(locations[0], 'string.quoted.jsx')
if isCss == False and isHtml == False:
return []
LIMIT = 250
cursor = locations[0] # - len(prefix) - 1
start = max(0, cursor - LIMIT)
line = view.substr(sublime.Region(start, cursor))
if isCss:
match = re.match('.*?@apply ([^;}]*)$', line, re.DOTALL | re.IGNORECASE)
elif isHtml:
match = re.search('\\bclass(Name)?=["\']([^"\']*)$', line, re.IGNORECASE)
if match is not None:
parts = match.group(len(match.groups())).split(' ')
string = parts[-1]
if string.startswith('.'):
string = string[1:]
keys = re.findall('(.*?):', string)
if keys is None:
return items
else:
return self.get_items_from_class_names(self.safeget(class_names, keys), screens, keys)
elif isCss:
match = re.search('config\(["\']([^\'"]*)$', line, re.IGNORECASE)
if match is None:
return []
keys = match.group(1).split('.')
if len(keys) == 1:
return config_items
else:
subset = self.safeget(config, keys[:-1])
if subset is None or not isinstance(subset, dict):
return []
return self.get_config_items(subset)
else:
return []
def safeget(self, dct, keys):
for key in keys:
try:
dct = dct[key]
except KeyError:
return None
return dct
def find_file(self, dir, names, exclude_dirs = None):
file = None
for root, dirs, files in os.walk(dir):
if exclude_dirs is not None:
dirs[:] = [d for d in dirs if d not in exclude_dirs]
for filename in files:
if filename in names:
file = os.path.join(root, filename)
break
if file is not None:
break
return file
def find_node_module(self, dir, name):
module = None
for root, dirs, files in os.walk(dir):
if 'node_modules' not in root or name not in root:
continue
for filename in files:
if filename != 'package.json':
continue
basename = os.path.split(os.path.join(root, filename))[0]
if basename.endswith(os.path.join('node_modules', name)):
module = basename
break
if module is not None:
break
return module