-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathset_setting_command.py
225 lines (200 loc) · 9.95 KB
/
set_setting_command.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
225
"""A ST3 command for setting plugin settings."""
try:
from . import st_helper
from .settings import COLOR_HIGHLIGHTER_SETTINGS_NAME, ColorSchemeColorHighlighterSettings, Settings
except ValueError:
import st_helper
from settings import COLOR_HIGHLIGHTER_SETTINGS_NAME, ColorSchemeColorHighlighterSettings, Settings
if st_helper.running_in_st():
import sublime # pylint: disable=import-error
import sublime_plugin # pylint: disable=import-error
else:
from . import sublime
from . import sublime_plugin
class ColorHighlighterSetSetting(sublime_plugin.ApplicationCommand):
"""
A ST3 command for setting plugin settings.
It supports:
- Regular settings that just get set.
- Boolean settings that get flipped.
- Enum settings that have a special "none" value that gets mapped to flipping a specified boolean setting.
- Computed boolean settings which get automatically recomputed every time it's dependencies are recomputed.
- Disabling some settings or setting prefixes for ST2.
"""
_BOOL_SETTINGS = [
"autoreload.when_settings_change",
"autoreload.when_color_scheme_change",
"search_colors_in.selection.enabled",
"search_colors_in.selection.color_highlighters.color_scheme.enabled",
"search_colors_in.selection.color_highlighters.gutter_icons.enabled",
"search_colors_in.selection.color_highlighters.phantoms.enabled",
"search_colors_in.all_content.enabled",
"search_colors_in.all_content.color_highlighters.color_scheme.enabled",
"search_colors_in.all_content.color_highlighters.gutter_icons.enabled",
"search_colors_in.all_content.color_highlighters.phantoms.enabled",
"search_colors_in.hover.enabled",
"search_colors_in.hover.color_highlighters.color_scheme.enabled",
"search_colors_in.hover.color_highlighters.gutter_icons.enabled",
"search_colors_in.hover.color_highlighters.phantoms.enabled",
"default_keybindings",
"experimental.asynchronosly_update_color_scheme",
]
# A key is an enum setting and a value is the boolean setting that gets disabled when the value setting is set to
# none.
_ENUM_SETTINGS = {
"search_colors_in.selection.color_highlighters.color_scheme.highlight_style":
"search_colors_in.selection.color_highlighters.color_scheme.enabled",
"search_colors_in.selection.color_highlighters.gutter_icons.icon_style":
"search_colors_in.selection.color_highlighters.gutter_icons.enabled",
"search_colors_in.selection.color_highlighters.phantoms.style":
"search_colors_in.selection.color_highlighters.phantoms.enabled",
"search_colors_in.all_content.color_highlighters.color_scheme.highlight_style":
"search_colors_in.all_content.color_highlighters.color_scheme.enabled",
"search_colors_in.all_content.color_highlighters.gutter_icons.icon_style":
"search_colors_in.all_content.color_highlighters.gutter_icons.enabled",
"search_colors_in.all_content.color_highlighters.phantoms.style":
"search_colors_in.all_content.color_highlighters.phantoms.enabled",
"search_colors_in.hover.color_highlighters.color_scheme.highlight_style":
"search_colors_in.hover.color_highlighters.color_scheme.enabled",
"search_colors_in.hover.color_highlighters.gutter_icons.icon_style":
"search_colors_in.hover.color_highlighters.gutter_icons.enabled",
"search_colors_in.hover.color_highlighters.phantoms.style":
"search_colors_in.hover.color_highlighters.phantoms.enabled",
}
# A key is a boolean setting, a value is a list of boolean settings that need to be OR-ed to compute the key
# setting.
_COMPUTED_BOOLEAN_SETTINGS = {
"search_colors_in.selection.enabled": [
"search_colors_in.selection.color_highlighters.color_scheme.enabled",
"search_colors_in.selection.color_highlighters.gutter_icons.enabled",
"search_colors_in.selection.color_highlighters.phantoms.enabled",
],
"search_colors_in.all_content.enabled": [
"search_colors_in.all_content.color_highlighters.color_scheme.enabled",
"search_colors_in.all_content.color_highlighters.gutter_icons.enabled",
"search_colors_in.all_content.color_highlighters.phantoms.enabled",
],
"search_colors_in.hover.enabled": [
"search_colors_in.hover.color_highlighters.color_scheme.enabled",
"search_colors_in.hover.color_highlighters.gutter_icons.enabled",
"search_colors_in.hover.color_highlighters.phantoms.enabled",
]
}
# A key is a setting prefix, a value is either a boolean, in which case the whole prefix is disabled for ST2 or
# a list of values for this setting unsupported in ST2.
_ST2_UNSUPPORTED_SETTINGS = {
"search_colors_in.selection.color_highlighters.color_scheme.highlight_style":
list(set(ColorSchemeColorHighlighterSettings.ST3_VALID_STYLES) -
set(ColorSchemeColorHighlighterSettings.ST2_VALID_STYLES)),
"search_colors_in.all_content.color_highlighters.color_scheme.highlight_style":
list(set(ColorSchemeColorHighlighterSettings.ST3_VALID_STYLES) -
set(ColorSchemeColorHighlighterSettings.ST2_VALID_STYLES)),
"search_colors_in.selection.color_highlighters.gutter_icons": True,
"search_colors_in.selection.color_highlighters.phantoms": True,
"search_colors_in.all_content.color_highlighters.gutter_icons": True,
"search_colors_in.all_content.color_highlighters.phantoms": True,
"search_colors_in.hover": True,
"experimental.asynchronosly_update_color_scheme": True,
}
def run(self, setting, **args):
"""
Run the command.
Arguments:
- setting - the setting name.
- args - other args as a dict.
"""
value = self._get_value(setting, **args)
if self._is_enum_setting(setting):
enable_setting = self._ENUM_SETTINGS[setting]
if value == "none":
_set_setting(enable_setting, False)
self._recompute_settings([enable_setting])
else:
_set_setting(enable_setting, True)
_set_setting(setting, value)
self._recompute_settings([setting, enable_setting])
return
_set_setting(setting, value)
self._recompute_settings([setting])
def is_checked(self, setting, **args):
"""
Return True if the setting is enabled or False otherwise.
Arguments:
- setting - the setting name.
- args - other args as a dict.
"""
if self._is_bool_setting(setting):
return _get_setting(setting)
if self._is_enum_setting(setting):
enable_setting = self._ENUM_SETTINGS[setting]
if not _get_setting(enable_setting):
return args["value"] == "none"
return _get_setting(setting) == args["value"]
return False
def is_visible(self, setting, **args):
"""
Return True if the setting is supported or False otherwise.
Arguments:
- setting - the setting name.
- args - other args as a dict.
"""
if st_helper.is_st3():
return True
support_data = self._get_st2_unsupported_prefix(setting)
if support_data is None:
return True
if isinstance(support_data, bool):
return False
return self._get_value(setting, **args) not in support_data
def _get_st2_unsupported_prefix(self, setting):
for setting_prefix in self._ST2_UNSUPPORTED_SETTINGS:
if setting.startswith(setting_prefix):
return self._ST2_UNSUPPORTED_SETTINGS[setting_prefix]
return None
def _recompute_settings(self, changed_settings):
if not changed_settings:
return
settings_to_recompute = []
for computed_setting in self._COMPUTED_BOOLEAN_SETTINGS:
for setting in self._COMPUTED_BOOLEAN_SETTINGS[computed_setting]:
if setting in changed_settings:
settings_to_recompute.append(computed_setting)
for setting_to_recompute in settings_to_recompute:
ored_value = False
for setting in self._COMPUTED_BOOLEAN_SETTINGS[setting_to_recompute]:
ored_value = ored_value or _get_setting(setting)
_set_setting(setting_to_recompute, ored_value)
self._recompute_settings(settings_to_recompute)
def _get_value(self, setting, **args):
if self._is_bool_setting(setting):
return not _get_setting(setting)
return args["value"]
def _is_bool_setting(self, setting):
return setting in self._BOOL_SETTINGS
def _is_enum_setting(self, setting):
return setting in self._ENUM_SETTINGS
def _get_setting(setting):
setting_path = setting.split(".")
settings = sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)
setting_value = settings.get(setting_path[0])
if len(setting_path) == 1:
return setting_value
for name in setting_path[1:-1]:
setting_value = setting_value[name]
return setting_value[setting_path[-1]]
def _set_setting(setting, value):
settings = sublime.load_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)
if Settings(settings).debug:
print("ColorHighlighter: action=run_command name=color_highlighter_set_setting setting=%s value=%s"
% (setting, value))
setting_path = setting.split(".")
if len(setting_path) == 1:
settings.set(setting_path[0], value)
return
top_level_setting = settings.get(setting_path[0])
setting_value = top_level_setting
for name in setting_path[1:-1]:
setting_value = setting_value[name]
setting_value[setting_path[-1]] = value
settings.set(setting_path[0], top_level_setting)
sublime.save_settings(COLOR_HIGHLIGHTER_SETTINGS_NAME)