forked from facelessuser/BracketHighlighter
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bh_regions.py
531 lines (470 loc) · 19.8 KB
/
bh_regions.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
import sublime
DEFAULT_STYLES = {
"default": {
"icon": "dot",
"color": "brackethighlighter.default",
"style": "underline"
},
"unmatched": {
"icon": "question",
"color": "brackethighlighter.unmatched",
"style": "outline"
}
}
HV_RSVD_VALUES = ["__default__", "__bracket__"]
def underline(regions):
"""
Convert sublime regions into underline regions
"""
r = []
for region in regions:
start = region.begin()
end = region.end()
while start < end:
r.append(sublime.Region(start))
start += 1
return r
def select_bracket_style(option):
"""
Configure style of region based on option
"""
style = sublime.HIDE_ON_MINIMAP
if option == "outline":
style |= sublime.DRAW_NO_FILL
elif option == "none":
style |= sublime.HIDDEN
elif option == "underline":
style |= sublime.DRAW_EMPTY_AS_OVERWRITE
elif option == "thin_underline":
style |= sublime.DRAW_NO_FILL
style |= sublime.DRAW_NO_OUTLINE
style |= sublime.DRAW_SOLID_UNDERLINE
elif option == "squiggly":
style |= sublime.DRAW_NO_FILL
style |= sublime.DRAW_NO_OUTLINE
style |= sublime.DRAW_SQUIGGLY_UNDERLINE
elif option == "stippled":
style |= sublime.DRAW_NO_FILL
style |= sublime.DRAW_NO_OUTLINE
style |= sublime.DRAW_STIPPLED_UNDERLINE
return style
def select_bracket_icons(option, icon_path):
"""
Configure custom gutter icons if they can be located.
"""
icon = ""
small_icon = ""
open_icon = ""
small_open_icon = ""
close_icon = ""
small_close_icon = ""
# Icon exist?
if not option == "none" and not option == "":
try:
pth = "%s/%s.png" % (icon_path, option)
sublime.load_binary_resource(pth)
icon = pth
except:
pass
try:
pth = "%s/%s_small.png" % (icon_path, option)
sublime.load_binary_resource(pth)
small_icon = pth
except:
pass
try:
pth = "%s/%s_open.png" % (icon_path, option)
sublime.load_binary_resource(pth)
open_icon = pth
except:
open_icon = icon
pass
try:
pth = "%s/%s_open_small.png" % (icon_path, option)
sublime.load_binary_resource(pth)
small_open_icon = pth
except:
small_open_icon = small_icon
pass
try:
pth = "%s/%s_close.png" % (icon_path, option)
sublime.load_binary_resource(pth)
close_icon = pth
except:
close_icon = icon
pass
try:
pth = "%s/%s_close_small.png" % (icon_path, option)
sublime.load_binary_resource(pth)
small_close_icon = pth
except:
small_close_icon = small_icon
pass
return icon, small_icon, open_icon, small_open_icon, close_icon, small_close_icon
def get_bracket_regions(settings):
"""
Get styled regions for brackets to use.
"""
styles = settings.get("bracket_styles", DEFAULT_STYLES)
icon_path = "Packages/BracketHighlighter/icons"
# Make sure default and unmatched styles in styles
for key, value in DEFAULT_STYLES.items():
if key not in styles:
styles[key] = value
continue
for k, v in value.items():
if k not in styles[key]:
styles[key][k] = v
# Initialize styles
default_settings = styles["default"]
for k, v in styles.items():
yield k, StyleDefinition(k, v, default_settings, icon_path)
class StyleDefinition(object):
"""
Styling definition.
"""
def __init__(self, name, style, default_highlight, icon_path):
"""
Setup the style object by reading the
passed in dictionary. And other parameters.
"""
self.name = name
self.color = style.get("color", default_highlight["color"])
self.style = select_bracket_style(style.get("style", default_highlight["style"]))
self.underline = self.style & sublime.DRAW_EMPTY_AS_OVERWRITE
self.endpoints = style.get("endpoints", False)
(
self.icon, self.small_icon, self.open_icon,
self.small_open_icon, self.close_icon, self.small_close_icon
) = select_bracket_icons(style.get("icon", default_highlight["icon"]), icon_path)
self.no_icon = ""
self.clear()
def clear(self):
"""
Clear tracked selections
"""
self.selections = []
self.open_selections = []
self.close_selections = []
self.center_selections = []
self.content_selections = []
class BhRegion(object):
def __init__(self, alter_select, count_lines):
settings = sublime.load_settings("bh_core.sublime-settings")
self.count_lines = count_lines
self.hv_style = select_bracket_style(settings.get("high_visibility_style", "outline"))
self.hv_underline = self.hv_style & sublime.DRAW_EMPTY_AS_OVERWRITE
self.hv_color = settings.get("high_visibility_color", HV_RSVD_VALUES[1])
self.no_multi_select_icons = bool(settings.get("no_multi_select_icons", False))
self.bracket_regions = {}
self.alter_select = alter_select
for style, bracket_region in get_bracket_regions(settings):
self.bracket_regions[style] = bracket_region
self.set_show_unmatched()
def get_color(self, bracket_color, high_visibility):
"""
Get color
"""
if high_visibility:
color = self.hv_color
if self.hv_color == HV_RSVD_VALUES[0]:
color = self.bracket_regions["default"].color
elif self.hv_color == HV_RSVD_VALUES[1]:
color = bracket_color
else:
color = bracket_color
return color
def set_show_unmatched(self, language=None):
"""
Determine if show_unmatched should be enabled for the current view
"""
settings = sublime.load_settings("bh_core.sublime-settings")
show_unmatched = bool(settings.get("show_unmatched", True))
exceptions = settings.get("show_unmatched_exceptions", [])
if isinstance(exceptions, list) and language is not None:
for option in exceptions:
if option.lower() == language:
show_unmatched = not show_unmatched
break
self.show_unmatched = show_unmatched
def reset(self, view, num_sels):
"""
Reset
"""
self.chars = 0
self.lines = 0
self.multi_select = num_sels > 1
self.sels = []
self.view = view
for r in self.bracket_regions.values():
r.clear()
def store_sel(self, regions):
"""
Store the current selection to be set at the end.
"""
if self.alter_select:
for region in regions:
self.sels.append(region)
def change_sel(self):
"""
Change the view's selections.
"""
if self.alter_select and len(self.sels) > 0:
if self.multi_select is False:
self.view.show(self.sels[0])
self.view.sel().clear()
self.view.sel().add_all(self.sels)
def save_incomplete_regions(self, left, right, regions):
"""
Store single incomplete brackets for highlighting.
"""
found = left if left is not None else right
bracket = self.bracket_regions["unmatched"]
if bracket.underline:
bracket.selections += underline((found.toregion(),))
else:
bracket.selections += [found.toregion()]
self.store_sel(regions)
def save_regions(self, left, right, regions, style, high_visibility):
"""
Saved (un)matched regions. Perform any special considerations for region formatting.
"""
handled = False
if left is not None and right is not None:
self.save_complete_regions(left, right, regions, style, high_visibility)
handled = True
elif (left is not None or right is not None) and self.show_unmatched:
self.save_incomplete_regions(left, right, regions)
handled = True
return handled
def save_complete_regions(self, left, right, regions, style, high_visibility):
"""
Saved matched regions.
"""
bracket = self.bracket_regions.get(style, self.bracket_regions["default"])
lines = abs(self.view.rowcol(right.begin)[0] - self.view.rowcol(left.end)[0] + 1)
if self.count_lines:
self.chars += abs(right.begin - left.end)
self.lines += lines
if high_visibility:
self.save_high_visibility_regions(left, right, bracket, lines)
elif bracket.endpoints:
self.save_endpoint_regions(left, right, bracket, lines)
elif bracket.underline:
self.save_underline_regions(left, right, bracket, lines)
else:
self.save_normal_regions(left, right, bracket, lines)
if sublime.load_settings("bh_core.sublime-settings").get("content_highlight_bar", False) and lines > 1:
self.save_content_regions(left, right, bracket, lines)
self.store_sel(regions)
def save_content_regions(self, left, right, bracket, lines):
"""
Calculate content bar location and save region(s)
"""
first_line = self.view.rowcol(left.begin)[0]
last_line = first_line + lines - 1
whitespace = (' ', '\t')
bracket_locations = (left.begin, right.begin)
if sublime.load_settings("bh_core.sublime-settings").get("align_content_highlight_bar", False):
start_pt = self.view.text_point(first_line, 0)
end_pt = left.end
tab_size = self.view.settings().get("tab_size", 4)
index = 0
tabs = 0
count = 0
# Calculate column index of where text starts for line
# containing opening bracket
for x in range(start_pt, start_pt + end_pt):
char = self.view.substr(x)
if char == "\t":
# Track all tabs
tabs += 1
elif char != " ":
# Calculate column on first non-whitespace character
remainder = count & tab_size
tab_aligned = int(count / tab_size)
if remainder and tabs:
# Index of first non-whitespace character.
# Account for smaller tabs that are not aligned on
# tab_size boudary.
index = tab_aligned + (tabs * (tab_size - 1)) + tab_size
else:
# Index of first non-whitespace character.
# Spaces and full tabs aligned on tab_size boundaries
index = count + (tabs * (tab_size - 1))
break
count += 1
for x in range(first_line + 1, first_line + lines):
start_pt = self.view.text_point(x, 0)
end_pt = start_pt + index
actual_pt = start_pt - 1
offset = 0
tab_unit = 0
include = True
# Loop through all lines after the first.
# Calculate the true column postion where the bar should
# be drawn. Calculation should account for tabs.
for y in range(start_pt, start_pt + end_pt):
char = self.view.substr(y)
if char == '\x00':
# Exended past the file's end
actual_pt += 1
break
elif char == "\t":
# Tab will expand to the rest of the tab_size.
# Track columns that are consumed by tabs.
offset += tab_size - 1 - tab_unit
tab_unit = tab_size
actual_pt += 1
elif char == " ":
# Normal space.
# Track columns consumed by spaces in relation to tab_size.
actual_pt += 1
tab_unit += 1
elif (actual_pt + 1 + offset) < end_pt:
# Do not draw bar if text comes before bar
include = False
break
if tab_unit == tab_size:
# Roll over tab_unit
tab_unit = 0
if (actual_pt + offset) >= end_pt:
# Reached the target point.
break
if include and (actual_pt - start_pt) + 1 > count and actual_pt < right.begin:
if self.view.rowcol(actual_pt)[0] == x and actual_pt not in bracket_locations:
if x == last_line:
# Draw bar on last line if text comes before bracket
include = False
for y in range(actual_pt, right.begin):
if self.view.substr(y) not in whitespace:
include = True
break
if include:
bracket.content_selections.append(sublime.Region(actual_pt))
else:
# Content line; draw bar
bracket.content_selections.append(sublime.Region(actual_pt))
else:
# Loop through all lines after the first, draw a bar
for x in range(first_line + 1, first_line + lines):
pt = self.view.text_point(x, 0)
if pt not in bracket_locations:
if x == last_line:
# Draw bar on last line if text comes before bracket
include = False
for y in range(pt, right.begin):
if self.view.substr(y) not in whitespace:
include = True
break
if include:
bracket.content_selections.append(sublime.Region(pt))
else:
# Content line; draw bar
bracket.content_selections.append(sublime.Region(pt))
def save_high_visibility_regions(self, left, right, bracket, lines):
"""
Save high visibility regions.
"""
if lines <= 1:
if self.hv_underline:
bracket.selections += underline((sublime.Region(left.begin, right.end),))
else:
bracket.selections += [sublime.Region(left.begin, right.end)]
else:
bracket.open_selections += [sublime.Region(left.begin)]
if self.hv_underline:
bracket.center_selections += underline((sublime.Region(left.begin + 1, right.end - 1),))
else:
bracket.center_selections += [sublime.Region(left.begin, right.end)]
bracket.close_selections += [sublime.Region(right.begin)]
def save_endpoint_regions(self, left, right, bracket, lines):
"""
Save endpoint regions. Underlined and normal.
"""
offset = 0 if bracket.underline else 1
if lines <= 1:
bracket.selections += [
sublime.Region(left.begin, left.begin + offset),
sublime.Region(right.begin, right.begin + offset)
]
if left.size() > 1:
bracket.selections += [sublime.Region(left.end - offset, left.end)]
if right.size() > 1:
bracket.selections += [sublime.Region(right.end - offset, right.end)]
else:
bracket.open_selections += [sublime.Region(left.begin, left.begin + offset)]
bracket.close_selections += [sublime.Region(right.begin, right.begin + offset)]
if left.size() > 1:
bracket.center_selections += [sublime.Region(left.end - offset, left.end)]
if right.size() > 1:
bracket.center_selections += [sublime.Region(right.end - offset, right.end)]
def save_underline_regions(self, left, right, bracket, lines):
"""
Save underlined regions
"""
if lines <= 1:
bracket.selections += underline((left.toregion(), right.toregion()))
else:
bracket.open_selections += [sublime.Region(left.begin)]
bracket.close_selections += [sublime.Region(right.begin)]
if left.size():
bracket.center_selections += underline((sublime.Region(left.begin + 1, left.end),))
if right.size():
bracket.center_selections += underline((sublime.Region(right.begin + 1, right.end),))
def save_normal_regions(self, left, right, bracket, lines):
"""
Save normal regions
"""
if lines <= 1:
bracket.selections += [left.toregion(), right.toregion()]
else:
bracket.open_selections += [left.toregion()]
bracket.close_selections += [right.toregion()]
def highlight_regions(self, name, icon_type, selections, bracket, regions, high_visibility):
"""
Apply the highlightes for the highlight region.
"""
if len(selections):
if selections == "content_selections":
self.view.add_regions(
name,
getattr(bracket, selections) if not high_visibility else [],
self.get_color(bracket.color, False),
getattr(bracket, icon_type),
sublime.DRAW_EMPTY
)
else:
self.view.add_regions(
name,
getattr(bracket, selections),
self.get_color(bracket.color, high_visibility),
getattr(bracket, icon_type),
self.hv_style if high_visibility else bracket.style
)
regions.append(name)
def highlight(self, high_visibility):
"""
Highlight all bracket regions.
"""
self.change_sel()
for region_key in self.view.settings().get("bh_regions", []):
self.view.erase_regions(region_key)
regions = []
icon_type = "no_icon"
open_icon_type = "no_icon"
close_icon_type = "no_icon"
if not self.no_multi_select_icons or not self.multi_select:
icon_type = "small_icon" if self.view.line_height() < 16 else "icon"
open_icon_type = "small_open_icon" if self.view.line_height() < 16 else "open_icon"
close_icon_type = "small_close_icon" if self.view.line_height() < 16 else "close_icon"
for name, r in self.bracket_regions.items():
self.highlight_regions("bh_" + name, icon_type, "selections", r, regions, high_visibility)
self.highlight_regions("bh_" + name + "_center", "no_icon", "center_selections", r, regions, high_visibility)
self.highlight_regions("bh_" + name + "_open", open_icon_type, "open_selections", r, regions, high_visibility)
self.highlight_regions("bh_" + name + "_close", close_icon_type, "close_selections", r, regions, high_visibility)
self.highlight_regions("bh_" + name + "_content", "no_icon", "content_selections", r, regions, high_visibility)
# Track which regions were set in the view so that they can be cleaned up later.
self.view.settings().set("bh_regions", regions)
if self.count_lines:
sublime.status_message('In Block: Lines ' + str(self.lines) + ', Chars ' + str(self.chars))