forked from dcsobral/sublime-ensime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc.py
552 lines (437 loc) · 17.8 KB
/
rpc.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
from __future__ import unicode_literals
import inspect, functools
from functools import partial as bind
import sexp
from sexp import key, sym
# ############################# DATA STRUCTURES ##############################
class ActiveRecord(object):
@classmethod
def parse_list(cls, raw):
if not raw: return []
if type(raw[0]) == type(key(":key")):
m = sexp.sexp_to_key_map(raw)
field = ":" + cls.__name__.lower() + "s"
return [cls.parse(raw) for raw in (m[field] if field in m else [])]
else:
return [cls.parse(raw) for raw in raw]
@classmethod
def parse(cls, raw):
"""Parse a data type from a raw data structure"""
if not raw: return None
value_map = sexp.sexp_to_key_map(raw)
self = cls()
populate = getattr(self, "populate")
populate(value_map)
return self
def unparse(self):
raise Exception("abstract method: ActiveRecord.unparse - on " + str(this))
def __str__(self):
return str(self.__dict__)
class Note(ActiveRecord):
def populate(self, m):
self.message = m[":msg"]
self.file_name = m[":file"]
self.severity = m[":severity"]
self.start = m[":beg"]
self.end = m[":end"]
self.line = m[":line"]
self.col = m[":col"]
class CompletionInfoList(ActiveRecord):
@classmethod
def create(cls, prefix, completions):
self = CompletionInfoList()
self.prefix = prefix
self.completions = completions
return self
def populate(self, m):
self.prefix = m[":prefix"]
self.completions = CompletionInfo.parse_list(m[":completions"])
class CompletionSignature(ActiveRecord):
"""A completion signature consists of the parameter 'sections' which is a list of name to type) and a 'result' type.
n.b. these are user readable rather than programmtic for presentation to the user.
# sections: List[List[(String, String)]],
# result: String
"""
def __init__(self, sections, result):
self.sections = sections
self.result = result
@classmethod
def from_raw(cls, data):
# this hacky is all because () in both false and and empty list
# the parser cannot tell, so hack it until we move to jerk
sections_raw = data[0] if(data[0] is not False) else []
sections = []
for s in sections_raw:
if s == False:
sections.append([])
else:
sections.append(s)
result = data[1]
return CompletionSignature(sections, result)
def __repr__(self):
return 'CompletionSignature("{str(self.sections)}", "{self.result}")'.format(self=self)
class CompletionInfo(ActiveRecord):
def populate(self, m):
self.name = m[":name"]
self.signature = CompletionSignature.from_raw(m[":type-sig"])
self.is_callable = bool(m[":is-callable"]) if ":is-callable" in m else False
self.type_id = m[":type-id"]
self.to_insert = m[":to-insert"] if ":to-insert" in m else None
def __repr__(self):
return 'CompletionInfo("{self.name}", "{self.signature}", {self.is_callable}, {self.type_id}, ...)'.format(
self=self)
class SourcePosition(ActiveRecord):
def populate(self, m):
self.file_name = m[":file"] if ":file" in m else None
self.offset = m[":offset"] if ":offset" in m else None
self.start = m[":start"] if ":start" in m else None
self.end = m[":end"] if ":end" in m else None
class SymbolInfo(ActiveRecord):
def populate(self, m):
self.name = m[":name"]
self.type = TypeInfo.parse(m[":type"])
self.decl_pos = SourcePosition.parse(m[":decl-pos"]) if ":decl-pos" in m else None
self.is_callable = bool(m[":is-callable"]) if ":is-callable" in m else False
self.owner_type_id = m[":owner-type-id"] if ":owner-type-id" in m else None
class TypeInfo(ActiveRecord):
def populate(self, m):
self.name = m[":name"]
self.type_id = m[":type-id"]
isArrowType = bool(m[":arrow-type"]) if ":arrow-type" in m else False
if isArrowType:
self.arrow_type = True
self.result_type = TypeInfo.parse(m[":result-type"])
self.param_sections = ParamSectionInfo.parse_list(m[":param-sections"]) if ":param-sections" in m else []
else:
# Basic type
self.arrow_type = False
self.full_name = m[":full-name"] if ":full-name" in m else None
self.decl_as = m[":decl-as"] if ":decl-as" in m else None
self.decl_pos = SourcePosition.parse(m[":pos"]) if ":pos" in m else None
self.type_args = TypeInfo.parse_list(m[":type-args"]) if ":type-args" in m else []
self.outer_type_id = m[":outer-type-id"] if ":outer-type-id" in m else None
self.members = Member.parse_list(m[":members"]) if ":members" in m else []
class SymbolSearchResults(ActiveRecord):
# we override parse here because raw contains a List of SymbolSearchResult
# typehe ActiveRecord parse method expects raw to contain an object at this point
# and calls sexp_to_key_map
@classmethod
def parse(cls, raw):
if not raw: return None
self = cls()
self.populate(raw)
return self
def populate(self, m):
self.results = SymbolSearchResult.parse_list(m)
class SymbolSearchResult(ActiveRecord):
def populate(self, m):
self.name = m[":name"]
self.local_name = m[":local-name"]
self.decl_as = m[":decl-as"] if ":decl-as" in m else None
self.pos = SourcePosition.parse(m[":pos"]) if ":pos" in m else None
class RefactorResult(ActiveRecord):
def populate(self, m):
self.done = True
class Member(ActiveRecord):
def populate(self, m):
pass
class ParamSectionInfo(ActiveRecord):
def populate(self, m):
self.is_implicit = bool(m[":is-implicit"]) if ":is-implicit" in m else False
self.params = Param.parse_list(m[":params"]) if ":params" in m else []
class Param(ActiveRecord):
def populate(self, m):
pass
class DebugEvent(ActiveRecord):
def populate(self, m):
self.type = str(m[":type"])
if self.type == "output":
self.body = m[":body"]
elif self.type == "step":
self.thread_id = m[":thread-id"]
self.thread_name = m[":thread-name"]
self.file_name = m[":file"]
self.line = m[":line"]
elif self.type == "breakpoint":
self.thread_id = m[":thread-id"]
self.thread_name = m[":thread-name"]
self.file_name = m[":file"]
self.line = m[":line"]
elif self.type == "death":
pass
elif self.type == "start":
pass
elif self.type == "disconnect":
pass
elif self.type == "exception":
self.exception_id = m[":exception"]
self.thread_id = m[":thread-id"]
self.thread_name = m[":thread-name"]
self.file_name = m[":file"]
self.line = m[":line"]
elif self.type == "threadStart":
self.thread_id = m[":thread-id"]
elif self.type == "threadDeath":
self.thread_id = m[":thread-id"]
else:
raise Exception("unexpected debug event of type " + str(self.type) + ": " + str(m))
class DebugKickoffResult(ActiveRecord):
def __nonzero__(self):
return not self.error
def populate(self, m):
status = m[":status"]
if status == "success":
self.error = False
elif status == "error":
self.error = True
self.code = m[":error-code"]
self.details = m[":details"]
else:
raise Exception("unexpected status: " + str(status))
class DebugBacktrace(ActiveRecord):
def populate(self, m):
self.frames = DebugStackFrame.parse_list(m[":frames"]) if ":frames" in m else []
self.thread_id = m[":thread-id"]
self.thread_name = m[":thread-name"]
class SourceFileInfo(ActiveRecord):
def populate(self, m):
self.file = m[":file"]
self.contents = m[":contents"] if ":contents" in m else None
self.contents_in = m[":contents-in"] if ":contents-in" in m else None
def __init__(self, file_name, contents=None, contents_in=None):
self.file = file_name
self.contents = contents
self.contents_in = contents_in
def unparse(self):
base = [key(":file"), self.file]
if self.contents is not None:
base.extend([key(":contents"), self.contents])
if self.contents_in is not None:
base.extend([key(":contents-in"), self.contents_in])
return [base]
class DebugStackFrame(ActiveRecord):
def populate(self, m):
self.index = m[":index"]
self.locals = DebugStackLocal.parse_list(m[":locals"]) if ":locals" in m else []
self.num_args = m[":num-args"]
self.class_name = m[":class-name"]
self.method_name = m[":method-name"]
self.pc_location = DebugSourcePosition.parse(m[":pc-location"])
self.this_object_id = m[":this-object-id"]
class DebugSourcePosition(ActiveRecord):
def populate(self, m):
self.file_name = m[":file"]
self.line = m[":line"]
class DebugStackLocal(ActiveRecord):
def populate(self, m):
self.index = m[":index"]
self.name = m[":name"]
self.summary = m[":summary"]
self.type_name = m[":type-name"]
class DebugValue(ActiveRecord):
def populate(self, m):
self.type = m[":val-type"]
self.type_name = m[":type-name"]
self.length = m[":length"] if ":length" in m else None
self.element_type_name = m[":element-type-name"] if ":element-type-name" in m else None
self.summary = m[":summary"] if ":summary" in m else None
self.object_id = m[":object-id"] if ":object-id" in m else None
self.fields = DebugObjectField.parse_list(m[":fields"]) if ":fields" in m else []
if str(self.type) == "null" or str(self.type) == "prim" or str(self.type) == "obj" or str(
self.type) == "str" or str(self.type) == "arr":
pass
else:
raise Exception("unexpected debug value of type " + str(self.type) + ": " + str(m))
class DebugObjectField(ActiveRecord):
def populate(self, m):
self.index = m[":index"]
self.name = m[":name"]
self.summary = m[":summary"]
self.type_name = m[":type-name"]
class DebugLocation(ActiveRecord):
def populate(self, m):
self.type = str(m[":type"])
if self.type == "reference":
self.object_id = m[":object-id"]
elif self.type == "element":
self.object_id = m[":object-id"]
self.index = m[":index"]
elif self.type == "field":
self.object_id = m[":object-id"]
self.field = m[":field"]
elif self.type == "slot":
self.thread_id = m[":thread-id"]
self.frame = m[":frame"]
self.offset = m[":offset"]
else:
raise Exception("unexpected debug location of type " + str(self.type) + ": " + str(m))
class DebugLocationReference(DebugLocation):
def __init__(self, object_id):
self.object_id = object_id
def unparse(self):
return [[key(":type"), sym("reference"), key(":object-id"), self.object_id]]
class DebugLocationElement(DebugLocation):
def __init__(self, object_id, index):
self.object_id = object_id
self.index = index
def unparse(self):
return [[key(":type"), sym("element"), key(":object-id"), self.object_id, key(":index"), self.index]]
class DebugLocationField(DebugLocation):
def __init__(self, object_id, field):
self.object_id = object_id
self.field = field
def unparse(self):
return [[key(":type"), sym("field"), key(":object-id"), self.object_id, key(":field"), self.field]]
class DebugLocationSlot(DebugLocation):
def __init__(self, thread_id, frame, offset):
self.thread_id = thread_id
self.frame = frame
self.offset = offset
def unparse(self):
return [
[key(":type"), sym("slot"), key(":thread-id"), self.thread_id, key(":frame"), self.frame, key(":offset"),
self.offset]]
# ############################# REMOTE PROCEDURES ##############################
def _mk_req(func, *args, **kwargs):
if kwargs: raise Exception("kwargs are not supported by the RPC proxy")
req = []
def translate_name(name):
if name.startswith("_"): name = name[1:]
name = name.replace("_", "-")
return name
req.append(sym("swank:" + translate_name(func.__name__)))
(spec_args, spec_varargs, spec_keywords, spec_defaults) = inspect.getargspec(func)
if spec_varargs: raise Exception("varargs in signature of " + str(func))
if spec_keywords: raise Exception("keywords in signature of " + str(func))
if len(spec_args) != len(args):
if len(args) < len(spec_args) and len(args) + len(spec_defaults) >= len(spec_args):
# everything is fine. we can use default values for parameters to provide arguments to the call
args += spec_defaults[len(spec_defaults) - len(spec_args) + len(args):]
else:
preamble = "argc mismatch in signature of " + str(func) + ": "
expected = "expected " + str(len(spec_args)) + " args " + str(spec_args) + ", "
actual = "actual " + str(len(args)) + " args " + str(args) + " with types " + str(
map(lambda a: type(a), args))
raise Exception(preamble + expected + actual)
for arg in args[1:]: # strip off self
if hasattr(arg, "unparse"):
argreq = arg.unparse()
else:
argreq = [arg]
req.extend(argreq)
return req
def async_rpc(*args):
parser = args[0] if args else lambda raw: raw
def wrapper(func):
def wrapped(*args, **kwargs):
self = args[0]
if callable(args[-1]):
on_complete = args[-1]
args = args[:-1]
else:
on_complete = None
req = _mk_req(func, *args, **kwargs)
def callback(payload):
data = parser(payload)
if (on_complete):
on_complete(data)
self.env.controller.client.async_req(req, callback, call_back_into_ui_thread=True)
return wrapped
return wrapper
def sync_rpc(*args):
parser = args[0] if args else lambda raw: raw
def wrapper(func):
def wrapped(*args, **kwargs):
self = args[0]
req = _mk_req(func, *args, **kwargs)
timeout = self.env.settings.get("timeout_" + func.__name__)
raw = self.env.controller.client.sync_req(req, timeout=timeout)
return parser(raw)
return wrapped
return wrapper
class Rpc(object):
def __init__(self, env):
self.env = env
@sync_rpc()
def shutdown_server(self):
pass
@async_rpc()
def typecheck_file(self, file):
pass
@async_rpc()
def patch_source(self, file_name, edits):
pass
@sync_rpc(CompletionInfoList.parse)
def completions(self, file_name, position, max_results, case_sensitive, reload_from_disk):
pass
@async_rpc(TypeInfo.parse)
def type_at_point(self, file_name, position):
pass
@async_rpc(SymbolInfo.parse)
def symbol_at_point(self, file_name, position):
pass
@async_rpc(SymbolSearchResults.parse_list)
def import_suggestions(self, file_name, position, type_names, max_results):
pass
@async_rpc(RefactorResult.parse)
def prepare_refactor(self, procedure_id, refactor_type, parameters, require_confirmation):
pass
@async_rpc()
def debug_set_break(self, file_name, line):
pass
@async_rpc()
def debug_clear_break(self, file_name, line):
pass
@async_rpc()
def debug_clear_all_breaks(self):
pass
@async_rpc(DebugKickoffResult.parse)
def _debug_start(self, command_line):
pass
@async_rpc(DebugKickoffResult.parse)
def _debug_attach(self, host, port):
pass
def debug_start(self, launch, breakpoints, on_complete=None):
def set_breakpoints(breakpoints, status):
if status:
if breakpoints:
self.debug_set_break(breakpoints[0].file_name, breakpoints[0].line,
bind(set_breakpoints, breakpoints[1:]))
else:
if launch.main_class:
self._debug_start(launch.command_line, on_complete)
elif launch.remote_address:
self._debug_attach(launch.remote_host, launch.remote_port, on_complete)
else:
raise Exception("unsupported launch: " + str(launch))
elif on_complete:
on_complete(status)
def clear_breakpoints():
def callback(status):
if status:
set_breakpoints(breakpoints, status)
elif on_complete:
on_complete(status)
self.debug_clear_all_breaks(callback)
clear_breakpoints()
@async_rpc()
def debug_stop(self):
pass
@async_rpc()
def debug_step(self, thread_id):
pass
@async_rpc()
def debug_next(self, thread_id):
pass
@async_rpc()
def debug_continue(self, thread_id):
pass
@sync_rpc(DebugBacktrace.parse)
def debug_backtrace(self, thread_id, first_frame=0, num_frames=-1):
pass
@sync_rpc(DebugValue.parse)
def debug_value(self, debug_location):
pass
@sync_rpc()
def debug_to_string(self, thread_id, debug_location):
pass