-
Notifications
You must be signed in to change notification settings - Fork 0
/
wrap-langserv
executable file
·535 lines (442 loc) · 16.9 KB
/
wrap-langserv
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
#!/usr/bin/env python3
import socket
import selectors
import signal
import subprocess
import os
import sys
import traceback
import json
import time
import base64
import re
# TODO: write this in a way which would work on windows
import fcntl
from langserv import util, docstate
red = "\x1b[31m"
grn = "\x1b[32m"
yel = "\x1b[33m"
blu = "\x1b[34m"
nil = "\x1b[m"
with open("/tmp/wrap-langserv.log", "w"):
pass
def log(*args, **kwargs):
with open("/tmp/wrap-langserv.log", "a") as f:
print(*args, **kwargs, file=f)
class Connection:
"""An incoming connection from outside"""
def __init__(self, conn, injector, selector):
self.conn = conn
conn.setblocking(False)
self.injector = injector
self.selector = selector
self.write_buf = b''
self.parser = util.Parser(self.handle_conn_msg)
self.selector.register(self.conn, selectors.EVENT_READ)
self.closed = False
def close(self):
self.selector.unregister(self.conn)
self.conn.close()
self.closed = True
def write(self, msg):
if self.write_buf:
self.write_buf += msg
return
self.write_buf = msg
mask = selectors.EVENT_READ | selectors.EVENT_WRITE
self.selector.modify(self.conn, mask)
def event(self, key, mask):
readable = mask & selectors.EVENT_READ
writable = mask & selectors.EVENT_WRITE
if readable:
try:
msg = self.conn.recv(4096)
except ConnectionError:
msg = None
if not msg:
self.close()
return
log(grn + msg.decode('utf8') + nil)
self.parser.feed(msg)
if writable:
n = self.conn.send(self.write_buf)
if not n:
self.close()
return
self.write_buf = self.write_buf[n:]
if not self.write_buf:
self.selector.modify(self.conn, selectors.EVENT_READ)
def handle_conn_msg(self, content, body, headers):
parsed = json.loads(body)
# injected messages must have ID's.
if "id" not in parsed:
raise ValueError(f"no id in {parsed}")
def cb(_content, _parsed):
if not self.closed():
self.write(content)
self.injector.inject_msg(parsed["id"], content, cb)
class LanguageServer:
"""The language server we launch as a subprocess"""
def __init__(self, cmd, injector, selector):
self.injector = injector
self.selector = selector
self.parser = util.Parser(self.handle_server_response)
self.proc = subprocess.Popen(
cmd, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE
)
self.expect_exit = False
self.exit_code = None
self.write_buf = b""
fcntl.fcntl(self.proc.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(self.proc.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(self.proc.stderr.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
self.selector.register(self.proc.stdout, selectors.EVENT_READ, self)
self.selector.register(self.proc.stderr, selectors.EVENT_READ, self)
self.stdout_drained = False
self.stderr_drained = False
def close(self):
if self.proc is None:
return
if not self.stdout_drained:
self.selector.unregister(self.proc.stdout)
if not self.stderr_drained:
self.selector.unregister(self.proc.stderr)
if self.write_buf:
self.selector.unregister(self.proc.stdin)
self.proc.terminate()
self.exit_code = self.proc.wait()
self.proc = None
def write(self, msg):
if self.proc == None:
raise ValueError("can't write to dead server")
if self.write_buf:
self.write_buf += msg
return None
self.write_buf = msg
self.selector.register(self.proc.stdin, selectors.EVENT_WRITE, self)
def event(self, key, mask):
readable = mask & selectors.EVENT_READ
writable = mask & selectors.EVENT_WRITE
# stdout
if readable and key.fileobj == self.proc.stdout:
msg = self.proc.stdout.raw.read(4096)
if not msg:
if not self.expect_exit:
raise ValueError("server died")
self.stdout_drained = True
self.selector.unregister(self.proc.stdout)
return
log(yel + msg.decode('utf8') + nil)
self.parser.feed(msg)
# stderr
if readable and key.fileobj == self.proc.stderr:
msg = self.proc.stderr.raw.read(4096)
if not msg:
if not self.expect_exit:
raise ValueError("server died")
self.stderr_drained = True
self.selector.unregister(self.proc.stderr)
return
self.injector.handle_server_error(msg)
# stdin
if writable:
n = self.proc.stdin.raw.write(self.write_buf)
if not n:
if not self.expect_exit:
raise ValueError("server died")
self.write_buf = b""
self.selector.unregister(self.proc.stdin)
return
self.write_buf = self.write_buf[n:]
if not self.write_buf:
self.selector.unregister(self.proc.stdin)
def handle_server_response(self, content, body, headers):
self.injector.handle_server_response(content, body)
def gather(self):
if self.stdout_drained and self.stderr_drained and not self.write_buf:
self.close()
return True
return False
class Editor:
"""The editor which launched us as a subprocess"""
def __init__(self, injector, selector):
self.injector = injector
self.selector = selector
fcntl.fcntl(sys.stdin.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
fcntl.fcntl(sys.stdout.fileno(), fcntl.F_SETFL, os.O_NONBLOCK)
self.write_buf = b""
self.err_buf = b""
self.selector.register(sys.stdin, selectors.EVENT_READ, self)
self.parser = util.Parser(self.handle_stdin_msg)
def write(self, msg):
if self.write_buf:
self.write_buf += msg
return
self.write_buf = msg
self.selector.register(sys.stdout, selectors.EVENT_WRITE, self)
def write_err(self, msg):
if self.err_buf:
self.err_buf += msg
return
self.err_buf = msg
self.selector.register(sys.stderr, selectors.EVENT_WRITE, self)
def event(self, key, mask):
readable = mask & selectors.EVENT_READ
writable = mask & selectors.EVENT_WRITE
# stdin
if readable:
msg = os.read(sys.stdin.fileno(), 4096)
if not msg:
raise ValueError("stdin from editor failed")
self.parser.feed(msg)
# stdout
if writable and key.fileobj == sys.stdout:
n = os.write(sys.stdout.fileno(), self.write_buf)
if not n:
raise ValueError("stdout to editor failed")
self.write_buf = self.write_buf[n:]
if not self.write_buf:
self.selector.unregister(sys.stdout)
# stderr
if writable and key.fileobj == sys.stderr:
n = os.write(sys.stderr.fileno(), self.err_buf)
if not n:
raise ValueError("stderr to editor failed")
self.err_buf = self.err_buf[n:]
if not self.err_buf:
self.selector.unregister(sys.stderr)
def handle_stdin_msg(self, content, body, headers):
# always pass the message to the language server
self.injector.handle_editor_msg(content, body)
def close(self):
self.selector.unregister(sys.stdin)
if self.write_buf:
self.selector.unregister(sys.stdout)
def gather(self):
# don't exit until we have drained write_buf and err_buf
return all([not self.write_buf, not self.err_buf])
class Timeout:
def __init__(self, period=1.0):
self.period = period
self.last = time.time()
def passed(self):
now = time.time()
if now > self.last + self.period:
self.last = now
return True
return False
class LSPInjector:
"""Inject traffic between the Editor and LangServ based on Connections"""
def __init__(self, cmd, selector):
self.selector = selector
# connections maps socket connections to Connection objects
self.connections = {}
# map id's to callbacks we make to handle the responses
self.injected = {}
# map id's to callbacks we make when we observe responses
self.watching = {}
if os.path.exists("/tmp/wrap-langserv.sock"):
os.remove("/tmp/wrap-langserv.sock")
self.listener = socket.socket(family=socket.AF_UNIX)
self.listener.setblocking(False)
self.listener.bind("/tmp/wrap-langserv.sock")
self.listener.listen()
self.ls = LanguageServer(cmd, self, selector)
self.editor = Editor(self, selector)
self.want_exit = False
self.selector.register(self.listener, selectors.EVENT_READ)
# map uri's to open Documents
self.docs = {}
# talon is a special Connection that we initiate and push updates to
self.talon = None
self.docsym = None
self.id_gen = util.IDGen("injector")
self.probe_talon()
def handle_connection(self):
conn, _ = self.listener.accept()
self.connections[conn] = Connection(conn, self, self.selector)
def handle_editor_msg(self, content, body):
# write the message before we even have a chance to inject anything so
# that text updates preceed what we inject
self.ls.write(content)
log(content.decode('utf8'))
parsed = json.loads(body)
method = parsed.get("method")
if method == "textDocument/didOpen":
uri = parsed["params"]["textDocument"]["uri"]
# create a new document
text = parsed["params"]["textDocument"]["text"]
self.docs[uri] = docstate.Document(text)
# request documentSymbol
self.inject_documentSymbol(uri)
elif method == "textDocument/didClose":
uri = parsed["params"]["textDocument"]["uri"]
# discard document state
del self.docs[uri]
elif method == "textDocument/didChange":
uri = parsed["params"]["textDocument"]["uri"]
# sync changes
doc = self.docs[uri]
for change in parsed["params"]["contentChanges"]:
doc.did_change(docstate.ContentChange(change))
# request documentSymbol
self.inject_documentSymbol(uri)
elif method == "textDocument/completion":
uri = parsed["params"]["textDocument"]["uri"]
id = parsed["id"]
pos = docstate.Position(parsed["params"]["position"])
self.watch_completion(uri, id, pos)
elif method == "exit":
self.ls.expect_exit = True
self.want_exit = True
def inject_documentSymbol(self, uri):
id = next(self.id_gen)
msg = {
"jsonrpc": "2.0",
"method": "textDocument/documentSymbol",
"id": id,
"params": {
"textDocument": {
"uri": uri
}
}
}
content = util.make_content(msg)
def docsym_cb(_, parsed):
headers = {"Uri": uri, "Type": "documentSymbol"}
_content = util.make_content(parsed, headers)
self.docsym = _content
if self.talon is not None:
self.talon.write(_content)
self.inject_msg(id, content, docsym_cb)
def watch_completion(self, uri, id, pos):
text = self.docs[uri].text
# give talon the line preceeding the completion request, so it can
# figure out how much of the word to not type
pretext = ""
if pos.character > 0:
line_start = docstate.Position(line=pos.line, character=0)
start = line_start.index(text)
end = pos.index(text)
pretext = text[start:end]
pretext = base64.b64encode(pretext.encode('utf8')).decode('utf8')
def cb(_, _parsed):
headers = {
"Uri": uri,
"Type": "completion",
"Pretext": pretext,
}
_content = util.make_content(_parsed, headers)
if self.talon is not None:
self.talon.write(_content)
self.watch_msg(id, cb)
def watch_msg(self, id, cb):
self.watching[id] = cb
def inject_msg(self, id, content, cb):
self.injected[id] = cb
self.ls.write(content)
log(blu + content.decode('utf8') + nil)
def handle_server_response(self, content, body):
parsed = json.loads(body)
# do we need to direct this response to a connection?
if "id" in parsed and parsed["id"] in self.injected:
cb = self.injected.pop(parsed["id"])
cb(content, parsed)
return
self.editor.write(content)
# did we want that message for something?
if "id" in parsed and parsed["id"] in self.watching:
cb = self.watching.pop(parsed["id"])
cb(content, parsed)
def handle_server_error(self, msg):
# log the message in red
log(red + msg.decode('utf8') + nil, end="")
# pass the message to the editor.
self.editor.write_err(msg)
def close(self):
for conn in self.connections.values():
conn.close()
self.ls.close()
self.editor.close()
self.selector.unregister(self.listener)
self.listener.close()
def __enter__(self):
return self
def __exit__(self, etype, value, tb):
self.close()
def run(self):
timeout = Timeout(period=1.0)
while not self.gather():
for key, mask in self.selector.select(timeout.period):
readable = mask & selectors.EVENT_READ
writable = mask & selectors.EVENT_WRITE
if key.data == self.editor:
self.editor.event(key, mask)
elif key.data == self.ls:
self.ls.event(key, mask)
elif key.fileobj == self.listener:
self.handle_connection()
elif self.talon and key.fileobj == self.talon.conn:
self.talon.event(key, mask)
if self.talon.closed:
self.talon = None
else:
conn = self.connections[key.fileobj]
conn.event(key, mask)
if conn.closed:
del self.connections[key.fileobj]
if timeout.passed():
self.probe_talon()
def gather(self):
# see if we are ready to exit
return all([
self.want_exit,
self.editor.gather(),
self.ls.gather(),
])
def probe_talon(self):
if self.talon is not None:
return
sockpath = "~/.talon/user/rexroni_talon/langserv/langserv.sock"
sockpath = os.path.expanduser(sockpath)
sock = socket.socket(family=socket.AF_UNIX)
try:
sock.connect(sockpath)
except (ConnectionError, FileNotFoundError):
sock.close()
return
log('connected to talon!')
self.talon = Connection(sock, self, self.selector)
if self.docsym is not None:
self.talon.write(self.docsym)
def main(cmd):
exit_code = 0
try:
with selectors.DefaultSelector() as selector:
with LSPInjector(cmd, selector) as injector:
injector.run()
if injector.ls.exit_code is not None:
exit_code = injector.ls.exit_code
except Exception as e:
with open("/tmp/wrap-langserv.log", "a") as f:
traceback.print_exc(file=f)
# We're out of the event loop, but still give the editor a one-liner
print(e, file=sys.stderr)
exit(1)
exit(exit_code)
if __name__ == "__main__":
# check for path hack
executable = os.path.basename(sys.argv[0])
if executable != "wrap-langserv":
# insert the name of the executable we were called with before the args
args = [executable] + sys.argv[1:]
else:
args = sys.argv[1:]
# regardless of the path hack, always remove the path hack before calling
# the subprocess, in case the path hack is active but we were invoked
# directly
path = os.environ.get("PATH")
if path is not None:
path = re.sub("[^:]*/langserv/path-hack/?:", "", path)
os.environ["PATH"] = path
main(args)