forked from wuub/SublimeREPL
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsublimerepl.py
407 lines (336 loc) · 12.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
# -*- 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 repl
import os
import buzhug
repl_views = {}
PLATFORM = sublime.platform().lower()
SUBLIMEREPL_DIR = os.getcwdu()
SETTINGS_FILE = 'SublimeREPL.sublime-settings'
def repl_view(view):
id = view.settings().get("repl_id")
if not repl_views.has_key(id):
return None
rv = repl_views[id]
rv.update_view(view)
return rv
def find_repl(external_id):
for rv in repl_views.values():
if rv.external_id == external_id:
return rv
return None
def _delete_repl(view):
id = view.settings().get("repl_id")
if not repl_views.has_key(id):
return None
del repl_views[id]
def subst_for_translate(window):
""" Return all available substitutions"""
import os.path
import locale
res = {
"packages": sublime.packages_path(),
"installed_packages" : sublime.installed_packages_path()
}
if sublime.platform() == "windows":
res["win_cmd_encoding"] = locale.getdefaultlocale()[1]
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)
return res
def translate_string(window, string, subst=None):
#$file, $file_path, $packages
from string import Template
if subst is None:
subst = subst_for_translate(window)
return Template(string).safe_substitute(**subst)
def translate_list(window, list, subst=None):
if subst is None:
subst = subst_for_translate(window)
return [translate(window, x, subst) for x in list]
def translate_dict(window, dictionary, subst=None):
if subst is None:
subst = subst_for_translate(window)
if PLATFORM in dictionary:
return translate(window, dictionary[PLATFORM], subst)
for k, v in dictionary.items():
dictionary[k] = translate(window, v, subst)
return dictionary
def translate(window, obj, subst=None):
if subst is None:
subst = subst_for_translate(window)
if isinstance(obj, dict):
return translate_dict(window, obj, subst)
if isinstance(obj, basestring):
return translate_string(window, obj, subst)
if isinstance(obj, list):
return translate_list(window, obj, subst)
return obj
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 NotImplemented
def match(self, command_prefix):
raise NotImplemented
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):
import re
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
if syntax:
view.set_syntax_file(syntax)
self._output_end = view.size()
view.settings().set("repl_id", repl.id)
view.settings().set("repl", True)
view.settings().set("translate_tabs_to_spaces", False)
self._repl_reader = ReplReader(repl)
self._repl_reader.start()
if self.external_id and sublime.load_settings(SETTINGS_FILE).get("presistent_history_enabled"):
self._history = PersistentHistory(self.external_id)
else:
self._history = MemHistory()
self._history_match = None
# begin refreshing attached view
self.update_view_loop()
@property
def external_id(self):
return self.repl.external_id
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 user_input(self):
"""Returns text entered by the user"""
region = sublime.Region(self._output_end, self._view.size())
return self._view.substr(region)
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."""
# 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)
self.scroll_to_end()
def scroll_to_end(self):
v = self._view
v.show(v.line(v.size()).begin())
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 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 view_previous_command(self, edit):
self.ensure_history_match()
self.replace_current_with_history(edit, self._history_match.prev_command())
def view_next_command(self, edit):
self.ensure_history_match()
self.replace_current_with_history(edit, self._history_match.next_command())
def replace_current_with_history(self, edit, cmd):
if not cmd:
return #don't replace if no match
user_region = sublime.Region(self._output_end, self._view.size())
self._view.erase(edit, user_region)
self._view.insert(edit, user_region.begin(), cmd)
class ReplOpenCommand(sublime_plugin.WindowCommand):
def run(self, encoding, type, syntax=None, **kwds):
try:
window = self.window
kwds = translate(window, kwds)
encoding = translate(window, encoding)
r = repl.Repl.subclass(type)(encoding, **kwds)
view = window.new_file()
rv = ReplView(view, r, syntax)
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))
class ReplEnterCommand(sublime_plugin.TextCommand):
def run(self, edit):
v = self.view
if v.sel()[0].begin() != v.size():
v.run_command("insert", {"characters": "\n"})
return
rv = repl_view(v)
rv.push_history(rv.user_input()) # don't include cmd_postfix in history
v.run_command("insert", {"characters": rv.repl.cmd_postfix})
command = rv.user_input()
rv.adjust_end()
rv.repl.write(command)
class ReplViewPreviousCommand(sublime_plugin.TextCommand):
def run(self, edit):
repl_view(self.view).view_previous_command(edit)
class ReplViewNextCommand(sublime_plugin.TextCommand):
def run(self, edit):
repl_view(self.view).view_next_command(edit)
class SublimeReplListener(sublime_plugin.EventListener):
def on_close(self, view):
rv = repl_view(view)
if not rv:
return
rv.repl.close()
_delete_repl(view)
class SubprocessReplSendSignal(sublime_plugin.TextCommand):
def run(self, edit, signal=None):
rv = repl_view(self.view)
subrepl = rv.repl
signals = subrepl.available_signals()
sorted_names = sorted(signals.keys())
if signals.has_key(signal):
#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 = 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"