forked from wuub/SublimeREPL
-
Notifications
You must be signed in to change notification settings - Fork 1
/
sublimerepl.py
688 lines (550 loc) · 20.8 KB
/
sublimerepl.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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
# -*- coding: utf-8 -*-
# Copyright (c) 2011, Wojciech Bederski (wuub.net)
# All rights reserved.
# See LICENSE.txt for details.
import threading
import Queue
import sublime
import sublime_plugin
import repls
import sys
import os
import os.path
import buzhug
import re
import sublimerepl_build_system_hack
PLATFORM = sublime.platform().lower()
SETTINGS_FILE = 'SublimeREPL.sublime-settings'
class Event:
def __init__(self):
self.handlers = set()
def handle(self, handler):
self.handlers.add(handler)
return self
def unhandle(self, handler):
try:
self.handlers.remove(handler)
except:
raise ValueError("Handler is not handling this event, so cannot unhandle it.")
return self
def fire(self, *args, **kargs):
for handler in self.handlers:
handler(*args, **kargs)
def handlers_count(self):
return len(self.handlers)
__iadd__ = handle
__isub__ = unhandle
__call__ = fire
__len__ = handlers_count
class ReplReader(threading.Thread):
def __init__(self, repl):
super(ReplReader, self).__init__()
self.repl = repl
self.daemon = True
self.queue = Queue.Queue()
def run(self):
r = self.repl
q = self.queue
while True:
result = r.read()
q.put(result)
if result is None:
break
class HistoryMatchList(object):
def __init__(self, command_prefix, commands):
self._command_prefix = command_prefix
self._commands = commands
self._cur = len(commands) # no '-1' on purpose
def current_command(self):
if not self._commands:
return ""
return self._commands[self._cur]
def prev_command(self):
self._cur = max(0, self._cur - 1)
return self.current_command()
def next_command(self):
self._cur = min(len(self._commands) - 1, self._cur + 1)
return self.current_command()
class History(object):
def __init__(self):
self._last = None
def push(self, command):
cmd = command.rstrip()
if not cmd or cmd == self._last:
return
self.append(cmd)
self._last = cmd
def append(self, cmd):
raise NotImplementedError()
def match(self, command_prefix):
raise NotImplementedError()
class MemHistory(History):
def __init__(self):
super(MemHistory, self).__init__()
self._stack = []
def append(self, cmd):
self._stack.append(cmd)
def match(self, command_prefix):
matching_commands = []
for cmd in self._stack:
if cmd.startswith(command_prefix):
matching_commands.append(cmd)
return HistoryMatchList(command_prefix, matching_commands)
class PersistentHistory(History):
def __init__(self, external_id):
import datetime
super(PersistentHistory, self).__init__()
path = os.path.join(sublime.packages_path(), "User", "SublimeREPLHistory")
self._db = buzhug.TS_Base(path)
self._external_id = external_id
self._db.create(("external_id", unicode), ("command", unicode), ("ts", datetime.datetime), mode="open")
def append(self, cmd):
from datetime import datetime
self._db.insert(external_id=self._external_id, command=cmd, ts=datetime.now())
def match(self, command_prefix):
pattern = re.compile("^" + re.escape(command_prefix) + ".*")
retults = self._db.select(None, 'external_id==eid and p.match(command)', eid=self._external_id, p=pattern)
retults.sort_by("+ts")
return HistoryMatchList(command_prefix, [x.command for x in retults])
class ReplView(object):
def __init__(self, view, repl, syntax):
self.repl = repl
self._view = view
self._window = view.window()
self.closed = Event()
if syntax:
view.set_syntax_file(syntax)
self._output_end = view.size()
self._repl_reader = ReplReader(repl)
self._repl_reader.start()
settings = sublime.load_settings(SETTINGS_FILE)
view.settings().set("repl_external_id", repl.external_id)
view.settings().set("repl_id", repl.id)
view.settings().set("repl", True)
rv_settings = settings.get("repl_view_settings", {})
for setting, value in rv_settings.items():
view.settings().set(setting, value)
view.settings().set("history_arrows", settings.get("history_arrows", True))
# for hysterical rasins ;)
persistent_history_enabled = settings.get("persistent_history_enabled") or settings.get("presistent_history_enabled")
if self.external_id and persistent_history_enabled:
self._history = PersistentHistory(self.external_id)
else:
self._history = MemHistory()
self._history_match = None
self._filter_color_codes = settings.get("filter_ascii_color_codes")
# begin refreshing attached view
self.update_view_loop()
@property
def external_id(self):
return self.repl.external_id
def on_backspace(self):
if self.delta < 0:
self._view.run_command("left_delete")
def on_ctrl_backspace(self):
if self.delta < 0:
self._view.run_command("delete_word", {"forward": False, "sub_words": True})
def on_super_backspace(self):
if self.delta < 0:
for i in range(abs(self.delta)):
self._view.run_command("left_delete") # Hack to delete to BOL
def on_left(self):
if self.delta != 0:
self._window.run_command("move", {"by": "characters", "forward": False, "extend": False})
def on_shift_left(self):
if self.delta != 0:
self._window.run_command("move", {"by": "characters", "forward": False, "extend": True})
def on_home(self):
if self.delta > 0:
self._window.run_command("move_to", {"to": "bol", "extend": False})
else:
for i in range(abs(self.delta)):
self._window.run_command("move", {"by": "characters", "forward": False, "extend": False})
def on_shift_home(self):
if self.delta > 0:
self._window.run_command("move_to", {"to": "bol", "extend": True})
else:
for i in range(abs(self.delta)):
self._window.run_command("move", {"by": "characters", "forward": False, "extend": True})
def on_selection_modified(self):
self._view.set_read_only(self.delta > 0)
def on_close(self):
self.repl.close()
self.closed(self.repl.id)
def clear(self, edit):
self.escape(edit)
self._view.erase(edit, self.output_region)
self._output_end = self._view.sel()[0].begin()
def escape(self, edit):
self._view.set_read_only(False)
self._view.erase(edit, self.input_region)
self._view.show(self.input_region)
def enter(self):
v = self._view
if v.sel()[0].begin() != v.size():
v.sel().clear()
v.sel().add(sublime.Region(v.size()))
self.push_history(self.user_input) # don't include cmd_postfix in history
v.run_command("insert", {"characters": self.repl.cmd_postfix})
command = self.user_input
self.adjust_end()
self.repl.write(command)
def previous_command(self, edit):
self._view.set_read_only(False)
self.ensure_history_match()
self.replace_current_input(edit, self._history_match.prev_command())
self._view.show(self.input_region)
def next_command(self, edit):
self._view.set_read_only(False)
self.ensure_history_match()
self.replace_current_input(edit, self._history_match.next_command())
self._view.show(self.input_region)
def update_view(self, view):
"""If projects were switched, a view could be a new instance"""
if self._view is not view:
self._view = view
def adjust_end(self):
if self.repl.suppress_echo:
v = self._view
vsize = v.size()
self._output_end = min(vsize, self._output_end)
edit = v.begin_edit()
v.erase(edit, sublime.Region(self._output_end, vsize))
v.end_edit(edit)
else:
self._output_end = self._view.size()
def write(self, unistr):
"""Writes output from Repl into this view."""
# remove color codes
if self._filter_color_codes:
unistr = re.sub(r'\033\[\d*(;\d*)?\w', '', unistr)
unistr = re.sub(r'.\x08', '', unistr)
# string is assumet to be already correctly encoded
v = self._view
edit = v.begin_edit()
try:
v.insert(edit, self._output_end, unistr)
self._output_end += len(unistr)
finally:
v.end_edit(edit)
v.show(self.input_region)
def append_input_text(self, text, edit=None):
e = edit
if not edit:
e = self._view.begin_edit()
self._view.insert(e, self._view.size(), text)
if not edit:
self._view.end_edit(e)
def new_output(self):
"""Returns new data from Repl and bool indicating if Repl is still
working"""
q = self._repl_reader.queue
data = ""
try:
while True:
packet = q.get_nowait()
if packet is None:
return data, False
data += packet
except Queue.Empty:
return data, True
def update_view_loop(self):
(data, is_still_working) = self.new_output()
if data:
self.write(data)
if is_still_working:
sublime.set_timeout(self.update_view_loop, 100)
else:
self.write("\n***Repl Killed***\n""" if self.repl._killed else "\n***Repl Closed***\n""")
self._view.set_read_only(True)
if sublime.load_settings(SETTINGS_FILE).get("view_auto_close"):
window = self._view.window()
window.focus_view(self._view)
window.run_command("close")
def push_history(self, command):
self._history.push(command)
self._history_match = None
def ensure_history_match(self):
user_input = self.user_input
if self._history_match is not None:
if user_input != self._history_match.current_command():
# user did something! reset
self._history_match = None
if self._history_match is None:
self._history_match = self._history.match(user_input)
def replace_current_input(self, edit, cmd):
if cmd:
self._view.replace(edit, self.input_region, cmd)
self._view.sel().clear()
self._view.sel().add(sublime.Region(self._view.size()))
def run(self, edit, code):
self.replace_current_input(edit, code)
self.enter()
self._view.show(self.input_region)
self._window.focus_view(self._view)
@property
def input_region(self):
return sublime.Region(self._output_end, self._view.size())
@property
def output_region(self):
return sublime.Region(0, self._output_end - 2)
@property
def user_input(self):
"""Returns text entered by the user"""
return self._view.substr(self.input_region)
@property
def delta(self):
"""Return a repl_view and number of characters from current selection
to then begging of user_input (otherwise known as _output_end)"""
return self._output_end - self._view.sel()[0].begin()
class ReplManager(object):
def __init__(self):
self.repl_views = {}
def repl_view(self, view):
repl_id = view.settings().get("repl_id")
if repl_id not in self.repl_views:
return None
rv = self.repl_views[repl_id]
rv.update_view(view)
return rv
def find_repl(self, external_id):
for rv in self.repl_views.values():
if rv.external_id == external_id:
return rv
return None
def find_repl_by_syntax(self, syntax):
for rv in self.repl_views.values():
print rv._view.settings().get('syntax')
if rv._view.settings().get('syntax') == syntax:
return rv
return None
def open(self, window, encoding, type, syntax=None, view_id=None, **kwds):
try:
kwds = ReplManager.translate(window, kwds)
encoding = ReplManager.translate(window, encoding)
r = repls.Repl.subclass(type)(encoding, **kwds)
found = None
for view in window.views():
if view.id() == view_id:
found = view
break
view = found or window.new_file()
rv = ReplView(view, r, syntax)
rv.closed += self._delete_repl
self.repl_views[r.id] = rv
view.set_scratch(True)
view.set_name("*REPL* [%s]" % (r.name(),))
return rv
except Exception, e:
sublime.error_message(repr(e))
def _delete_repl(self, repl_id):
if repl_id not in self.repl_views:
return None
del self.repl_views[repl_id]
@staticmethod
def translate(window, obj, subst=None):
if subst is None:
subst = ReplManager._subst_for_translate(window)
if isinstance(obj, dict):
return ReplManager._translate_dict(window, obj, subst)
if isinstance(obj, basestring):
return ReplManager._translate_string(window, obj, subst)
if isinstance(obj, list):
return ReplManager._translate_list(window, obj, subst)
return obj
@staticmethod
def _subst_for_translate(window):
""" Return all available substitutions"""
import locale
res = {
"packages": sublime.packages_path(),
"installed_packages": sublime.installed_packages_path()
}
res["editor"] = "subl -w"
res["win_cmd_encoding"] = "utf8"
if sublime.platform() == "windows":
res["win_cmd_encoding"] = locale.getdefaultlocale()[1]
res["editor"] = '"%s"' % (sys.executable,)
av = window.active_view()
if av is None:
return res
filename = av.file_name()
if not filename:
return res
filename = os.path.abspath(filename)
res["file"] = filename
res["file_path"] = os.path.dirname(filename)
res["file_basename"] = os.path.basename(filename)
if window.folders():
res["folder"] = window.folders()[0]
else:
res["folder"] = res["file_path"]
if sublime.load_settings(SETTINGS_FILE).get("use_build_system_hack", False):
project_settings = sublimerepl_build_system_hack.get_project_settings(window)
res.update(project_settings)
return res
@staticmethod
def _translate_string(window, string, subst=None):
#$file, $file_path, $packages
from string import Template
if subst is None:
subst = ReplManager._subst_for_translate(window)
return Template(string).safe_substitute(**subst)
@staticmethod
def _translate_list(window, list, subst=None):
if subst is None:
subst = ReplManager._subst_for_translate(window)
return [ReplManager.translate(window, x, subst) for x in list]
@staticmethod
def _translate_dict(window, dictionary, subst=None):
if subst is None:
subst = ReplManager._subst_for_translate(window)
if PLATFORM in dictionary:
return ReplManager.translate(window, dictionary[PLATFORM], subst)
for k, v in dictionary.items():
dictionary[k] = ReplManager.translate(window, v, subst)
return dictionary
manager = ReplManager()
# Window Commands #########################################
# Opens a new REPL
class ReplOpenCommand(sublime_plugin.WindowCommand):
def run(self, encoding, type, syntax=None, view_id=None, **kwds):
manager.open(self.window, encoding, type, syntax, view_id, **kwds)
# View Commands ###########################################
# Send selection/line to REPL
class ReplSendNewCommand(sublime_plugin.TextCommand):
def run(self, edit):
## TODO: see text_transfer.py!
syntax = self.view.settings().get('syntax')
rv = manager.find_repl_by_syntax(syntax)
if not rv:
return
lines = []
for region in self.view.sel():
if region.empty():
line = self.view.substr(self.view.line(region))
else:
line = self.view.substr(region)
lines.append(line)
cmd = '\n'.join(lines)
print cmd
rv.run(edit, cmd)
# REPL Comands ############################################
# Submits the Command to the REPL
class ReplEnterCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.enter()
class ReplClearCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.clear(edit)
# Resets Repl Command Line
class ReplEscapeCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.escape(edit)
def repl_view_delta(sublime_view):
"""Return a repl_view and number of characters from current selection
to then beggingin of user_input (otherwise known as _output_end)"""
rv = repl_view(sublime_view)
if not rv:
return None, -1
delta = rv._output_end - sublime_view.sel()[0].begin()
return rv, delta
class ReplBackspaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_backspace()
class ReplCtrlBackspaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_ctrl_backspace()
class ReplSuperBackspaceCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_super_backspace()
class ReplLeftCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_left()
class ReplShiftLeftCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_shift_left()
class ReplHomeCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_home()
class ReplShiftHomeCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.on_shift_home()
class ReplViewPreviousCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.previous_command(edit)
class ReplViewNextCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.next_command(edit)
class ReplKillCommand(sublime_plugin.TextCommand):
def run(self, edit):
rv = manager.repl_view(self.view)
if rv:
rv.repl.kill()
class SublimeReplListener(sublime_plugin.EventListener):
def on_selection_modified(self, view):
rv = manager.repl_view(view)
if rv:
rv.on_selection_modified()
def on_close(self, view):
rv = manager.repl_view(view)
if rv:
rv.on_close()
class SubprocessReplSendSignal(sublime_plugin.TextCommand):
def run(self, edit, signal=None):
rv = manager.repl_view(self.view)
subrepl = rv.repl
signals = subrepl.available_signals()
sorted_names = sorted(signals.keys())
if signal in signals:
#signal given by name
self.safe_send_signal(subrepl, signals[signal])
return
if signal in signals.values():
#signal given by code (correct one!)
self.safe_send_signal(subrepl, signal)
return
# no or incorrect signal given
def signal_selected(num):
if num == -1:
return
signame = sorted_names[num]
sigcode = signals[signame]
self.safe_send_signal(subrepl, sigcode)
self.view.window().show_quick_panel(sorted_names, signal_selected)
def safe_send_signal(self, subrepl, sigcode):
try:
subrepl.send_signal(sigcode)
except Exception, e:
sublime.error_message(str(e))
def is_visible(self):
rv = manager.repl_view(self.view)
return rv and hasattr(rv.repl, "send_signal")
def is_enabled(self):
return self.is_visible()
def description(self):
return "Send SIGNAL"