lsp: Fix text encoding issue

Content-Length is the number of bytes in the message, but the JSONRPC
code was interpreting it as a number of characters (when both reading and
writing), which caused it to fail on multibyte UTF-8 characters.
This commit is contained in:
James Westman 2021-12-06 11:43:10 -06:00
parent 08f7a4ebbd
commit d23c06b8fd
No known key found for this signature in database
GPG key ID: CE2DBA0ADB654EA6

View file

@ -103,12 +103,12 @@ class LanguageServer:
line = "" line = ""
content_len = -1 content_len = -1
while content_len == -1 or (line != "\n" and line != "\r\n"): while content_len == -1 or (line != "\n" and line != "\r\n"):
line = sys.stdin.readline() line = sys.stdin.buffer.readline().decode()
if line == "": if line == "":
return return
if line.startswith("Content-Length:"): if line.startswith("Content-Length:"):
content_len = int(line.split("Content-Length:")[1].strip()) content_len = int(line.split("Content-Length:")[1].strip())
line = sys.stdin.read(content_len) line = sys.stdin.buffer.read(content_len).decode()
self._log("input: " + line) self._log("input: " + line)
data = json.loads(line) data = json.loads(line)
@ -126,7 +126,7 @@ class LanguageServer:
data["jsonrpc"] = "2.0" data["jsonrpc"] = "2.0"
line = json.dumps(data, separators=(",", ":")) + "\r\n" line = json.dumps(data, separators=(",", ":")) + "\r\n"
self._log("output: " + line) self._log("output: " + line)
sys.stdout.write(f"Content-Length: {len(line)}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}") sys.stdout.write(f"Content-Length: {len(line.encode())}\r\nContent-Type: application/vscode-jsonrpc; charset=utf-8\r\n\r\n{line}")
sys.stdout.flush() sys.stdout.flush()
def _log(self, msg): def _log(self, msg):