1
- import sublime , sublime_plugin , colorsys , plistlib , re , os , os .path
1
+ import sublime , sublime_plugin , colorsys , plistlib , re , os , time , os .path
2
2
3
3
class crc8 :
4
4
def __init__ (self ):
@@ -42,65 +42,104 @@ def crc(self, msg):
42
42
runningCRC = self .crcTable [runningCRC ^ c ]
43
43
return runningCRC
44
44
45
- def plugin_loaded ():
46
- sublime .load_settings ("Preferences.sublime-settings" ).add_on_change ('color_scheme' ,maybefixscheme )
47
- pp = sublime .packages_path ()
48
- if not os .path .exists (pp + "/Colorcoder" ):
49
- os .makedirs (pp + "/Colorcoder" )
45
+ hasher = crc8 ()
46
+ scopes = []
50
47
51
- firstrunfile = pp + "/Colorcoder/firstrun"
52
- if not os .path .exists (firstrunfile ):
53
- maybefixscheme ()
54
- open (firstrunfile , 'a' ).close ()
48
+ class colorcoder (sublime_plugin .TextCommand ,sublime_plugin .EventListener ):
55
49
56
- class colorcoder (sublime_plugin .EventListener ):
50
+ def on_new (self ,view ):
51
+ view .settings ().set ('colorcode' ,True )
57
52
58
- hasher = crc8 ();
53
+ def on_activated (self , view ):
54
+ self .on_load (view )
59
55
60
- def on_load_async (self ,view ):
56
+ def on_load (self ,view ):
57
+ view .settings ().set ('colorcode' ,True )
58
+ set = sublime .load_settings ("colorcoder.sublime-settings" )
61
59
if view .file_name ():
62
60
filename = os .path .split (view .file_name ())[1 ]
63
61
dotp = filename .rfind ('.' )
64
62
ext = '' if dotp == - 1 else filename [dotp + 1 :]
65
- set = sublime .load_settings ("colorcoder.sublime-settings" )
66
63
if (set .has ('enabled_for' ) and ext not in set .get ('enabled_for' )) or ext in set .get ('disabled_for' ,[]):
67
64
view .settings ().set ('colorcode' ,False )
68
65
return
69
- else :
70
- pass
66
+
67
+ if view .size () > set .get ('max_size' ,10000 ) and not view .settings ().get ('forcecolorcode' ,False ):
68
+ sublime .status_message ("File is too big, disabling colorcoding as it might hurt perfromance" )
69
+ view .settings ().set ('colorcode' ,False )
70
+ return
71
71
72
72
vcc = view .settings ().get ('color_scheme' )
73
73
if vcc and "Widget" in vcc :
74
74
view .settings ().set ('colorcode' ,False )
75
75
return
76
76
77
+ vcc = view .settings ().get ('syntax' )
78
+ if vcc and ".build-language" in vcc :
79
+ view .settings ().set ('colorcode' ,False )
80
+ return
81
+
77
82
self .on_modified_async (view )
78
83
79
- def on_activated_async (self , view ):
80
- self .on_load_async (view )
84
+ def on_post_save (self ,view ):
85
+ self .on_load (view )
81
86
82
87
def on_modified_async (self , view ):
83
- if not view .settings ().get ('colorcode' ,True ):
84
- return
88
+ view .run_command ("colorcoder" )
89
+
90
+ @staticmethod
91
+ def update_scopes ():
92
+ global scopes
93
+ scopes = sublime .load_settings ("colorcoder.sublime-settings" ).get ('scopes' )
94
+
95
+ def on_post_text_command (self , view , cmd , args ):
96
+ if cmd == "set_file_type" :
97
+ sublime .active_window ().active_view ().run_command ("colorcoder" )
98
+
99
+ def __init__ (self , view = None ):
100
+ self .view = view
101
+
102
+ def run (self , edit ):
103
+ global hasher , scopes
85
104
86
105
regs = {}
87
106
for i in map (hex ,range (256 )):
88
107
regs [i ] = []
89
108
90
- for sel in sublime . load_settings ( "colorcoder.sublime-settings" ). get ( ' scopes' ) :
91
- for r in view .find_by_selector (sel ):
92
- regs [hex (self . hasher .crc (view .substr (r )))].append (r )
109
+ for sel in scopes :
110
+ for r in self . view .find_by_selector (sel ):
111
+ regs [hex (hasher .crc (self . view .substr (r )))].append (r )
93
112
94
113
for key in regs :
95
- view .add_regions ('cc' + key ,regs [key ],'cc' + key ,'' , sublime .DRAW_NO_OUTLINE )
114
+ self . view .add_regions ('cc' + key ,regs [key ],'cc' + key ,'' , sublime .DRAW_NO_OUTLINE )
96
115
97
- def post_text_command (self , win , cmd , args ):
98
- if cmd == "set_file_type" :
99
- self .on_modified_async (sublime .active_window ().active_view ())
116
+ del regs
100
117
101
- class colorcoderdisabler (sublime_plugin .ApplicationCommand ):
118
+ class colorcodertoggler (sublime_plugin .ApplicationCommand ):
102
119
def run (self ):
103
- sublime .active_window ().active_view ().settings ().set ('colorcode' ,False )
120
+ view = sublime .active_window ().active_view ()
121
+ cc = view .settings ().get ('colorcode' ,False )
122
+ view .settings ().set ('colorcode' ,not cc )
123
+ view .settings ().set ('forcecolorcode' ,False )
124
+
125
+ if cc :
126
+ for i in map (hex ,range (256 )):
127
+ view .erase_regions ('cc' + i )
128
+ else :
129
+ if view .size () > set .get ('max_size' ,10000 ):
130
+ view .settings ().set ('forcecolorcode' ,True )
131
+ view .run_command ("colorcoder" )
132
+
133
+ def is_checked (self ):
134
+ return sublime .active_window ().active_view ().settings ().get ('colorcode' ,False )
135
+
136
+ def description (self ):
137
+ if sublime .active_window ().active_view ().size () > sublime .load_settings ("colorcoder.sublime-settings" ).get ('max_size' ,10000 ):
138
+ return "Colorcoding may hurt performance, File is large"
139
+ else :
140
+ return "Colorcode this view"
141
+
142
+ modification_running = False
104
143
105
144
class colorshemeemodifier (sublime_plugin .ApplicationCommand ):
106
145
def run (self ):
@@ -111,7 +150,70 @@ def panel_callback(self, text):
111
150
sublime .load_settings ("colorcoder.sublime-settings" ).set ('lightness' ,l )
112
151
sublime .load_settings ("colorcoder.sublime-settings" ).set ('saturation' ,s )
113
152
sublime .save_settings ("colorcoder.sublime-settings" )
114
- modify_color_scheme (l ,s ,True )
153
+ colorshemeemodifier .modify_color_scheme (l ,s ,True )
154
+
155
+ @staticmethod
156
+ def maybefixscheme ():
157
+ set = sublime .load_settings ("colorcoder.sublime-settings" )
158
+ if set .get ('auto_apply_on_scheme_change' ):
159
+ if sublime .load_settings ("Preferences.sublime-settings" ).get ('color_scheme' ).find ('/Colorcoder/' ) == - 1 :
160
+ colorshemeemodifier .modify_color_scheme (set .get ('lightness' ),set .get ('saturation' ))
161
+
162
+ @staticmethod
163
+ def modify_color_scheme (l ,s ,read_original = False ):
164
+ read_original = read_original and sublime .load_settings ("Preferences.sublime-settings" ).has ("original_color_scheme" )
165
+ if read_original and sublime .load_settings ("Preferences.sublime-settings" ).get ('color_scheme' ).find ('/Colorcoder/' ) == - 1 :
166
+ read_original = False
167
+ global modification_running
168
+ if modification_running :
169
+ return
170
+ modification_running = True
171
+ name = sublime .load_settings ("Preferences.sublime-settings" ).get ("original_color_scheme" ) if read_original else sublime .active_window ().active_view ().settings ().get ('color_scheme' )
172
+ try :
173
+ cs = plistlib .readPlistFromBytes (sublime .load_binary_resource (name ))
174
+
175
+ tokenclr = "#000000"
176
+ for rule in cs ["settings" ]:
177
+ if "scope" not in rule and "name" not in rule :
178
+ bgc = rule ["settings" ]["background" ]
179
+ r = int (bgc [1 :3 ],16 )
180
+ g = int (bgc [3 :5 ],16 )
181
+ b = int (bgc [5 :7 ],16 )
182
+ if b > 0 :
183
+ b = b - 1
184
+ elif g > 0 :
185
+ g = g - 1
186
+ elif r > 0 :
187
+ r = r - 1
188
+ else :
189
+ rule ["settings" ]["background" ] = "#000001"
190
+ tokenclr = "#%02x%02x%02x" % (r ,g ,b )
191
+ break
192
+
193
+ cs ["name" ] = cs ["name" ] + " (Colorcoded)"
194
+
195
+ for x in range (0 ,256 ):
196
+ cs ["settings" ].append (dict (
197
+ scope = "cc0x%x" % x ,
198
+ settings = dict (
199
+ foreground = "#" + '' .join (map (lambda c : "%02x" % int (256 * c ),colorsys .hls_to_rgb (x / 256. , l , s ))),
200
+ background = tokenclr
201
+ )
202
+ ))
203
+
204
+ newname = "/Colorcoder/%s (Colorcoded).tmTheme" % re .search ("/([^/]+).tmTheme$" , name ).group (1 )
205
+
206
+ plistlib .writePlist (cs ,"%s%s" % (sublime .packages_path (),newname ))
207
+
208
+ sublime .load_settings ("Preferences.sublime-settings" ).set ("original_color_scheme" , name )
209
+ sublime .load_settings ("Preferences.sublime-settings" ).set ("color_scheme" ,"Packages%s" % newname )
210
+ sublime .save_settings ("Preferences.sublime-settings" )
211
+ except Exception as e :
212
+ sublime .error_message ("Colorcoder was not able to parse the colorscheme\n Check the console for the actual error message." )
213
+ sublime .active_window ().run_command ("show_panel" , {"panel" : "console" , "toggle" : True })
214
+ print (e )
215
+ finally :
216
+ modification_running = False
115
217
116
218
class colorcoderInspectScope (sublime_plugin .ApplicationCommand ):
117
219
def run (self ):
@@ -120,49 +222,21 @@ def run(self):
120
222
print (view .scope_name (sel .a ))
121
223
sublime .active_window ().run_command ("show_panel" , {"panel" : "console" , "toggle" : True })
122
224
123
- def maybefixscheme ():
124
- set = sublime .load_settings ("colorcoder.sublime-settings" )
125
- if set .get ('auto_apply_on_scheme_change' ):
126
- if sublime .load_settings ("Preferences.sublime-settings" ).get ('color_scheme' ).find ('/Colorcoder/' ) == - 1 :
127
- modify_color_scheme (set .get ('lightness' ),set .get ('saturation' ))
128
-
129
- def modify_color_scheme (l ,s ,read_original = False ):
130
- name = sublime .load_settings ("Preferences.sublime-settings" ).get ("original_color_scheme" ) if read_original else sublime .active_window ().active_view ().settings ().get ('color_scheme' )
131
- cs = plistlib .readPlistFromBytes (bytes (sublime .load_resource (name ),'UTF-8' ))
132
- tokenclr = "#000000"
133
-
134
- for rule in cs ["settings" ]:
135
- if "scope" not in rule and "name" not in rule :
136
- bgc = rule ["settings" ]["background" ]
137
- r = int (bgc [1 :3 ],16 )
138
- g = int (bgc [3 :5 ],16 )
139
- b = int (bgc [5 :7 ],16 )
140
- if b > 0 :
141
- b = b - 1
142
- elif g > 0 :
143
- g = g - 1
144
- elif r > 0 :
145
- r = r - 1
146
- else :
147
- rule ["settings" ]["background" ] = "#000001"
148
- tokenclr = "#%02x%02x%02x" % (r ,g ,b )
149
- break
150
-
151
- cs ["name" ] = cs ["name" ] + " (Colorcode)"
152
-
153
- for x in range (0 ,256 ):
154
- cs ["settings" ].append (dict (
155
- scope = "cc0x%x" % x ,
156
- settings = dict (
157
- foreground = "#" + '' .join (map (lambda c : "%02x" % int (256 * c ),colorsys .hls_to_rgb (x / 256 , l , s ))),
158
- background = tokenclr
159
- )
160
- ))
161
-
162
- newname = "/Colorcoder/%s (Colorcode).tmTheme" % re .search ("/([^/]+).tmTheme$" , name ).group (1 )
163
-
164
- plistlib .writePlist (cs ,"%s%s" % (sublime .packages_path (),newname ))
165
-
166
- sublime .load_settings ("Preferences.sublime-settings" ).set ("original_color_scheme" , name )
167
- sublime .load_settings ("Preferences.sublime-settings" ).set ("color_scheme" ,"Packages%s" % newname )
168
- sublime .save_settings ("Preferences.sublime-settings" )
225
+ def plugin_loaded ():
226
+ sublime .load_settings ("Preferences.sublime-settings" ).add_on_change ('color_scheme' ,colorshemeemodifier .maybefixscheme )
227
+ sublime .load_settings ("colorcoder.sublime-settings" ).add_on_change ('scopes' ,colorcoder .update_scopes )
228
+ colorcoder .update_scopes ()
229
+ pp = sublime .packages_path ()
230
+ if not os .path .exists (pp + "/Colorcoder" ):
231
+ os .makedirs (pp + "/Colorcoder" )
232
+
233
+ firstrunfile = pp + "/Colorcoder/firstrun"
234
+ if not os .path .exists (firstrunfile ):
235
+ colorshemeemodifier .maybefixscheme ()
236
+ open (firstrunfile , 'a' ).close ()
237
+
238
+ for wnd in sublime .windows ():
239
+ for view in wnd .views ():
240
+ view .settings ().set ('colorcode' ,True )
241
+ view .run_command ("colorcoder" )
242
+
0 commit comments