mirror of
https://github.com/neovim/neovim.git
synced 2025-09-07 03:48:18 +00:00
feat(lsp): run handler in coroutine to support async response (#21026)
To illustrate a use-case this also changes `window/showMessageRequest` to use `vim.ui.select`
This commit is contained in:

committed by
GitHub

parent
0958dccc6d
commit
af204dd0f1
@@ -81,17 +81,32 @@ M['window/workDoneProgress/create'] = function(_, result, ctx)
|
|||||||
end
|
end
|
||||||
|
|
||||||
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessageRequest
|
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessageRequest
|
||||||
|
---@param result lsp.ShowMessageRequestParams
|
||||||
M['window/showMessageRequest'] = function(_, result)
|
M['window/showMessageRequest'] = function(_, result)
|
||||||
local actions = result.actions
|
local actions = result.actions or {}
|
||||||
print(result.message)
|
local co, is_main = coroutine.running()
|
||||||
|
if co and not is_main then
|
||||||
|
local opts = {
|
||||||
|
prompt = result.message .. ': ',
|
||||||
|
format_item = function(action)
|
||||||
|
return (action.title:gsub('\r\n', '\\r\\n')):gsub('\n', '\\n')
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
vim.ui.select(actions, opts, function(choice)
|
||||||
|
-- schedule to ensure resume doesn't happen _before_ yield with
|
||||||
|
-- default synchronous vim.ui.select
|
||||||
|
vim.schedule(function()
|
||||||
|
coroutine.resume(co, choice or vim.NIL)
|
||||||
|
end)
|
||||||
|
end)
|
||||||
|
return coroutine.yield()
|
||||||
|
else
|
||||||
local option_strings = { result.message, '\nRequest Actions:' }
|
local option_strings = { result.message, '\nRequest Actions:' }
|
||||||
for i, action in ipairs(actions) do
|
for i, action in ipairs(actions) do
|
||||||
local title = action.title:gsub('\r\n', '\\r\\n')
|
local title = action.title:gsub('\r\n', '\\r\\n')
|
||||||
title = title:gsub('\n', '\\n')
|
title = title:gsub('\n', '\\n')
|
||||||
table.insert(option_strings, string.format('%d. %s', i, title))
|
table.insert(option_strings, string.format('%d. %s', i, title))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- window/showMessageRequest can return either MessageActionItem[] or null.
|
|
||||||
local choice = vim.fn.inputlist(option_strings)
|
local choice = vim.fn.inputlist(option_strings)
|
||||||
if choice < 1 or choice > #actions then
|
if choice < 1 or choice > #actions then
|
||||||
return vim.NIL
|
return vim.NIL
|
||||||
@@ -99,6 +114,7 @@ M['window/showMessageRequest'] = function(_, result)
|
|||||||
return actions[choice]
|
return actions[choice]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_registerCapability
|
--see: https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_registerCapability
|
||||||
M['client/registerCapability'] = function(_, _, ctx)
|
M['client/registerCapability'] = function(_, _, ctx)
|
||||||
|
@@ -20,6 +20,14 @@ function transform_schema_to_table()
|
|||||||
end
|
end
|
||||||
--]=]
|
--]=]
|
||||||
|
|
||||||
|
---@class lsp.ShowMessageRequestParams
|
||||||
|
---@field type lsp.MessageType
|
||||||
|
---@field message string
|
||||||
|
---@field actions nil|lsp.MessageActionItem[]
|
||||||
|
|
||||||
|
---@class lsp.MessageActionItem
|
||||||
|
---@field title string
|
||||||
|
|
||||||
local constants = {
|
local constants = {
|
||||||
DiagnosticSeverity = {
|
DiagnosticSeverity = {
|
||||||
-- Reports an error.
|
-- Reports an error.
|
||||||
@@ -39,6 +47,7 @@ local constants = {
|
|||||||
Deprecated = 2,
|
Deprecated = 2,
|
||||||
},
|
},
|
||||||
|
|
||||||
|
---@enum lsp.MessageType
|
||||||
MessageType = {
|
MessageType = {
|
||||||
-- An error message.
|
-- An error message.
|
||||||
Error = 1,
|
Error = 1,
|
||||||
|
@@ -391,6 +391,7 @@ function Client:handle_body(body)
|
|||||||
-- Schedule here so that the users functions don't trigger an error and
|
-- Schedule here so that the users functions don't trigger an error and
|
||||||
-- we can still use the result.
|
-- we can still use the result.
|
||||||
schedule(function()
|
schedule(function()
|
||||||
|
coroutine.wrap(function()
|
||||||
local status, result
|
local status, result
|
||||||
status, result, err = self:try_call(
|
status, result, err = self:try_call(
|
||||||
client_errors.SERVER_REQUEST_HANDLER_ERROR,
|
client_errors.SERVER_REQUEST_HANDLER_ERROR,
|
||||||
@@ -429,6 +430,7 @@ function Client:handle_body(body)
|
|||||||
result = nil
|
result = nil
|
||||||
end
|
end
|
||||||
self:send_response(decoded.id, err, result)
|
self:send_response(decoded.id, err, result)
|
||||||
|
end)()
|
||||||
end)
|
end)
|
||||||
-- This works because we are expecting vim.NIL here
|
-- This works because we are expecting vim.NIL here
|
||||||
elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then
|
elseif decoded.id and (decoded.result ~= vim.NIL or decoded.error ~= vim.NIL) then
|
||||||
|
Reference in New Issue
Block a user