-
Notifications
You must be signed in to change notification settings - Fork 39
/
settings.py
213 lines (162 loc) · 6.81 KB
/
settings.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
from __future__ import absolute_import, unicode_literals, print_function
import os
import json
from copy import deepcopy
from collections import defaultdict
import sublime
import sublime_plugin
class Settings(object):
"""This class provides global access to and management of plugin settings."""
nested_settings = ()
def __init__(self, name):
"""Initialize a new instance."""
self.name = name
self.settings = {}
self.previous_settings = {}
self.changeset = set()
self.plugin_settings = None
self.edits = defaultdict(list)
def load(self, force=False):
"""Load the plugin settings."""
if force or not self.settings:
self.observe()
self.on_change()
def has_setting(self, setting):
"""Return whether the given setting exists."""
return setting in self.settings
def get(self, setting, default=None):
"""Return a plugin setting, defaulting to default if not found."""
return self.settings.get(setting, default)
def set(self, setting, value, changed=False):
"""
Set a plugin setting to the given value.
Clients of this module should always call this method to set a value
instead of doing settings['foo'] = 'bar'.
If the caller knows for certain that the value has changed,
they should pass changed=True.
"""
self.copy()
self.settings[setting] = value
if changed:
self.changeset.add(setting)
def pop(self, setting, default=None):
"""
Remove a given setting and return default if it is not in self.settings.
Clients of this module should always call this method to pop a value
instead of doing settings.pop('foo').
"""
self.copy()
return self.settings.pop(setting, default)
def copy(self):
"""Save a copy of the plugin settings."""
self.previous_settings = deepcopy(self.settings)
def observe(self, observer=None):
"""Observer changes to the plugin settings."""
self.plugin_settings = sublime.load_settings('{}.sublime-settings'.format(self.name))
self.plugin_settings.clear_on_change(self.name)
self.plugin_settings.add_on_change(self.name, observer or self.on_change)
def merge_user_settings(self, settings):
"""
Return the default settings merged with the user's settings.
If there are any nested settings, those get merged as well.
"""
default = settings.get('default', {})
user = settings.get('user', {})
if user:
for setting_name in self.nested_settings:
default_setting = default.pop(setting_name, {})
user_setting = user.get(setting_name, {})
for name, data in user_setting.items():
if name in default_setting and isinstance(default_setting[name], dict):
default_setting[name].update(data)
else:
default_setting[name] = data
default[setting_name] = default_setting
user.pop(setting_name, None)
default.update(user)
return default
def on_change(self):
"""Update state when the user settings change."""
settings = self.merge_user_settings(self.plugin_settings)
self.settings.clear()
self.settings.update(settings)
self.on_update()
self.changeset.clear()
self.copy()
def on_update(self):
"""To be implemented by the user, when needed."""
pass
def save(self, view=None):
"""
Regenerate and save the user settings.
User settings are updated and merged with the default settings and if
the user settings are currently being edited, the view is also updated.
"""
self.load()
# Fill in default settings
settings = self.settings
settings_filename = '{}.sublime-settings'.format(self.name)
user_settings_path = os.path.join(sublime.packages_path(), 'User', settings_filename)
settings_views = []
if view is None:
# See if any open views are the user prefs
for window in sublime.windows():
for view in window.views():
if view.file_name() == user_settings_path:
settings_views.append(view)
else:
settings_views = [view]
if settings_views:
def replace(edit):
if not view.is_dirty():
j = json.dumps({'user': settings}, indent=4, sort_keys=True)
j = j.replace(' \n', '\n')
view.replace(edit, sublime.Region(0, view.size()), j)
for view in settings_views:
self.edits[view.id()].append(replace)
view.run_command('settings_view_editor', self)
view.run_command('save')
else:
user_settings = sublime.load_settings(settings_filename)
user_settings.set('user', settings)
sublime.save_settings(settings_filename)
def edit(self, vid, edit):
"""Perform an operation on a view with the given edit object."""
callbacks = self.edits.pop(vid, [])
for c in callbacks:
c(edit)
class SettingsViewEditorCommand(sublime_plugin.TextCommand):
"""A plugin command used to generate an edit object for a view."""
def run(self, edit, settings):
"""Run the command."""
settings.edit(self.view.id(), edit)
class SettingTogglerCommandMixin(object):
"""Command that toggles a setting."""
settings = None
def is_visible(self, **args):
"""Return True if the opposite of the setting is True."""
if args.get('checked', False):
return True
if self.settings.has_setting(args['setting']):
setting = self.settings.get(args['setting'], None)
return setting is not None and setting is not args['value']
else:
return args['value'] is not None
def is_checked(self, **args):
"""Return True if the setting should be checked."""
if args.get('checked', False):
setting = self.settings.get(args['setting'], False)
return setting is True
else:
return False
def run(self, **args):
"""Toggle the setting if value is boolean, or remove it if None."""
if 'value' in args:
if args['value'] is None:
self.settings.pop(args['setting'])
else:
self.settings.set(args['setting'], args['value'], changed=True)
else:
setting = self.settings.get(args['setting'], False)
self.settings.set(args['setting'], not setting, changed=True)
self.settings.save()