-
Notifications
You must be signed in to change notification settings - Fork 91
Expand file tree
/
Copy pathlanguageserverinstance.jl
More file actions
407 lines (362 loc) · 16.7 KB
/
Copy pathlanguageserverinstance.jl
File metadata and controls
407 lines (362 loc) · 16.7 KB
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
"""
LanguageServerInstance(pipe_in, pipe_out, env="", depot="", err_handler=nothing, symserver_store_path=nothing)
Construct an instance of the language server.
Once the instance is `run`, it will read JSON-RPC from `pipe_out` and
write JSON-RPC from `pipe_in` according to the [language server
specification](https://microsoft.github.io/language-server-protocol/specifications/specification-3-14/).
For normal usage, the language server can be instantiated with
`LanguageServerInstance(stdin, stdout, false, "/path/to/environment")`.
# Arguments
- `pipe_in::IO`: Pipe to read JSON-RPC from.
- `pipe_out::IO`: Pipe to write JSON-RPC to.
- `env::String`: Path to the
[environment](https://docs.julialang.org/en/v1.2/manual/code-loading/#Environments-1)
for which the language server is running. An empty string uses julia's
default environment.
- `depot::String`: Sets the
[`JULIA_DEPOT_PATH`](https://docs.julialang.org/en/v1.2/manual/environment-variables/#JULIA_DEPOT_PATH-1)
where the language server looks for packages required in `env`.
- `err_handler::Union{Nothing,Function}`: If not `nothing`, catch all errors and pass them to an error handler
function with signature `err_handler(err, bt)`. Mostly used for the VS Code crash reporting implementation.
- `symserver_store_path::Union{Nothing,String}`: if `nothing` is passed, the symbol server cash is stored in
a folder in the package. If an absolute path is passed, the symbol server will store the cache files in that
path. The path must exist on disc before this is called.
"""
mutable struct LanguageServerInstance
jr_endpoint::Union{JSONRPC.JSONRPCEndpoint,Nothing}
workspaceFolders::Set{String}
_documents::Dict{URI2,Document}
env_path::String
depot_path::String
symbol_server::SymbolServer.SymbolServerInstance
symbol_results_channel::Channel{Any}
global_env::StaticLint.ExternalEnv
roots_env_map::Dict{Document,StaticLint.ExternalEnv}
symbol_store_ready::Bool
format_options::DocumentFormat.FormatOptions
runlinter::Bool
lint_options::StaticLint.LintOptions
lint_missingrefs::Symbol
lint_disableddirs::Vector{String}
completion_mode::Symbol
combined_msg_queue::Channel{Any}
err_handler::Union{Nothing,Function}
status::Symbol
number_of_outstanding_symserver_requests::Int
symserver_use_download::Bool
current_symserver_progress_token::Union{Nothing,String}
clientcapability_window_workdoneprogress::Bool
clientcapability_workspace_didChangeConfiguration::Bool
# Can probably drop the above 2 and use the below.
clientCapabilities::Union{ClientCapabilities,Missing}
clientInfo::Union{InfoParams,Missing}
shutdown_requested::Bool
function LanguageServerInstance(pipe_in, pipe_out, env_path="", depot_path="", err_handler=nothing, symserver_store_path=nothing, download=true)
new(
JSONRPC.JSONRPCEndpoint(pipe_in, pipe_out, err_handler),
Set{String}(),
Dict{URI2,Document}(),
env_path,
depot_path,
SymbolServer.SymbolServerInstance(depot_path, symserver_store_path),
Channel(Inf),
StaticLint.ExternalEnv(deepcopy(SymbolServer.stdlibs), SymbolServer.collect_extended_methods(SymbolServer.stdlibs), collect(keys(SymbolServer.stdlibs))),
Dict(),
false,
DocumentFormat.FormatOptions(),
true,
StaticLint.LintOptions(),
:all,
LINT_DIABLED_DIRS,
:qualify, # options: :import or :qualify, anything else turns this off
Channel{Any}(Inf),
err_handler,
:created,
0,
download,
nothing,
false,
false,
missing,
missing,
false
)
end
end
function Base.display(server::LanguageServerInstance)
println("Root: ", server.workspaceFolders)
for d in getdocuments_value(server)
display(d)
end
end
function hasdocument(server::LanguageServerInstance, uri::URI2)
return haskey(server._documents, uri)
end
function getdocument(server::LanguageServerInstance, uri::URI2)
return server._documents[uri]
end
function getdocuments_key(server::LanguageServerInstance)
return keys(server._documents)
end
function getdocuments_pair(server::LanguageServerInstance)
return pairs(server._documents)
end
function getdocuments_value(server::LanguageServerInstance)
return values(server._documents)
end
function setdocument!(server::LanguageServerInstance, uri::URI2, doc::Document)
server._documents[uri] = doc
end
function deletedocument!(server::LanguageServerInstance, uri::URI2)
doc = getdocument(server, uri)
StaticLint.clear_meta(getcst(doc))
delete!(server._documents, uri)
for d in getdocuments_value(server)
if getroot(d) === doc
setroot(d, d)
semantic_pass(getroot(d))
end
end
end
function create_symserver_progress_ui(server)
if server.clientcapability_window_workdoneprogress
token = string(uuid4())
server.current_symserver_progress_token = token
JSONRPC.send(server.jr_endpoint, window_workDoneProgress_create_request_type, WorkDoneProgressCreateParams(token))
JSONRPC.send(
server.jr_endpoint,
progress_notification_type,
ProgressParams(token, WorkDoneProgressBegin("Julia Language Server", missing, "Indexing packages...", missing))
)
end
end
function destroy_symserver_progress_ui(server)
if server.clientcapability_window_workdoneprogress && server.current_symserver_progress_token !== nothing
progress_token = server.current_symserver_progress_token
server.current_symserver_progress_token = nothing
JSONRPC.send(
server.jr_endpoint,
progress_notification_type,
ProgressParams(progress_token, WorkDoneProgressEnd(missing))
)
end
end
function trigger_symbolstore_reload(server::LanguageServerInstance)
server.symbol_store_ready = false
if server.number_of_outstanding_symserver_requests == 0 && server.status == :running
create_symserver_progress_ui(server)
end
server.number_of_outstanding_symserver_requests += 1
if server.symserver_use_download
@debug "Will download symbol server caches for this instance."
end
@async try
# TODO Add try catch handler that links into crash reporting
ssi_ret, payload = SymbolServer.getstore(
server.symbol_server,
server.env_path,
function (i)
if server.clientcapability_window_workdoneprogress && server.current_symserver_progress_token !== nothing
JSONRPC.send(
server.jr_endpoint,
progress_notification_type,
ProgressParams(server.current_symserver_progress_token, WorkDoneProgressReport(missing, "Indexing $i...", missing))
)
else
@info "Indexing $i..."
end
end,
server.err_handler,
download = server.symserver_use_download
)
server.number_of_outstanding_symserver_requests -= 1
if server.number_of_outstanding_symserver_requests == 0
destroy_symserver_progress_ui(server)
end
if ssi_ret == :success
push!(server.symbol_results_channel, payload)
elseif ssi_ret == :failure
error_payload = Dict(
"command" => "symserv_crash",
"name" => "LSSymbolServerFailure",
"message" => payload === nothing ? "" : String(take!(payload)),
"stacktrace" => "")
JSONRPC.send(
server.jr_endpoint,
telemetry_event_notification_type,
error_payload
)
elseif ssi_ret == :package_load_crash
error_payload = Dict(
"command" => "symserv_pkgload_crash",
"name" => payload.package_name,
"message" => payload.stderr === nothing ? "" : String(take!(payload.stderr)))
JSONRPC.send(
server.jr_endpoint,
telemetry_event_notification_type,
error_payload
)
end
server.symbol_store_ready = true
catch err
bt = catch_backtrace()
if server.err_handler !== nothing
server.err_handler(err, bt)
else
Base.display_error(stderr, err, bt)
end
end
end
function request_wrapper(func, server::LanguageServerInstance)
return function (conn, params)
if server.shutdown_requested
# it's fine to always return a value here, even for notifications, because
# JSONRPC discards it anyways in that case
return JSONRPC.JSONRPCError(
-32600,
"LS shutdown was requested.",
nothing
)
end
func(params, server, conn)
end
end
"""
run(server::LanguageServerInstance)
Run the language `server`.
"""
function Base.run(server::LanguageServerInstance)
server.status = :started
run(server.jr_endpoint)
trigger_symbolstore_reload(server)
@async try
@debug "LS: Starting client listener task."
while true
msg = JSONRPC.get_next_message(server.jr_endpoint)
put!(server.combined_msg_queue, (type = :clientmsg, msg = msg))
end
catch err
bt = catch_backtrace()
if server.err_handler !== nothing
server.err_handler(err, bt)
else
io = IOBuffer()
Base.display_error(io, err, bt)
print(stderr, String(take!(io)))
end
finally
if isopen(server.combined_msg_queue)
put!(server.combined_msg_queue, (type = :close,))
close(server.combined_msg_queue)
end
@debug "LS: Client listener task done."
end
@async try
@debug "LS: Starting symbol server listener task."
while true
msg = take!(server.symbol_results_channel)
put!(server.combined_msg_queue, (type = :symservmsg, msg = msg))
end
catch err
bt = catch_backtrace()
if server.err_handler !== nothing
server.err_handler(err, bt)
else
io = IOBuffer()
Base.display_error(io, err, bt)
print(stderr, String(take!(io)))
end
finally
if isopen(server.combined_msg_queue)
put!(server.combined_msg_queue, (type = :close,))
close(server.combined_msg_queue)
end
@debug "LS: Symbol server listener task done."
end
msg_dispatcher = JSONRPC.MsgDispatcher()
msg_dispatcher[textDocument_codeAction_request_type] = request_wrapper(textDocument_codeAction_request, server)
msg_dispatcher[workspace_executeCommand_request_type] = request_wrapper(workspace_executeCommand_request, server)
msg_dispatcher[textDocument_completion_request_type] = request_wrapper(textDocument_completion_request, server)
msg_dispatcher[textDocument_signatureHelp_request_type] = request_wrapper(textDocument_signatureHelp_request, server)
msg_dispatcher[textDocument_definition_request_type] = request_wrapper(textDocument_definition_request, server)
msg_dispatcher[textDocument_formatting_request_type] = request_wrapper(textDocument_formatting_request, server)
msg_dispatcher[textDocument_references_request_type] = request_wrapper(textDocument_references_request, server)
msg_dispatcher[textDocument_rename_request_type] = request_wrapper(textDocument_rename_request, server)
msg_dispatcher[textDocument_prepareRename_request_type] = request_wrapper(textDocument_prepareRename_request, server)
msg_dispatcher[textDocument_documentSymbol_request_type] = request_wrapper(textDocument_documentSymbol_request, server)
msg_dispatcher[textDocument_documentHighlight_request_type] = request_wrapper(textDocument_documentHighlight_request, server)
msg_dispatcher[textDocument_semanticTokens_request_type] = request_wrapper(textDocument_semanticTokens_request, server)
msg_dispatcher[textDocument_semanticTokens_full_request_type] = request_wrapper(textDocument_semanticTokens_full_request, server)
msg_dispatcher[julia_getModuleAt_request_type] = request_wrapper(julia_getModuleAt_request, server)
msg_dispatcher[julia_getDocAt_request_type] = request_wrapper(julia_getDocAt_request, server)
msg_dispatcher[textDocument_hover_request_type] = request_wrapper(textDocument_hover_request, server)
msg_dispatcher[initialize_request_type] = request_wrapper(initialize_request, server)
msg_dispatcher[initialized_notification_type] = request_wrapper(initialized_notification, server)
msg_dispatcher[shutdown_request_type] = request_wrapper(shutdown_request, server)
msg_dispatcher[exit_notification_type] = request_wrapper(exit_notification, server)
msg_dispatcher[cancel_notification_type] = request_wrapper(cancel_notification, server)
msg_dispatcher[setTrace_notification_type] = request_wrapper(setTrace_notification, server)
msg_dispatcher[setTraceNotification_notification_type] = request_wrapper(setTraceNotification_notification, server)
msg_dispatcher[julia_getCurrentBlockRange_request_type] = request_wrapper(julia_getCurrentBlockRange_request, server)
msg_dispatcher[julia_activateenvironment_notification_type] = request_wrapper(julia_activateenvironment_notification, server)
msg_dispatcher[textDocument_didOpen_notification_type] = request_wrapper(textDocument_didOpen_notification, server)
msg_dispatcher[textDocument_didClose_notification_type] = request_wrapper(textDocument_didClose_notification, server)
msg_dispatcher[textDocument_didSave_notification_type] = request_wrapper(textDocument_didSave_notification, server)
msg_dispatcher[textDocument_willSave_notification_type] = request_wrapper(textDocument_willSave_notification, server)
msg_dispatcher[textDocument_willSaveWaitUntil_request_type] = request_wrapper(textDocument_willSaveWaitUntil_request, server)
msg_dispatcher[textDocument_didChange_notification_type] = request_wrapper(textDocument_didChange_notification, server)
msg_dispatcher[workspace_didChangeWatchedFiles_notification_type] = request_wrapper(workspace_didChangeWatchedFiles_notification, server)
msg_dispatcher[workspace_didChangeConfiguration_notification_type] = request_wrapper(workspace_didChangeConfiguration_notification, server)
msg_dispatcher[workspace_didChangeWorkspaceFolders_notification_type] = request_wrapper(workspace_didChangeWorkspaceFolders_notification, server)
msg_dispatcher[workspace_symbol_request_type] = request_wrapper(workspace_symbol_request, server)
msg_dispatcher[julia_refreshLanguageServer_notification_type] = request_wrapper(julia_refreshLanguageServer_notification, server)
msg_dispatcher[julia_getDocFromWord_request_type] = request_wrapper(julia_getDocFromWord_request, server)
msg_dispatcher[textDocument_selectionRange_request_type] = request_wrapper(textDocument_selectionRange_request, server)
while true
message = take!(server.combined_msg_queue)
if message.type == :close
@info "Shutting down server instance."
return
elseif message.type == :clientmsg
msg = message.msg
JSONRPC.dispatch_msg(server.jr_endpoint, msg_dispatcher, msg)
elseif message.type == :symservmsg
@info "Received new data from Julia Symbol Server."
server.global_env.symbols = message.msg
server.global_env.extended_methods = SymbolServer.collect_extended_methods(server.global_env.symbols)
server.global_env.project_deps = collect(keys(server.global_env.symbols))
# redo roots_env_map
for (root, _) in server.roots_env_map
@debug "resetting get_env_for_root"
newenv = get_env_for_root(root, server)
if newenv === nothing
delete!(server.roots_env_map, root)
else
server.roots_env_map[root] = newenv
end
end
@debug "starting re-lint of everything"
relintserver(server)
@debug "re-lint done"
end
end
end
function relintserver(server)
roots = Set{Document}()
documents = getdocuments_value(server)
for doc in documents
StaticLint.clear_meta(getcst(doc))
set_doc(getcst(doc), doc)
end
for doc in documents
# only do a pass on documents once
root = getroot(doc)
if !(root in roots)
push!(roots, root)
semantic_pass(root)
end
end
for doc in documents
lint!(doc, server)
end
end