Created
October 19, 2016 19:29
-
-
Save tbelaire/94ace69a6c7536f68e6495a9305e2ed8 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import json | |
import subprocess | |
import os | |
import re | |
class LangServer: | |
def __init__(self): | |
self.server = subprocess.Popen( | |
["/Users/tbelaire/.cargo/bin/cargo", "run", "-q"], | |
env={ "RUST_BACKTRACE": "1", | |
"SYS_ROOT": "/Users/tbelaire/.multirust/toolchains/nightly-x86_64-apple-darwin", | |
"TMPDIR": "/Users/tbelaire/tmp"}, | |
bufsize=0, | |
stdin=subprocess.PIPE, | |
stdout=subprocess.PIPE, | |
stderr=subprocess.DEVNULL, | |
universal_newlines=True) | |
self.header_regex = re.compile("(?P<header>(\w|-)+): (?P<value>\d+)") | |
self.stdin = self.server.stdin | |
self.stdout = self.server.stdout | |
self.poll = self.server.poll | |
def read_headers(self): | |
"""Reads in the headers for a response""" | |
result = {} | |
while True: | |
line = self.server.stdout.readline() | |
if line == "\n": | |
break | |
m = self.header_regex.match(line) | |
result[m.group("header")] = m.group("value") | |
return result | |
def read_response(self): | |
headers = self.read_headers() | |
size = int(headers["Content-Length"]) | |
content = self.server.stdout.read(size) | |
return json.loads(content) | |
rls = LangServer() | |
def format_request(request): | |
content = json.dumps(request, indent=2) | |
content_length = len(content) | |
result = "Content-Length: {}\r\n\r\n{}".format(content_length, content) | |
return result | |
init_request = { | |
"jsonrpc": "2.0", | |
"id": 1, | |
"method": "initialize", | |
"params": { | |
"processId": os.getpid(), | |
"rootPath": "/Users/tbelaire/github/rustls/sample_project", | |
"capabilities":{ | |
} | |
} | |
} | |
# _ = rls.stdout.read() | |
request = format_request(init_request) | |
print("Request:") | |
print(request) | |
rls.stdin.write(request) | |
rls.stdin.flush() | |
if rls.poll() != None: | |
print("Exited!") | |
print("Response:") | |
response = rls.read_response() | |
print(json.dumps(response, indent=2)) | |
print("Response:") | |
response = rls.read_response() | |
print(json.dumps(response, indent=2)) | |
print("Done") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment