-
-
Notifications
You must be signed in to change notification settings - Fork 75
/
Autoprefixer.py
80 lines (66 loc) · 2.35 KB
/
Autoprefixer.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
import sublime
import sublime_plugin
import json
from os.path import dirname, realpath, join, splitext, basename
try:
# Python 2
from node_bridge import node_bridge
except:
from .node_bridge import node_bridge
# Monkeypatch `Region` to be iterable
sublime.Region.totuple = lambda self: (self.a, self.b)
sublime.Region.__iter__ = lambda self: self.totuple().__iter__()
BIN_PATH = join(sublime.packages_path(), dirname(realpath(__file__)), 'autoprefixer.js')
def get_setting(view, key):
settings = view.settings().get('Autoprefixer')
if settings is None:
settings = sublime.load_settings('Autoprefixer.sublime-settings')
return settings.get(key)
def get_syntax(view):
return splitext(basename(view.settings().get('syntax')))[0];
def get_extension(view):
file_name = view.file_name();
return file_name and splitext(file_name)[1];
def is_css(view):
return get_syntax(view) == 'CSS' or get_extension(view) == '.css'
def is_scss(view):
return get_syntax(view) in ('Sass', 'SCSS') or get_extension(view) == '.scss'
class AutoprefixerCommand(sublime_plugin.TextCommand):
def run(self, edit):
if not self.has_selection():
region = sublime.Region(0, self.view.size())
originalBuffer = self.view.substr(region)
prefixed = self.prefix(originalBuffer)
if prefixed:
self.view.replace(edit, region, prefixed)
return
for region in self.view.sel():
if region.empty():
continue
originalBuffer = self.view.substr(region)
prefixed = self.prefix(originalBuffer)
if prefixed:
self.view.replace(edit, region, prefixed)
def prefix(self, data):
try:
return node_bridge(data, BIN_PATH, [json.dumps({
'browsers': get_setting(self.view, 'browsers'),
'cascade': get_setting(self.view, 'cascade'),
'remove': get_setting(self.view, 'remove'),
'supports': get_setting(self.view, 'supports'),
'flexbox': get_setting(self.view, 'flexbox'),
'grid': get_setting(self.view, 'grid'),
'is_css': is_css(self.view)
})])
except Exception as e:
sublime.error_message('Autoprefixer\n%s' % e)
def has_selection(self):
for sel in self.view.sel():
start, end = sel
if start != end:
return True
return False
class AutoprefixerPreSaveCommand(sublime_plugin.EventListener):
def on_pre_save(self, view):
if get_setting(view, 'prefixOnSave') is True and (is_css(view) or is_scss(view)):
view.run_command('autoprefixer')