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
|
local gc = require 'gc'
local util = require 'utility'
local proto = require 'proto'
local await = require 'await'
local timer = require 'timer'
require 'provider'
local counter = util.counter()
---@class languageClient
---@field _outs table
---@field _gc gc
---@field _waiting table
---@field _methods table
local mt = {}
mt.__index = mt
function mt:__close()
self:remove()
end
function mt:_fakeProto()
proto.send = function (data)
self._outs[#self._outs+1] = data
end
end
function mt:_flushServer()
-- reset scopes
local ws = require 'workspace'
local scope = require 'workspace.scope'
local files = require 'files'
ws.reset()
scope.reset()
files.reset()
end
---@param callback async fun(client: languageClient)
function mt:start(callback)
self:_fakeProto()
self:_flushServer()
local finished = false
---@async
await.call(function ()
callback(self)
finished = true
end)
local jumpedTime = 0
while true do
if finished then
break
end
if await.step() then
goto CONTINUE
end
timer.update()
if await.step() then
goto CONTINUE
end
if self:update() then
goto CONTINUE
end
timer.timeJump(1.0)
jumpedTime = jumpedTime + 1.0
if jumpedTime > 2 * 60 * 60 then
error('two hours later ...')
end
::CONTINUE::
end
self:remove()
end
function mt:gc(obj)
return self._gc:add(obj)
end
function mt:remove()
self._gc:remove()
end
function mt:notify(method, params)
proto.doMethod {
method = method,
params = params,
}
end
function mt:request(method, params, callback)
local id = counter()
self._waiting[id] = callback
proto.doMethod {
id = id,
method = method,
params = params,
}
end
---@async
function mt:awaitRequest(method, params)
return await.wait(function (waker)
self:request(method, params, waker)
end)
end
function mt:update()
local outs = self._outs
if #outs == 0 then
return false
end
self._outs = {}
for _, out in ipairs(outs) do
if out.method then
local callback = self._methods[out.method]
if callback then
proto.doResponse {
id = out.id,
params = callback(out.params),
}
elseif out.method:sub(1, 2) ~= '$/' then
error('Unknown method: ' .. out.method)
end
else
local callback = self._waiting[out.id]
self._waiting[out.id] = nil
callback(out.result, out.error)
end
end
return true
end
function mt:register(method, callback)
self._methods[method] = callback
end
function mt:registerFakers()
for _, method in ipairs {
'workspace/configuration',
'textDocument/publishDiagnostics',
} do
self:register(method, function ()
return nil
end)
end
end
---@return languageClient
return function ()
local self = setmetatable({
_gc = gc(),
_outs = {},
_waiting = {},
_methods = {},
}, mt)
return self
end
|