This repository has been archived by the owner on Sep 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
switch_settings_core.py
104 lines (67 loc) · 2.71 KB
/
switch_settings_core.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
import sublime, sublime_plugin
class SettingsWrapper():
SETTINGS_FILE_NAME = 'SwitchSettings.sublime-settings'
SS_CURRENT_SETTINGS_NAME = 'current_settings_name'
SS_SETTINGS_NAMES = 'settings_names'
SS_SETTINGS_CONTENTS = 'settings_contents'
def __init__(self):
self._buf = None
self.settings = sublime.load_settings(SettingsWrapper.SETTINGS_FILE_NAME)
self.settings.add_on_change(SettingsWrapper.SS_CURRENT_SETTINGS_NAME, self.on_change)
def __save(self):
import os
if self._buf is None: return False
preferences = os.path.join(sublime.packages_path(), 'User', 'Preferences.sublime-settings')
if not os.path.exists(preferences):
with open(preferences, mode='w') as f:
pass
with open(preferences, mode='r') as f:
preferences_settings = sublime.decode_value(f.read())
contents = self.get_settings_contents()
contents[self._buf] = preferences_settings
self.settings.set(SettingsWrapper.SS_SETTINGS_CONTENTS, contents)
self._buf = None
return True
def __overwrite(self):
import os
cur_name = self.get_current_settings()
contents = self.get_settings_contents()
current_content = contents[cur_name]
preferences = os.path.join(sublime.packages_path(), 'User', 'Preferences.sublime-settings')
with open(preferences, mode='w') as f:
f.write(sublime.encode_value(current_content, True))
return True
def on_change(self):
from . import switch_settings_util as ss_util
if self.__save() and self.__overwrite():
self.save_ss_settings()
ss_util.reboot()
def save_ss_settings(self):
sublime.save_settings(SettingsWrapper.SETTINGS_FILE_NAME)
def set_buffer(self, buf):
self._buf = buf
def get_current_settings(self):
return self.settings.get(SettingsWrapper.SS_CURRENT_SETTINGS_NAME, 'Switch_Settings_Default')
def set_current_settings(self, name):
self.settings.set(SettingsWrapper.SS_CURRENT_SETTINGS_NAME, name)
def get_settings(self):
return self.settings.get(SettingsWrapper.SS_SETTINGS_NAMES, [])
def add_settings(self, name):
names = self.get_settings()
names.append(name)
self.settings.set(SettingsWrapper.SS_SETTINGS_NAMES, names);
def remove_settings(self, name):
names = self.get_settings()
names.remove(name)
self.settings.set(SettingsWrapper.SS_SETTINGS_NAMES, names)
def get_settings_contents(self):
return self.settings.get(SettingsWrapper.SS_SETTINGS_CONTENTS, {})
def add_settings_content(self, name, item):
contents = self.get_settings_contents()
contents[name] = item
self.settings.set(SettingsWrapper.SS_SETTINGS_CONTENTS, contents)
def pop_settings_content(self, name):
contents = self.get_settings_contents()
tmp = contents.pop(name)
self.settings.set(SettingsWrapper.SS_SETTINGS_CONTENTS, contents)
return tmp