1
- import logging
2
- import re
3
- import sys
4
- from collections import OrderedDict
5
-
6
1
import sublime
7
2
import sublime_plugin
8
3
4
+ from .plugin import logger as log
9
5
from .plugin .color_manager import ColorManager
6
+ from .plugin .settings import load_settings , settings , unload_settings
10
7
11
8
NAME = "Colored Comments"
12
- VERSION = "3.0.0 "
9
+ VERSION = "3.0.1 "
13
10
14
- log = logging .Logger
15
11
region_keys = list ()
16
- settings = dict ()
17
- tag_regex = OrderedDict ()
18
- icon = str ()
19
12
color_scheme_manager = ColorManager
20
13
21
- icon_path = "Packages/Colored Comments/icons"
22
- settings_path = "colored_comments.sublime-settings"
23
14
comment_selector = "comment - punctuation.definition.comment"
24
15
25
16
@@ -37,11 +28,6 @@ def on_modified_async(self, view):
37
28
38
29
class ColoredCommentsCommand (sublime_plugin .TextCommand ):
39
30
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
-
45
31
if self .view .match_selector (0 , "text.plain" ):
46
32
return
47
33
@@ -55,23 +41,21 @@ def ClearDecorations(self):
55
41
def ApplyDecorations (self ):
56
42
to_decorate = dict ()
57
43
prev_match = str ()
58
- for region in self .regions :
44
+ for region in self .view . find_by_selector ( comment_selector ) :
59
45
for reg in self .view .split_by_newlines (region ):
60
46
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 (" " ):
64
48
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 (
67
51
line .strip ()
68
52
)
69
53
if not matches :
70
54
if (
71
- settings .get ( " continued_matching" , False )
55
+ settings .continued_matching
72
56
and prev_match
73
57
and line
74
- and line .startswith (continued_matching_pattern )
58
+ and line .startswith (settings . continued_matching_pattern )
75
59
):
76
60
to_decorate .setdefault (prev_match , []).append (reg )
77
61
else :
@@ -82,22 +66,13 @@ def ApplyDecorations(self):
82
66
break
83
67
84
68
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 )
95
70
self .view .add_regions (
96
71
key = key .lower (),
97
72
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 ) ,
101
76
)
102
77
103
78
def _get_tag_flags (self , tag ):
@@ -116,6 +91,7 @@ def _get_tag_flags(self, tag):
116
91
117
92
class ColoredCommentsThemeGeneratorCommand (sublime_plugin .TextCommand ):
118
93
def run (self , edit ):
94
+ color_scheme_manager .tags = settings .tags
119
95
color_scheme_manager .create_user_custom_theme ()
120
96
121
97
@@ -127,46 +103,12 @@ def run(self, edit):
127
103
preferences .get ("color_scheme" ))
128
104
129
105
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 ()
170
112
171
113
172
114
def _generate_region_keys (region_keys , tag_map ):
@@ -175,51 +117,17 @@ def _generate_region_keys(region_keys, tag_map):
175
117
region_keys .append (key .lower ())
176
118
177
119
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
-
210
120
def plugin_loaded ():
211
- global tag_regex , region_keys
212
- global log , icon , color_scheme_manager
121
+ global region_keys
122
+ global color_scheme_manager
213
123
load_settings ()
214
- setup_logging ()
124
+ _generate_region_keys (region_keys , settings .tags )
125
+ log .set_debug_logging (settings .debug )
215
126
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 )
222
127
color_scheme_manager = ColorManager (
223
- tags = settings .get ("tags" , []),
224
- log = log ,
128
+ tags = settings .tags
225
129
)
130
+
131
+
132
+ def plugin_unloaded ():
133
+ unload_settings ()
0 commit comments