-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
cs_conn_nrepl_jvm.py
153 lines (135 loc) · 6.21 KB
/
cs_conn_nrepl_jvm.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
import os, sublime, sublime_plugin
from . import cs_common, cs_conn, cs_conn_nrepl_raw, cs_eval
class ConnectionNreplJvm(cs_conn_nrepl_raw.ConnectionNreplRaw):
"""
Enhanced nREPL connection that will work only on JVM
"""
def __init__(self, addr):
super().__init__(addr)
self.eval_op = 'clone-eval-close'
def send(self, msg):
if self.ready():
ns = cs_common.ns + '.middleware'
msg['nrepl.middleware.caught/caught'] = ns + '/print-root-trace'
if 'nrepl.middleware.print/quota' not in msg:
msg['nrepl.middleware.print/quota'] = cs_common.setting('print_quota', 4096)
if 0 == msg['nrepl.middleware.print/quota']:
del msg['nrepl.middleware.print/quota']
super().send(msg)
def eval_impl(self, form):
msg = {'id': form.id,
'session': self.session,
'op': self.eval_op,
'code': form.code,
'ns': form.ns}
if (line := form.line) is not None:
msg['line'] = line
if (column := form.column) is not None:
msg['column'] = column
if (file := form.file) is not None:
msg['file'] = file
if form.print_quota is not None:
msg['nrepl.middleware.print/quota'] = form.print_quota
self.send(msg)
def interrupt_impl(self, batch_id, id):
eval = cs_eval.by_id(id)
msg = {'session': eval.session or self.session,
'op': 'interrupt',
'interrupt-id': id}
self.send(msg)
def handle_connect(self, msg):
if 1 == msg.get('id') and 'new-session' in msg:
self.set_status(2, 'Uploading middleware 1/2...')
self.session = msg['new-session']
file = cs_common.clojure_source('core.clj')
self.send({'id': 2,
'session': self.session,
'op': 'load-file',
'file': file})
return True
if 2 == msg.get('id') and 'done' in msg.get('status', []):
self.set_status(2, 'Uploading middleware 2/2...')
file = cs_common.clojure_source('middleware.clj')
self.send({'id': 3,
'session': self.session,
'op': 'load-file',
'file': file})
return True
elif 3 == msg.get('id') and 'done' in msg.get('status', []):
self.set_status(2, 'Adding middlewares...')
eval_shared = cs_common.setting('eval_shared', '')
ns = cs_common.ns + '.middleware'
self.send({'id': 4 if eval_shared else 5,
'session': self.session,
'op': 'add-middleware',
'middleware': [ns + '/clone-and-eval',
ns + '/time-eval',
ns + '/wrap-errors',
ns + '/wrap-output'],
'extra-namespaces': [cs_common.ns + '.exception', ns]})
return True
elif 4 == msg.get('id') and 'done' in msg.get('status', []):
self.set_status(3, 'Evaluating session code...')
eval_shared = cs_common.setting('eval_shared')
self.send({'id': 5,
'session': self.session,
'op': 'eval',
'code': eval_shared})
return True
elif 5 == msg.get('id') and 'done' in msg.get('status', []):
self.set_status(4, self.get_addr())
return True
def handle_new_session(self, msg):
if 'new-session' in msg and (id := msg.get('id')) and (eval := cs_eval.by_id(id)):
eval.session = msg['new-session']
return True
def handle_value(self, msg):
if 'value' in msg and (id := msg.get('id')):
time = msg.get(cs_common.ns + '.middleware/time-taken')
if time:
time = time / 1000000
cs_eval.on_success(id, msg.get('value'), time = time)
return True
def handle_exception(self, msg):
if (id := msg.get('id')):
ns = cs_common.ns + '.middleware/'
present = lambda key: (ns + key) in msg
get = lambda key: msg.get(ns + key)
if get('root-ex-class') and get('root-ex-msg'):
text = get('root-ex-class') + ': ' + get('root-ex-msg')
line = None
column = None
if get('root-ex-data'):
text += ' ' + get('root-ex-data')
if present('line') and present('column') and get('source'):
line = get('line') - 1
column = get('column')
text += f" ({get('source')}:{get('line')}:{get('column')})"
cs_eval.on_exception(id, text, line = line, column = column, trace = get('trace'))
return True
else:
return super().handle_exception(msg)
def handle_msg(self, msg):
cs_common.debug('RCV {}', msg)
for key in msg.get('nrepl.middleware.print/truncated-keys', []):
msg[key] += ' ...'
self.handle_connect(msg) \
or self.handle_disconnect(msg) \
or self.handle_new_session(msg) \
or self.handle_value(msg) \
or self.handle_exception(msg) \
or self.handle_lookup(msg) \
or self.handle_done(msg)
class ClojureSublimedConnectNreplJvmCommand(sublime_plugin.WindowCommand):
def run(self, address, timeout = 0):
state = cs_common.get_state(self.window)
state.last_conn = ('clojure_sublimed_connect_nrepl_jvm', {'address': address})
if address == 'auto':
address = self.input({}).initial_text()
ConnectionNreplJvm(address).try_connect(timeout = timeout)
def input(self, args):
if 'address' not in args:
return cs_conn.AddressInputHandler(port_files = ['.nrepl-port', '.shadow-cljs/nrepl.port'])
def is_enabled(self):
state = cs_common.get_state(self.window)
return state.conn is None