fix(lsp): use vim.notify with action-less showMessage requests (#34720)

This commit is contained in:
Maria José Solano
2025-07-01 10:13:58 -07:00
committed by GitHub
parent 38aac21083
commit 807bc00dd6

View File

@ -28,6 +28,22 @@ local function err_message(...)
api.nvim_command('redraw')
end
--- @param params lsp.ShowMessageParams|lsp.ShowMessageRequestParams
--- @param ctx lsp.HandlerContext
local function show_message_notification(params, ctx)
local message_type = params.type
local message = params.message
local client_id = ctx.client_id
local client = vim.lsp.get_client_by_id(client_id)
local client_name = client and client.name or string.format('id=%d', client_id)
if not client then
err_message('LSP[', client_name, '] client has shut down after sending ', message)
end
message = ('LSP[%s] %s'):format(client_name, message)
vim.notify(message, log._from_lsp_level(message_type))
return params
end
--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#workspace_executeCommand
RCS[ms.workspace_executeCommand] = function(_, _, _)
-- Error handling is done implicitly by wrapping all handlers; see end of this file
@ -85,8 +101,8 @@ end
--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessageRequest
---@param params lsp.ShowMessageRequestParams
RSC[ms.window_showMessageRequest] = function(_, params)
local actions = params.actions or {}
RSC[ms.window_showMessageRequest] = function(_, params, ctx)
if next(params.actions or {}) then
local co, is_main = coroutine.running()
if co and not is_main then
local opts = {
@ -96,7 +112,7 @@ RSC[ms.window_showMessageRequest] = function(_, params)
return (action.title:gsub('\r\n', '\\r\\n')):gsub('\n', '\\n')
end,
}
vim.ui.select(actions, opts, function(choice)
vim.ui.select(params.actions, opts, function(choice)
-- schedule to ensure resume doesn't happen _before_ yield with
-- default synchronous vim.ui.select
vim.schedule(function()
@ -106,18 +122,23 @@ RSC[ms.window_showMessageRequest] = function(_, params)
return coroutine.yield()
else
local option_strings = { params.message, '\nRequest Actions:' }
for i, action in ipairs(actions) do
for i, action in ipairs(params.actions) do
local title = action.title:gsub('\r\n', '\\r\\n')
title = title:gsub('\n', '\\n')
table.insert(option_strings, string.format('%d. %s', i, title))
end
local choice = vim.fn.inputlist(option_strings)
if choice < 1 or choice > #actions then
if choice < 1 or choice > #params.actions then
return vim.NIL
else
return actions[choice]
return params.actions[choice]
end
end
else
-- No actions, just show the message.
show_message_notification(params, ctx)
return vim.NIL
end
end
--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#client_registerCapability
@ -580,17 +601,7 @@ end
--- @see # https://microsoft.github.io/language-server-protocol/specifications/specification-current/#window_showMessage
--- @param params lsp.ShowMessageParams
NSC['window/showMessage'] = function(_, params, ctx)
local message_type = params.type
local message = params.message
local client_id = ctx.client_id
local client = vim.lsp.get_client_by_id(client_id)
local client_name = client and client.name or string.format('id=%d', client_id)
if not client then
err_message('LSP[', client_name, '] client has shut down after sending ', message)
end
message = ('LSP[%s] %s'):format(client_name, message)
vim.notify(message, log._from_lsp_level(message_type))
return params
return show_message_notification(params, ctx)
end
--- @private