feat(lsp): show server name in code actions #30830

Problem:
If there are multiple LSP clients, it's not clear which actions are provided by which servers. #30710

Solution:
Show the server name next to each action (only if there are multiple clients).
This commit is contained in:
Jordan 2024-10-17 09:35:19 +01:00 committed by GitHub
parent e265363a88
commit ce678043e3
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 13 additions and 3 deletions

View File

@ -182,6 +182,8 @@ LSP
• |vim.lsp.buf.format()| now supports passing a list of ranges • |vim.lsp.buf.format()| now supports passing a list of ranges
via the `range` parameter (this requires support for the via the `range` parameter (this requires support for the
`textDocument/rangesFormatting` request). `textDocument/rangesFormatting` request).
• |vim.lsp.buf.code_action()| actions show client name when there are multiple
clients.
LUA LUA

View File

@ -827,11 +827,19 @@ local function on_code_action_results(results, opts)
return return
end end
---@param item {action: lsp.Command|lsp.CodeAction} ---@param item {action: lsp.Command|lsp.CodeAction, ctx: lsp.HandlerContext}
local function format_item(item) local function format_item(item)
local title = item.action.title:gsub('\r\n', '\\r\\n') local clients = vim.lsp.get_clients({ bufnr = item.ctx.bufnr })
return title:gsub('\n', '\\n') local title = item.action.title:gsub('\r\n', '\\r\\n'):gsub('\n', '\\n')
if #clients == 1 then
return title
end
local source = vim.lsp.get_client_by_id(item.ctx.client_id).name
return ('%s [%s]'):format(title, source)
end end
local select_opts = { local select_opts = {
prompt = 'Code actions:', prompt = 'Code actions:',
kind = 'codeaction', kind = 'codeaction',