-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path__init__.py
More file actions
484 lines (427 loc) · 17.8 KB
/
__init__.py
File metadata and controls
484 lines (427 loc) · 17.8 KB
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
"""
REMINDERS:
- When manipulating a page, if you are wiping it,
remember to clear your views registered live_regions with erase_regions
- When using dialogs, remember to reset the LiveView's last_click_time
so it doesn't think the focusing back on the view after you click ok is another click.
"""
import uuid
import time
import functools
import inspect
import sublime
import sublime_plugin
# Storage for all LiveView instances.
# Used to map Views to thier LiveViews
LIVE_VIEWS = {}
def has_live_view(view):
"""
Returns bool value to indicate if the view has associated LiveView.
"""
return view.id() in LIVE_VIEWS
def get_live_view(view, **kwargs):
"""
Will create an instance of LiveView if one doesn't exist.
If you do not want this, you can use "has_live_view" to test first.
"""
if not has_live_view(view):
LiveView(view=view, **kwargs)
return LIVE_VIEWS.get(view.id(), view)
def del_live_view(view, revert=True):
"""
Deletes the LiveView if one exists for this view.
Does nothing if the view does not have a live view.
"""
if has_live_view(view):
live_view = get_live_view(view)
if revert:
live_view.revert_settings()
del LIVE_VIEWS[live_view.id()]
del live_view
class LiveView:
"""
Extend the sublime text View class.
We've added:
- The ability to track the Live Regions.
- Processor to handle click events in the Live View
and invoke the processors of the relevant LiveRegions.
- The ability to say if the Live View is currently clickable or not.
- Basic settings applying functionality.
"""
def __init__(
self,
view=None,
window=None,
flags=0,
syntax="",
name=None,
process=None,
pre_process=None,
post_process=None,
clear_selection=True
):
if view is None and window is None:
window = sublime.active_window()
if view is None:
view = window.new_file(flags, syntax)
self.view = view
if name is not None:
self.set_name(name)
# Used to identify double clicks in a LiveView.
# We ignore clicks within 0.1 seconds of last click
self.last_click_time = 0
# Register of all LiveRegions associated with this LiveView
self.live_regions = {}
# Flag to turn disable clicking in a LiveView
self.clickable = True
# Used to store original view settings when we layer LiveView settings on top.
# These can be used to reset the view settings when the live view is finished.
self.org_view_settings = {}
# By default any caret selection is removed. This includes clicks.
# This stops items getting highlighted by the caret on click.
self.clear_selection = clear_selection
# The click handling method/functions of the LiveView.
# Optional fallback if we don't find a LiveRegion under a click
self.process = self.process if process is None else process
self.pre_process = self.pre_process if pre_process is None else pre_process
self.post_process = self.post_process if post_process is None else post_process
LIVE_VIEWS[view.id()] = self
def live_regions_to_regions(self, method):
"""
Convert any LiveRegions being passed to a sublime.View method with sublime.Regions
"""
@functools.wraps(method)
def inner_live_regions_to_regions(*args, **kwargs):
args = list(args)
for i, arg in enumerate(args):
if isinstance(arg, LiveRegion):
args[i] = sublime.Region(arg.a, arg.b, arg.xpos)
elif isinstance(arg, list) and len([x for x in arg if isinstance(x, LiveRegion)]) == len(arg):
for j, live_region in enumerate(arg):
arg[j] = sublime.Region(live_region.a, live_region.b, live_region.xpos)
for n, v in kwargs.items():
if isinstance(v, LiveRegion):
kwargs[n] = sublime.Region(v.a, v.b, v.xpos)
elif isinstance(v, list) and len([x for x in v if isinstance(x, LiveRegion)]) == len(v):
for j, live_region in enumerate(v):
v[j] = sublime.Region(live_region.a, live_region.b, live_region.xpos)
return method(*args, **kwargs)
return inner_live_regions_to_regions
def __getattr__(self, name):
"""
If LiveView doesn't have the attribute,
see if it's View does and use that instead.
"""
if self.view is not None and hasattr(self.view, name):
attribute = getattr(self.view, name)
if callable(attribute):
attribute = self.live_regions_to_regions(attribute)
return attribute
raise AttributeError('\'LiveView\' object has no attribute \'%s\'' % name)
def apply_settings(self, settings=None, use_defaults=True, read_only=None, scratch=None):
"""
Apply preset and supplied settings to the View.
This also records the original state if you want to revert it later.
"""
if settings is None:
settings = {}
if use_defaults:
defaults = {
'rulers': [],
'highlight_line': False,
'fade_fold_buttons': True,
'caret_style': 'solid',
'line_numbers': False,
'draw_white_space': 'none',
'gutter': False,
'word_wrap': False,
'indent_guide_options': []
}
defaults.update(settings)
settings = defaults
if read_only is not None:
self.org_view_settings['read_only'] = self.is_read_only()
self.set_read_only(read_only)
if scratch is not None:
self.org_view_settings['scratch'] = self.is_scratch()
self.set_scratch(scratch)
view_settings = self.settings()
for name, value in settings.items():
if not name in self.org_view_settings:
self.org_view_settings[name] = view_settings.get(name)
view_settings.set(name, value)
def revert_settings(self):
"""
If you have layered a LiveView on top of a View.
You can use this to reset the View's settings if you called
"apply_settings" to replace them when you created the LiveView.
"""
read_only = self.org_view_settings.pop('read_only', None)
if read_only is not None:
self.set_read_only(read_only)
scratch = self.org_view_settings.pop('scratch', None)
if scratch is not None:
self.set_scratch(scratch)
view_settings = self.settings()
for name, value in self.org_view_settings.items():
view_settings.set(name, value)
def add_regions(self, key, regions, scope="", icon="", flags=0):
"""
The same as sublime.View's add_regions method,
except we also record any LiveRegions being added
so we can reference them later
"""
sublime_regions = []
for region in regions:
if isinstance(region, LiveRegion):
if region._key is not None:
self.view.erase_regions(region._key)
region.key = key
region._key = '%s--%s--%s' % (key, uuid.uuid4(), region.id())
region.live_view = self
self.live_regions.setdefault(key, []).append(region)
self.view.add_regions(region._key, [region], scope, icon, flags)
else:
sublime_regions.append(region)
if sublime_regions:
self.view.add_regions(key, sublime_regions, scope, icon, flags)
def get_regions(self, key):
"""
The same as sublime.View's get_regions method,
except we also repace any Regions with
their appropriate LiveRegion counterparts.
"""
regions = self.view.get_regions(key)
for live_region in self.live_regions.get(key, []):
tmp_regions = self.view.get_regions(live_region._key)
if tmp_regions:
live_region.region.a = tmp_regions[0].begin()
live_region.region.b = tmp_regions[0].end()
regions.append(live_region)
return regions
def erase_regions(self, key):
"""
The same as sublime.View's erase_regions method,
except we also remove any registered LiveRegions.
"""
if key in self.live_regions:
for live_region in self.live_regions[key]:
if live_region._key is not None:
self.view.erase_regions(live_region._key)
live_region.key = live_region._key = None
del self.live_regions[key]
self.view.erase_regions(key)
def get_live_region(self, method, *args, **kwargs):
"""
Helper method to call sublime.View methods
and convert the returned sublime.Region to a LiveRegion
NOTE: You can of course call the methods directly
and create a LiveRegion with the returned sublime.Region
"""
method = getattr(self, method) if isinstance(method, str) else method
method_args = inspect.getfullargspec(method)[0]
method_kwargs = dict([(n, v) for n, v in kwargs.items() if n in method_args])
LiveRegionClass = kwargs.pop('LiveRegionClass', LiveRegion)
live_region_kwargs = dict([(n, v) for n, v in kwargs.items() if n not in method_args])
region = method(*args, **method_kwargs)
return LiveRegionClass(region.a, region.b, region.xpos, **live_region_kwargs)
def get_live_regions(self, method, *args, **kwargs):
"""
Helper method to call sublime.View methods
and convert the returned list of sublime.Regions to LiveRegions
NOTE: You can of course call the methods directly
and create LiveRegions with the returned sublime.Regions
"""
method = getattr(self, method) if isinstance(method, str) else method
method_args = inspect.getfullargspec(method)[0]
method_kwargs = dict([(n, v) for n, v in kwargs.items() if n in method_args])
LiveRegionClass = kwargs.pop('LiveRegionClass', LiveRegion)
live_region_kwargs = dict([(n, v) for n, v in kwargs.items() if n not in method_args])
regions = method(*args, **method_kwargs)
for i, region in enumerate(regions):
regions[i] = LiveRegionClass(region.a, region.b, region.xpos, **live_region_kwargs)
return regions
def clicked(self):
"""
Handle a click event in a LiveView.
Checks if the click point was inside a LiveRegion.
If it was, we call that LiveRegion's proccessing functionality.
"""
if self.clickable:
event_time = time.time()
regions = self.sel()
# Is this a double click?
if len(regions) == 1 and\
regions[0].empty() and\
event_time - self.last_click_time > 0.1:
self.last_click_time = event_time
point = regions[0].begin()
found = False
for live_regions in self.live_regions.values():
for live_region in live_regions:
if live_region._key is None:
raise LiveError('Registered LiveRegion with no _key.')
regions = self.view.get_regions(live_region._key)
if not regions:
raise LiveError('Could not find Region for LiveRegion key.')
elif len(regions) > 1:
raise LiveError('LiveRegion key returned more than one Region.')
region = regions[0]
if region.contains(point) and not point == region.end():
live_region.a = region.begin()
live_region.b = region.end()
if live_region.clickable:
if hasattr(live_region, 'pre_process'):
live_region.pre_process(live_region)
if hasattr(live_region, 'process'):
live_region.process(live_region)
if hasattr(live_region, 'post_process'):
live_region.post_process(live_region)
found = True
break
if found:
break
else:
if hasattr(self, 'pre_process'):
self.pre_process(self)
if hasattr(self, 'process'):
self.process(self)
if hasattr(self, 'post_process'):
self.post_process(self)
# Should we make this optional?
# Good for non editable LiveViews but may not be for others.
if self.clear_selection:
self.sel().clear()
def pre_process(self, live_view):
pass
def process(self, live_view):
pass
def post_process(self, live_view):
pass
class LiveRegion:
"""
LiveRegions are sublime regions that are remembered by a LiveView.
When a point on a LiveView is clicked it will see if any of it's
LiveRegions span that point and if there is,
it will either call the processing functionality of the LiveRegion
or of it's LiveGroup if it belongs to one.
We can also dictate if a LiveRegion is currently clickable.
"""
def __init__(
self,
a=None,
b=None,
xpos=-1,
process=None,
pre_process=None,
post_process=None,
clickable=True
):
self.region = None
if a is not None:
self.set_region(a, b, xpos)
# The key the LiveRegion was added under.
self.key = None
# The actual unique key we added the LiveRegion under.
self._key = None
# The LiveView this LiveRegion was added to
self.live_view = None
# Flag to turn disable clicking in a LiveRegion
self.clickable = clickable
# The click handling method/functions of the LiveRegion.
self.process = self.process if process is None else process
self.pre_process = self.pre_process if pre_process is None else pre_process
self.post_process = self.post_process if post_process is None else post_process
def set_region(self, a, b=None, xpos=-1):
if isinstance(a, sublime.Region):
self.region = a
else:
if isinstance(self.region, sublime.Region):
self.region.a = a
self.region.b = b
self.region.xpos = xpos
else:
r = sublime.Region(a, b, xpos)
self.region = r
return self.region
def __str__(self):
return self.region.__str__()
def __getattr__(self, name):
"""
If LiveRegio doesn't have the attribute,
see if it's Region does and use that instead.
"""
if self.region is not None and hasattr(self.region, name):
return getattr(self.region, name)
raise AttributeError('LiveRegion object has no attribute "%s"' % name)
@property
def a(self):
return self.region.a
@a.setter
def a(self, value):
self.region.a = value
@property
def b(self):
return self.region.b
@b.setter
def b(self, value):
self.region.b = value
def id(self):
return '%s-%s' % (self.__class__.__name__, id(self))
def update(self):
"""
Reset the LiveRegions a and b values in case the Region has moved in the View.
"""
if self._key is not None:
regions = self.live_view.get_regions(self._key)
if not regions:
raise LiveError('LiveRegion had a key but no region in view.')
if len(regions) > 1:
raise LiveError('LiveRegion had a key that returned more than one region from the view.')
region = regions[0]
self.region.a = region.begin()
self.region.b = region.end()
return self
def process(self, live_region):
print('Clicked Live Region: %s : %d\n"""%s"""' % (
str(live_region),
live_region.live_view.sel()[0].begin(),
live_region.live_view.substr(live_region)
)
)
def pre_process(self, live_region):
pass
def post_process(self, live_region):
pass
class UpdateLiveViewCommand(sublime_plugin.TextCommand):
"""
Generic Text Command to insert, erase or replace data in a view.
"""
def run(self, edit, data=None, start=0, end=None):
was_read_only = self.view.is_read_only()
if was_read_only:
self.view.set_read_only(False)
if end is not None and not start == end:
if data is not None:
self.view.replace(edit, sublime.Region(start, end), data)
else:
self.view.erase(edit, sublime.Region(start, end))
elif data is not None:
self.view.insert(edit, start, data)
if was_read_only:
self.view.set_read_only(True)
class LiveEventListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
"""
See if the clicked view is a LiveView.
If it is, the view will decide what to do.
"""
if has_live_view(view):
get_live_view(view).clicked()
def on_close(self, view):
del_live_view(view)
class LiveError(Exception):
def __init__(self, value):
self.value = value
def __str__(self):
return repr(self.value)