-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathThemeAutomator.py
96 lines (81 loc) · 3.35 KB
/
ThemeAutomator.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
import sublime, sublime_plugin, os
sublime_settings = None
plugin_settings = None
schemes = None
automate_selection = None
def plugin_loaded():
global sublime_settings, plugin_settings, schemes, automate_selection
sublime_settings = sublime.load_settings('Preferences.sublime-settings')
plugin_settings = sublime.load_settings('ThemeAutomator.sublime-settings')
schemes = plugin_settings.get('schemes')
automate_selection = plugin_settings.get('automate_selection')
if automate_selection is None:
automate_selection = True
def check_empty_color_scheme(view):
if plugin_settings.get('color_scheme') == '':
plugin_settings.set('color_scheme', sublime_settings.get('color_scheme'))
sublime.save_settings('ThemeAutomator.sublime-settings')
def extensions():
extensions = []
if schemes != None:
for scheme in schemes:
extensions.append(scheme.get('extension'))
return extensions
else:
return []
def save_scheme(color_scheme):
sublime_settings.set('color_scheme', color_scheme)
sublime.save_settings('Preferences.sublime-settings')
def theme(ext):
for index, scheme in enumerate(schemes):
if scheme['extension'] == ext:
return scheme.get('color_scheme')
def set_scheme(ext, color_scheme):
for index, scheme in enumerate(schemes):
if scheme['extension'] == ext and schemes[index]['color_scheme'] != color_scheme:
schemes[index]['color_scheme'] = color_scheme
plugin_settings.set('schemes', schemes)
sublime.save_settings('ThemeAutomator.sublime-settings')
def add_scheme(ext, color_scheme):
schemes.append({'color_scheme': color_scheme, 'extension': ext})
plugin_settings.set('schemes', schemes)
sublime.save_settings('ThemeAutomator.sublime-settings')
def set_scheme_for_extension(view):
if view.file_name() is not None:
root,ext = os.path.splitext(view.file_name())
if ext in extensions():
print('SET scheme for extension')
set_scheme(ext, sublime_settings.get('color_scheme'))
else:
print('ADD scheme for extension')
add_scheme(ext, sublime_settings.get('color_scheme'))
class ThemeAutomatorEvents(sublime_plugin.EventListener):
def on_activated(self, view):
check_empty_color_scheme(view)
if view.file_name() is not None:
root,ext = os.path.splitext(view.file_name())
if ext in extensions():
color_scheme = theme(ext)
if sublime_settings.get('color_scheme') != color_scheme and schemes is not None:
save_scheme(color_scheme)
else:
if automate_selection == True:
print('add_scheme 2 scheme for extension')
add_scheme(ext, sublime_settings.get('color_scheme'))
if sublime_settings.get('color_scheme') != plugin_settings.get('color_scheme'):
save_scheme(plugin_settings.get('color_scheme'))
def on_deactivated(self, view):
if automate_selection == True:
set_scheme_for_extension(view)
class SetSchemeForExtensionCommand(sublime_plugin.TextCommand):
def run(self, edit):
set_scheme_for_extension(self.view)
class SetDefaultSchemeAndForExtensionCommand(sublime_plugin.TextCommand):
def run(self, edit):
plugin_settings.set('color_scheme', sublime_settings.get('color_scheme'))
sublime.save_settings('ThemeAutomator.sublime-settings')
set_scheme_for_extension(self.view)
class SetDefaultSchemeCommand(sublime_plugin.TextCommand):
def run(self, edit):
plugin_settings.set('color_scheme', sublime_settings.get('color_scheme'))
sublime.save_settings('ThemeAutomator.sublime-settings')