1- import logging
2- import re
3- import sys
4- from collections import OrderedDict
5-
61import sublime
72import sublime_plugin
83
4+ from .plugin import logger as log
95from .plugin .color_manager import ColorManager
6+ from .plugin .settings import load_settings , settings , unload_settings
107
118NAME = "Colored Comments"
12- VERSION = "3.0.0 "
9+ VERSION = "3.0.1 "
1310
14- log = logging .Logger
1511region_keys = list ()
16- settings = dict ()
17- tag_regex = OrderedDict ()
18- icon = str ()
1912color_scheme_manager = ColorManager
2013
21- icon_path = "Packages/Colored Comments/icons"
22- settings_path = "colored_comments.sublime-settings"
2314comment_selector = "comment - punctuation.definition.comment"
2415
2516
@@ -37,11 +28,6 @@ def on_modified_async(self, view):
3728
3829class ColoredCommentsCommand (sublime_plugin .TextCommand ):
3930 def run (self , edit ):
40- global settings , tag_regex
41- self .settings = settings
42- self .tag_regex = tag_regex
43- self .regions = self .view .find_by_selector (comment_selector )
44-
4531 if self .view .match_selector (0 , "text.plain" ):
4632 return
4733
@@ -55,23 +41,21 @@ def ClearDecorations(self):
5541 def ApplyDecorations (self ):
5642 to_decorate = dict ()
5743 prev_match = str ()
58- for region in self .regions :
44+ for region in self .view . find_by_selector ( comment_selector ) :
5945 for reg in self .view .split_by_newlines (region ):
6046 line = self .view .substr (reg )
61- continued_matching_pattern = settings .get (
62- "continued_matching_pattern" , "-" )
63- if not continued_matching_pattern .startswith (" " ):
47+ if not settings .continued_matching_pattern .startswith (" " ):
6448 line = line .strip ()
65- for tag_identifier in self .tag_regex :
66- matches = self .tag_regex .get (tag_identifier ).search (
49+ for tag_identifier in settings .tag_regex :
50+ matches = settings .tag_regex .get (tag_identifier ).search (
6751 line .strip ()
6852 )
6953 if not matches :
7054 if (
71- settings .get ( " continued_matching" , False )
55+ settings .continued_matching
7256 and prev_match
7357 and line
74- and line .startswith (continued_matching_pattern )
58+ and line .startswith (settings . continued_matching_pattern )
7559 ):
7660 to_decorate .setdefault (prev_match , []).append (reg )
7761 else :
@@ -82,22 +66,13 @@ def ApplyDecorations(self):
8266 break
8367
8468 for key in to_decorate :
85- sel_tag = self .settings .get ("tags" , []).get (key )
86- flags = self ._get_tag_flags (sel_tag )
87- scope_to_use = ""
88- if sel_tag .get ("scope" ):
89- scope_to_use = sel_tag .get ("scope" )
90- else :
91- scope_to_use = (
92- "colored.comments.color.{}" .format (
93- sel_tag ["color" ]["name" ].replace (" " , "." ).lower ())
94- )
69+ tag = settings .tags .get (key )
9570 self .view .add_regions (
9671 key = key .lower (),
9772 regions = to_decorate .get (key ),
98- scope = scope_to_use ,
99- icon = icon ,
100- flags = flags ,
73+ scope = _get_scope_for_region ( tag ) ,
74+ icon = settings . comment_icon if settings . comment_icon_enabled else "" ,
75+ flags = self . _get_tag_flags ( tag ) ,
10176 )
10277
10378 def _get_tag_flags (self , tag ):
@@ -116,6 +91,7 @@ def _get_tag_flags(self, tag):
11691
11792class ColoredCommentsThemeGeneratorCommand (sublime_plugin .TextCommand ):
11893 def run (self , edit ):
94+ color_scheme_manager .tags = settings .tags
11995 color_scheme_manager .create_user_custom_theme ()
12096
12197
@@ -127,46 +103,12 @@ def run(self, edit):
127103 preferences .get ("color_scheme" ))
128104
129105
130- def escape_regex (pattern ):
131- pattern = re .escape (pattern )
132- for character in "'<>`" :
133- pattern = pattern .replace ("\\ " + character , character )
134- return pattern
135-
136-
137- def _generate_identifier_expression (tags ):
138- unordered_tags = dict ()
139- identifiers = OrderedDict ()
140- for key , value in tags .items ():
141- priority = 2147483647
142- if value .get ("priority" , False ):
143- tag_priority = value .get ("priority" )
144- try :
145- tag_priority = int (priority )
146- priority = tag_priority
147- except ValueError as ex :
148- log .debug (
149- "[Colored Comments]: {} - {}" .format (
150- _generate_identifier_expression .__name__ , ex
151- )
152- )
153- unordered_tags .setdefault (priority , list ()).append (
154- {"name" : key , "settings" : value }
155- )
156- for key in sorted (unordered_tags ):
157- for tag in unordered_tags [key ]:
158- tag_identifier = ["^(" ]
159- tag_identifier .append (
160- tag ["settings" ]["identifier" ]
161- if tag ["settings" ].get ("is_regex" , False )
162- else escape_regex (tag ["settings" ]["identifier" ])
163- )
164- tag_identifier .append (")[ \t ]+(?:.*)" )
165- flag = re .I if tag ["settings" ].get ("ignorecase" , False ) else 0
166- identifiers [tag ["name" ]] = re .compile (
167- "" .join (tag_identifier ), flags = flag
168- )
169- return identifiers
106+ def _get_scope_for_region (tag : dict ) -> str :
107+ if tag .get ("scope" ):
108+ return tag .get ("scope" )
109+ scope_name = "colored.comments.color.{}" .format (
110+ tag .get ("color" ).get ("name" ))
111+ return scope_name .replace (" " , "." ).lower ()
170112
171113
172114def _generate_region_keys (region_keys , tag_map ):
@@ -175,51 +117,17 @@ def _generate_region_keys(region_keys, tag_map):
175117 region_keys .append (key .lower ())
176118
177119
178- def _get_icon ():
179- icon = str ()
180- if settings .get ("comment_icon_enabled" , False ):
181- icon = settings .get ("comment_icon" , "dots" )
182- try :
183- icon = "%s/%s.png" % (icon_path , icon )
184- sublime .load_binary_resource (icon )
185- except OSError as ex :
186- log .debug (
187- "[Colored Comments]: {} - {}" .format (_get_icon .__name__ , ex ))
188- icon = str ()
189- pass
190- return icon
191-
192-
193- def load_settings ():
194- global settings , continued_matching , continued_matching_pattern
195- settings = sublime .load_settings (settings_path )
196- continued_matching = settings .get ("continued_matching" , False )
197- continued_matching_pattern = settings .get (
198- "continued_matching_pattern" , "-" )
199-
200-
201- def setup_logging ():
202- global log
203- log = logging .getLogger ("colored_comments" )
204- out_hdlr = logging .StreamHandler (sys .stdout )
205- out_hdlr .setFormatter (logging .Formatter ("%(asctime)s %(message)s" ))
206- out_hdlr .setLevel (logging .DEBUG )
207- log .addHandler (out_hdlr )
208-
209-
210120def plugin_loaded ():
211- global tag_regex , region_keys
212- global log , icon , color_scheme_manager
121+ global region_keys
122+ global color_scheme_manager
213123 load_settings ()
214- setup_logging ()
124+ _generate_region_keys (region_keys , settings .tags )
125+ log .set_debug_logging (settings .debug )
215126
216- tag_regex = _generate_identifier_expression (settings .get ("tags" , []))
217- _generate_region_keys (region_keys , settings .get ("tags" , []))
218- icon = _get_icon ()
219-
220- if settings .get ("debug" , False ):
221- log .setLevel (logging .DEBUG )
222127 color_scheme_manager = ColorManager (
223- tags = settings .get ("tags" , []),
224- log = log ,
128+ tags = settings .tags
225129 )
130+
131+
132+ def plugin_unloaded ():
133+ unload_settings ()
0 commit comments