From 76cb567e5c84500bc070755def9a7aa10c74ed20 Mon Sep 17 00:00:00 2001 From: Folke Lemaitre Date: Fri, 20 Mar 2026 19:01:02 +0100 Subject: [PATCH] refactor(lsp): cleaner support for code_actions --- lua/lazyvim/plugins/lsp/init.lua | 8 +++++- lua/lazyvim/plugins/lsp/keymaps.lua | 40 +++-------------------------- lua/lazyvim/util/lsp.lua | 17 ++++++++++++ 3 files changed, 27 insertions(+), 38 deletions(-) diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index a4c7709d..9a790b21 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -103,7 +103,13 @@ return { "co", LazyVim.lsp.action["source.organizeImports"], desc = "Organize Imports", - code_action = "source.organizeImports", + has = "codeAction", + enabled = function(buf) + local code_actions = vim.tbl_filter(function(action) + return action:find("^source%.organizeImports%.?$") + end, LazyVim.lsp.code_actions({ bufnr = buf })) + return #code_actions > 0 + end }, }, }, diff --git a/lua/lazyvim/plugins/lsp/keymaps.lua b/lua/lazyvim/plugins/lsp/keymaps.lua index 74fe971b..f4059471 100644 --- a/lua/lazyvim/plugins/lsp/keymaps.lua +++ b/lua/lazyvim/plugins/lsp/keymaps.lua @@ -3,8 +3,8 @@ local M = {} ---@type LazyKeysLspSpec[]|nil M._keys = {} ----@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun():boolean), code_action?:string} ----@alias LazyKeysLsp LazyKeys|{has?:string|string[], code_action?:string, enabled?:fun():boolean} +---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun(buf:number):boolean)} +---@alias LazyKeysLsp LazyKeys|{has?:string|string[], enabled?:fun(buf:number):boolean} ---@deprecated ---@return LazyKeysLspSpec[] @@ -44,41 +44,7 @@ function M.set(filter, spec) for _, keys in pairs(Keys.resolve(spec)) do ---@cast keys LazyKeysLsp local filters = {} ---@type vim.lsp.get_clients.Filter[] - if keys.code_action then - filter = vim.tbl_extend("force", vim.deepcopy(filter), { method = "textDocument/codeAction" }) - filters[#filters + 1] = filter - - ---@param kinds? string[] - local function has(kinds) - for _, k in ipairs(kinds or {}) do - if type(k) == "string" and vim.startswith(k, keys.code_action) then - return true - end - end - end - - local enabled = keys.enabled - keys.enabled = function() - if type(enabled) == "function" and not enabled() then - return false - end - local clients = vim.lsp.get_clients(filter) - for _, client in ipairs(clients) do - -- check server cababilities first - if has(vim.tbl_get(client, "server_capabilities", "codeActionProvider", "codeActionKinds")) then - return true - end - -- check dynamic capabilities - local regs = client.dynamic_capabilities:get("codeActionProvider") - for _, reg in ipairs(regs or {}) do - if has(vim.tbl_get(reg, "registerOptions", "codeActionKinds")) then - return true - end - end - end - return false - end - elseif keys.has then + if keys.has then local methods = type(keys.has) == "string" and { keys.has } or keys.has --[[@as string[] ]] for _, method in ipairs(methods) do method = method:find("/") and method or ("textDocument/" .. method) diff --git a/lua/lazyvim/util/lsp.lua b/lua/lazyvim/util/lsp.lua index ff248599..f667b12f 100644 --- a/lua/lazyvim/util/lsp.lua +++ b/lua/lazyvim/util/lsp.lua @@ -93,4 +93,21 @@ function M.execute(opts) end end +---@param filter? vim.lsp.get_clients.Filter +function M.code_actions(filter) + filter = filter or {} + local ret = {} ---@type string[] + local clients = vim.lsp.get_clients(filter) + for _, client in ipairs(clients) do + -- check server cababilities first + vim.list_extend(ret, vim.tbl_get(client, "server_capabilities", "codeActionProvider", "codeActionKinds") or {}) + -- check dynamic capabilities + local regs = client.dynamic_capabilities:get("codeActionProvider", filter) + for _, reg in ipairs(regs or {}) do + vim.list_extend(ret, vim.tbl_get(reg, "registerOptions", "codeActionKinds") or {}) + end + end + return LazyVim.dedup(ret) +end + return M