forked from Kronuz/TextMarker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
TextMarker.py
185 lines (143 loc) · 6.44 KB
/
TextMarker.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
from __future__ import absolute_import
import re
import sublime
import sublime_plugin
from itertools import chain
from .settings import Settings, SettingTogglerCommandMixin
from .colorizer import SchemaColorizer
NAME = "Text Marker"
VERSION = "1.2.3"
DEFAULT_COLORS = ['comment']
def regex_escape(string):
# Sublime Text chokes when regexes contain \', \<, \>, or \`.
# Call re.escape() to escape everything, and then unescape these four.
string = re.escape(string)
for c in "'<>`":
string = string.replace('\\' + c, c)
return string
def is_whitespace(string):
return (not string or string.isspace())
def highlight(view, color=None, min_length=4, when_selection_is_empty=False, when_whitespace=False, add_selections=False, prefix='wh_'):
view_settings = view.settings()
word_separators = view_settings.get('word_separators')
all_regions = {}
for color_scope_name in chain(colorizer.colors.values(), DEFAULT_COLORS):
regions = view.get_regions(prefix + color_scope_name)
if regions:
all_regions[color_scope_name] = regions
def find_color(sel):
for color_scope_name, regions in all_regions.items():
for region in regions:
if region.contains(sel):
return color_scope_name
colors = set()
view_sel = view.sel()
regions = []
for sel in view_sel:
# Figure out what colors are currently active in the selection
color_scope_name = find_color(sel)
if color_scope_name:
colors.add(color_scope_name)
if sel:
# If the selection is a range...
string = view.substr(sel)
if len(string) >= min_length and (when_whitespace or not is_whitespace(string)):
# If we directly compare sel and view.word(sel), then in compares their
# a and b values rather than their begin() and end() values. This means
# that a leftward selection (with a > b) will never match the view.word()
# of itself. As a workaround, we compare the lengths instead.
if len(sel) == len(view.word(sel)):
regex = r'\b%s\b' % regex_escape(string)
else:
regex = regex_escape(string)
regions.extend(view.find_all(regex))
else:
# If selection is a point...
if when_selection_is_empty:
string = view.substr(view.word(sel))
if string and any(c not in word_separators for c in string):
regions.extend(view.find_all(r'\b%s\b' % regex_escape(string)))
if not regions and len(view_sel) > 1:
regions = list(view_sel)
if regions:
sublime.status_message("%d region%s selected" % (len(regions), "" if len(regions) == 1 else "s"))
else:
sublime.status_message("")
if prefix == 'wh_' and colors:
for color_scope_name in colors:
view.erase_regions(prefix + color_scope_name)
else:
colorizer.setup_color_scheme(view_settings)
if not color:
for c in settings.get('default_colors') or DEFAULT_COLORS:
csn = colorizer.add_color(c)
if csn and csn not in all_regions:
color = c
break
color_scope_name = colorizer.add_color(color) or 'comment'
colorizer.update(view)
view.add_regions(prefix + color_scope_name, regions, color_scope_name, '', (sublime.DRAW_OUTLINED if settings.get('draw_outlined') else 0))
if add_selections:
view_sel.add_all(regions)
def erase_colors(view=None, prefix='wh_'):
if view:
for color_scope_name in chain(colorizer.colors.values(), ['comment']):
view.erase_regions(prefix + color_scope_name)
else:
for window in sublime.windows():
for view in window.views():
erase_colors(view)
class TextMarkerListener(sublime_plugin.EventListener):
live = False
def on_selection_modified(self, view):
if settings.get('live'):
self.live = True
color = settings.get('live_color') or 'comment'
min_length = settings.get('min_length')
when_selection_is_empty = settings.get('when_selection_is_empty')
when_whitespace = settings.get('when_whitespace')
highlight(view, color=color, min_length=min_length, when_selection_is_empty=when_selection_is_empty, when_whitespace=when_whitespace, prefix='whl_')
elif self.live:
erase_colors(view, prefix='whl_')
self.live = False
# command to restore color scheme
class TextMarkerRestoreCommand(sublime_plugin.ApplicationCommand):
def run(self):
erase_colors()
colorizer.restore_color_scheme()
class TextMarkerClearCommand(sublime_plugin.TextCommand):
def run(self, edit):
erase_colors(self.view)
class TextMarkerResetCommand(sublime_plugin.TextCommand):
def run(self, edit):
erase_colors()
colorizer.setup_color_scheme(self.view.settings())
class TextMarkerCommand(sublime_plugin.TextCommand):
def run(self, edit, color=None):
if color == "<select>":
highlight(self.view, min_length=4, when_selection_is_empty=True, when_whitespace=True, add_selections=True)
elif color == "<input>":
self.view.window().show_input_panel("Color:", "", self.on_done, None, None)
else:
highlight(self.view, color=color, min_length=4, when_selection_is_empty=True, when_whitespace=True)
def on_done(self, color):
if color:
highlight(self.view, color=color, min_length=4, when_selection_is_empty=True, when_whitespace=True)
################################################################################
# Initialize settings and main objects only once
class TextMarkerSettings(Settings):
pass
settings = TextMarkerSettings(NAME)
class TextMarkerToggleSettingCommand(SettingTogglerCommandMixin, sublime_plugin.WindowCommand):
settings = settings
if 'colorizer' not in globals():
colorizer = SchemaColorizer()
################################################################################
def plugin_loaded():
settings.load()
# ST3 features a plugin_loaded hook which is called when ST's API is ready.
#
# We must therefore call our init callback manually on ST2. It must be the last
# thing in this plugin (thanks, beloved contributors!).
if int(sublime.version()) < 3000:
plugin_loaded()