diff --git a/lua/lazyvim/plugins/extras/lang/python.lua b/lua/lazyvim/plugins/extras/lang/python.lua index eb0bd5a8..b4cd10da 100644 --- a/lua/lazyvim/plugins/extras/lang/python.lua +++ b/lua/lazyvim/plugins/extras/lang/python.lua @@ -38,23 +38,8 @@ return { logLevel = "error", }, }, - keys = { - { - "co", - LazyVim.lsp.action["source.organizeImports"], - desc = "Organize Imports", - }, - }, - }, - ruff_lsp = { - keys = { - { - "co", - LazyVim.lsp.action["source.organizeImports"], - desc = "Organize Imports", - }, - }, }, + ruff_lsp = {}, }, setup = { [ruff] = function() diff --git a/lua/lazyvim/plugins/extras/lang/svelte.lua b/lua/lazyvim/plugins/extras/lang/svelte.lua index 4060e2f3..5acfc251 100644 --- a/lua/lazyvim/plugins/extras/lang/svelte.lua +++ b/lua/lazyvim/plugins/extras/lang/svelte.lua @@ -23,15 +23,7 @@ return { "neovim/nvim-lspconfig", opts = { servers = { - svelte = { - keys = { - { - "co", - LazyVim.lsp.action["source.organizeImports"], - desc = "Organize Imports", - }, - }, - }, + svelte = {}, }, }, }, diff --git a/lua/lazyvim/plugins/extras/lang/typescript/init.lua b/lua/lazyvim/plugins/extras/lang/typescript/init.lua index 9bb459c6..ffc3b06f 100644 --- a/lua/lazyvim/plugins/extras/lang/typescript/init.lua +++ b/lua/lazyvim/plugins/extras/lang/typescript/init.lua @@ -56,11 +56,6 @@ return { }, }, keys = { - { - "co", - LazyVim.lsp.action["source.organizeImports"], - desc = "Organize Imports", - }, { "cu", LazyVim.lsp.action["source.removeUnused.ts"], @@ -136,11 +131,6 @@ return { end, desc = "File References", }, - { - "co", - LazyVim.lsp.action["source.organizeImports"], - desc = "Organize Imports", - }, { "cM", LazyVim.lsp.action["source.addMissingImports.ts"], diff --git a/lua/lazyvim/plugins/lsp/init.lua b/lua/lazyvim/plugins/lsp/init.lua index 1a3708e1..a4c7709d 100644 --- a/lua/lazyvim/plugins/lsp/init.lua +++ b/lua/lazyvim/plugins/lsp/init.lua @@ -99,6 +99,12 @@ return { desc = "Next Reference", enabled = function() return Snacks.words.is_enabled() end }, { "", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight", desc = "Prev Reference", enabled = function() return Snacks.words.is_enabled() end }, + { + "co", + LazyVim.lsp.action["source.organizeImports"], + desc = "Organize Imports", + code_action = "source.organizeImports", + }, }, }, stylua = { enabled = false }, diff --git a/lua/lazyvim/plugins/lsp/keymaps.lua b/lua/lazyvim/plugins/lsp/keymaps.lua index 57280055..74fe971b 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} ----@alias LazyKeysLsp LazyKeys|{has?:string|string[], enabled?:fun():boolean} +---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun():boolean), code_action?:string} +---@alias LazyKeysLsp LazyKeys|{has?:string|string[], code_action?:string, enabled?:fun():boolean} ---@deprecated ---@return LazyKeysLspSpec[] @@ -44,7 +44,41 @@ 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.has then + 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 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)