-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathlangserver_ext.py
60 lines (44 loc) · 1.69 KB
/
langserver_ext.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
import logging
import subprocess
import threading
from tornado import ioloop, process, web, websocket
from pylsp_jsonrpc import streams
try:
import orjson as json
except ImportError:
import json
log = logging.getLogger(__name__)
class LanguageServerWebSocketHandler(websocket.WebSocketHandler):
"""Setup tornado websocket handler to host an external language server."""
writer = None
def open(self, *args, **kwargs):
log.info("Spawning pylsp subprocess")
# Create an instance of the language server
proc = process.Subprocess(
['pylsp', '-v'],
stdin=subprocess.PIPE,
stdout=subprocess.PIPE
)
# Create a writer that formats json messages with the correct LSP headers
self.writer = streams.JsonRpcStreamWriter(proc.stdin)
# Create a reader for consuming stdout of the language server. We need to
# consume this in another thread
def consume():
# Start a tornado IOLoop for reading/writing to the process in this thread
ioloop.IOLoop()
reader = streams.JsonRpcStreamReader(proc.stdout)
reader.listen(lambda msg: self.write_message(json.dumps(msg)))
thread = threading.Thread(target=consume)
thread.daemon = True
thread.start()
def on_message(self, message):
"""Forward client->server messages to the endpoint."""
self.writer.write(json.loads(message))
def check_origin(self, origin):
return True
if __name__ == "__main__":
app = web.Application([
(r"/python", LanguageServerWebSocketHandler),
])
app.listen(3000, address='127.0.0.1')
ioloop.IOLoop.current().start()