Skip to content

Commit

Permalink
Merge pull request #1340 from julia-vscode/update-jsonrpc-v2
Browse files Browse the repository at this point in the history
Update to JSONRPC.jl v2
  • Loading branch information
davidanthoff authored Jan 9, 2025
2 parents fdcd7bf + 86b0543 commit 910211e
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Dates = "ade2ca70-3891-5945-98fb-dc099432e06a"
[compat]
CSTParser = "3.3"
JSON = "0.20, 0.21"
JSONRPC = "1.1"
JSONRPC = "2"
JuliaFormatter = "1"
PrecompileTools = "1"
StaticLint = "8.0"
Expand Down
52 changes: 38 additions & 14 deletions src/languageserverinstance.jl
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,30 @@ end
const USE_REVISE = Ref(false)

function request_wrapper(func, server::LanguageServerInstance)
return function (conn, params, token)
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
if USE_REVISE[] && isdefined(Main, :Revise)
try
Main.Revise.revise()
catch e
@warn "Reloading with Revise failed" exception = e
end
Base.invokelatest(func, params, server, conn)
else
func(params, server, conn)
end
end
end

function notification_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
Expand Down Expand Up @@ -394,24 +418,24 @@ function Base.run(server::LanguageServerInstance; timings = [])
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[initialized_notification_type] = notification_wrapper(initialized_notification, server)
msg_dispatcher[shutdown_request_type] = request_wrapper(shutdown_request, 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[cancel_notification_type] = notification_wrapper(cancel_notification, server)
msg_dispatcher[setTrace_notification_type] = notification_wrapper(setTrace_notification, server)
msg_dispatcher[setTraceNotification_notification_type] = notification_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[julia_activateenvironment_notification_type] = notification_wrapper(julia_activateenvironment_notification, server)
msg_dispatcher[textDocument_didOpen_notification_type] = notification_wrapper(textDocument_didOpen_notification, server)
msg_dispatcher[textDocument_didClose_notification_type] = notification_wrapper(textDocument_didClose_notification, server)
msg_dispatcher[textDocument_didSave_notification_type] = notification_wrapper(textDocument_didSave_notification, server)
msg_dispatcher[textDocument_willSave_notification_type] = notification_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[textDocument_didChange_notification_type] = notification_wrapper(textDocument_didChange_notification, server)
msg_dispatcher[workspace_didChangeWatchedFiles_notification_type] = notification_wrapper(workspace_didChangeWatchedFiles_notification, server)
msg_dispatcher[workspace_didChangeConfiguration_notification_type] = notification_wrapper(workspace_didChangeConfiguration_notification, server)
msg_dispatcher[workspace_didChangeWorkspaceFolders_notification_type] = notification_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_refreshLanguageServer_notification_type] = notification_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)
msg_dispatcher[textDocument_documentLink_request_type] = request_wrapper(textDocument_documentLink_request, server)
Expand Down
2 changes: 1 addition & 1 deletion src/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ end
end

# some timer utilities
add_timer_message!(did_show_timer, timings, msg::Dict) = add_timer_message!(did_show_timer, timings, string("LSP/", get(msg, "method", "")))
add_timer_message!(did_show_timer, timings, msg::JSONRPC.Request) = add_timer_message!(did_show_timer, timings, string("LSP/", msg.method))
function add_timer_message!(did_show_timer, timings, msg::String)
if did_show_timer[]
return
Expand Down
8 changes: 1 addition & 7 deletions test/test_communication.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,7 @@
}
""")

if Sys.iswindows()
global_socket_name = "\\\\.\\pipe\\julia-language-server-testrun"
elseif Sys.isunix()
global_socket_name = joinpath(tempdir(), "julia-language-server-testrun")
else
error("Unknown operating system.")
end
global_socket_name = JSONRPC.generate_pipe_name()

@async try
server = listen(global_socket_name)
Expand Down

0 comments on commit 910211e

Please sign in to comment.