-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlsp_reader_writer.tl
163 lines (129 loc) · 4.43 KB
/
lsp_reader_writer.tl
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
local _module_name = "lsp_reader_writer"
local StdinReader <const> = require("teal_language_server.stdin_reader")
local lsp <const> = require("teal_language_server.lsp")
local json <const> = require("cjson")
local uv <const> = require("luv")
local asserts <const> = require("teal_language_server.asserts")
local tracing <const> = require("teal_language_server.tracing")
local class <const> = require("teal_language_server.class")
local record LspReaderWriter
_stdin_reader:StdinReader
_stdout: uv.Pipe
_disposed: boolean
metamethod __call: function(self: LspReaderWriter, stdin_reader: StdinReader): LspReaderWriter
end
function LspReaderWriter:__init(stdin_reader: StdinReader)
asserts.is_not_nil(stdin_reader)
self._stdin_reader = stdin_reader
self._disposed = false
end
local record HeaderInfo
length:integer
content_type:string
end
local function json_nullable<T>(x: T): T
if x == nil then
return json.null as T
end
return x
end
local contenttype: {string:boolean} = {
["application/vscode-jsonrpc; charset=utf8"] = true,
["application/vscode-jsonrpc; charset=utf-8"] = true,
}
function LspReaderWriter:_parse_header(lines:{string}):HeaderInfo
local len:integer
local content_type:string
for _, line in ipairs(lines) do
local key, val = line:match("^([^:]+): (.+)$")
asserts.that(key ~= nil and val ~= nil, "invalid header: " .. line)
tracing.trace(_module_name, "Request Header: {}: {}", {key, val})
if key == "Content-Length" then
asserts.is_nil(len)
len = tonumber(val) as integer
elseif key == "Content-Type" then
if contenttype[val] == nil then
asserts.fail("Invalid Content-Type '{}'", val)
end
asserts.is_nil(content_type)
content_type = val
else
asserts.fail("Unexpected header: {}", line)
end
end
asserts.that(len ~= nil, "Missing Content-Length")
return {
length = len,
content_type = content_type,
}
end
function LspReaderWriter:initialize()
self._stdout = uv.new_pipe(false)
asserts.that(self._stdout ~= nil)
assert(self._stdout:open(1))
tracing.trace(_module_name, "Opened pipe for stdout")
end
function LspReaderWriter:dispose()
asserts.that(not self._disposed)
self._disposed = true
self._stdout:close()
tracing.debug(_module_name, "Closed pipe for stdout")
end
function LspReaderWriter:_decode_header():HeaderInfo
local header_lines:{string} = {}
tracing.trace(_module_name, "Reading LSP rpc header...")
while true do
local header_line = self._stdin_reader:read_line()
if #header_line == 0 then
break
end
table.insert(header_lines, header_line)
end
return self:_parse_header(header_lines)
end
function LspReaderWriter:receive_rpc():{string:any}
local header_info = self:_decode_header()
tracing.trace(_module_name, "Successfully read LSP rpc header: {}\nWaiting to receive body...", {header_info})
local body_line = self._stdin_reader:read(header_info.length)
tracing.trace(_module_name, "Received request Body: {}", {body_line})
local data = json.decode(body_line)
asserts.that(data and type(data) == 'table', "Malformed json")
asserts.that(data.jsonrpc == "2.0", "Incorrect jsonrpc version! Got {} but expected 2.0", data.jsonrpc)
return data
end
function LspReaderWriter:_encode(t: {string:any})
assert(t.jsonrpc == "2.0", "Expected jsonrpc to be 2.0")
local msg = json.encode(t)
local content = "Content-Length: " .. tostring(#msg) .. "\r\n\r\n" .. msg
assert(self._stdout:write(content))
tracing.trace(_module_name, "Sending data: {}", {content})
end
function LspReaderWriter:send_rpc(id: integer, t: {string:any})
self:_encode {
jsonrpc = "2.0",
id = json_nullable(id),
result = json_nullable(t),
}
end
function LspReaderWriter:send_rpc_error(id: integer, name: lsp.ErrorName, msg: string, data: {string:any})
self:_encode {
jsonrpc = "2.0",
id = json_nullable(id),
error = {
code = lsp.error_code[name] or lsp.error_code.UnknownErrorCode,
message = msg,
data = data,
},
}
end
function LspReaderWriter:send_rpc_notification(method: lsp.Method.Name, params: lsp.Method.Params)
self:_encode {
jsonrpc = "2.0",
method = method,
params = params,
}
end
class.setup(LspReaderWriter, "LspReaderWriter", {
nilable_members = { '_stdout' }
})
return LspReaderWriter