Skip to content

Commit 8c4c74b

Browse files
committed
Merge remote-tracking branch 'origin/master'
2 parents 1e4c318 + bdb9d17 commit 8c4c74b

File tree

6 files changed

+242
-124
lines changed

6 files changed

+242
-124
lines changed

colored_comments.py

+29-121
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,16 @@
1-
import logging
2-
import re
3-
import sys
4-
from collections import OrderedDict
5-
61
import sublime
72
import sublime_plugin
83

4+
from .plugin import logger as log
95
from .plugin.color_manager import ColorManager
6+
from .plugin.settings import load_settings, settings, unload_settings
107

118
NAME = "Colored Comments"
12-
VERSION = "3.0.0"
9+
VERSION = "3.0.1"
1310

14-
log = logging.Logger
1511
region_keys = list()
16-
settings = dict()
17-
tag_regex = OrderedDict()
18-
icon = str()
1912
color_scheme_manager = ColorManager
2013

21-
icon_path = "Packages/Colored Comments/icons"
22-
settings_path = "colored_comments.sublime-settings"
2314
comment_selector = "comment - punctuation.definition.comment"
2415

2516

@@ -37,11 +28,6 @@ def on_modified_async(self, view):
3728

3829
class 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

11792
class 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

172114
def _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-
210120
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
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()

messages.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{
22
"install": "messages/install.txt",
3-
"1.0.1": "messages/3.0.0.txt",
3+
"3.0.0": "messages/3.0.0.txt",
4+
"3.0.1": "messages/3.0.1.txt",
45
}

messages/3.0.1.txt

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Version 3.0.1 (May 14, 2020)
2+
----------------------------
3+
4+
* Enable hot reload of settings

plugin/color_manager.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@
22

33
import sublime
44

5+
56
sublime_settings = "Preferences.sublime-settings"
67
override_path = "Colored Comments Override"
78
scope_name = "colored.comments.color."
89

910

1011
class ColorManager:
11-
def __init__(self, tags, log):
12+
def __init__(self, tags):
1213
self.tags = tags
13-
self.log = log
1414

1515
def remove_override(self, scheme):
1616
self.save_scheme(os.path.basename(scheme), {"rules": [], "variables": {}})

plugin/logger.py

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
log_debug = False
2+
log_exceptions = True
3+
4+
5+
def set_debug_logging(logging_enabled: bool) -> None:
6+
global log_debug
7+
log_debug = logging_enabled
8+
9+
10+
def debug(msg: str) -> None:
11+
if log_debug:
12+
printf(msg)
13+
14+
15+
def printf(msg: str, prefix: str = 'Colored Comments') -> None:
16+
print("{}:{}".format(prefix, msg))

0 commit comments

Comments
 (0)