-
Notifications
You must be signed in to change notification settings - Fork 2
/
declare_that_color.py
73 lines (48 loc) · 1.86 KB
/
declare_that_color.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
import sublime
import sublime_plugin
from .libs import css
SETTINGS_FILE = 'declare_that_color.sublime-settings'
DEFAULT_PREPROCESSOR = None
DEFAULT_SELECTOR = ':root'
DEFAULT_CASE = 'dash'
DEFAULT_PREFIX = ''
libs_css = None
settings = None
def plugin_loaded():
global settings
settings = sublime.load_settings(SETTINGS_FILE)
instantiate_libs_css()
settings.add_on_change('css_selector', instantiate_libs_css)
def instantiate_libs_css():
global libs_css
css_preprocessor = settings.get('css_preprocessor')
name_prefix = settings.get('color_name_prefix')
css_selector = settings.get('css_selector')
type_case = settings.get('type_case')
if not css.Preprocessor.is_supported(css_preprocessor):
css_preprocessor = DEFAULT_PREPROCESSOR
if not isinstance(name_prefix, str):
name_prefix = DEFAULT_PREFIX
if not isinstance(css_selector, str):
css_selector = DEFAULT_SELECTOR
if not all([isinstance(type_case, str),
css.is_supported_type_case(type_case)]):
type_case = DEFAULT_CASE
settings.set('css_preprocessor', css_preprocessor)
settings.set('color_name_prefix', name_prefix)
settings.set('css_selector', css_selector)
settings.set('type_case', type_case)
if settings.get('css_preprocessor') is not None:
libs_css = css.Preprocessor(settings)
return
libs_css = css.Vanilla(settings)
def run_command(edit, view, command_func):
region = sublime.Region(0, view.size())
buffer_ = view.substr(region)
view.replace(edit, region, command_func(buffer_))
class DeclareThatColor(sublime_plugin.TextCommand):
def run(self, edit):
run_command(edit, self.view, libs_css.declare_hexcodes)
class UndeclareThatColor(sublime_plugin.TextCommand):
def run(self, edit):
run_command(edit, self.view, libs_css.undeclare_hexcodes)