mirror of
https://github.com/LazyVim/LazyVim.git
synced 2026-07-21 20:11:06 +00:00
refactor(lsp): cleaner support for code_actions
This commit is contained in:
parent
53f4eabd77
commit
76cb567e5c
3 changed files with 27 additions and 38 deletions
|
|
@ -103,7 +103,13 @@ return {
|
||||||
"<leader>co",
|
"<leader>co",
|
||||||
LazyVim.lsp.action["source.organizeImports"],
|
LazyVim.lsp.action["source.organizeImports"],
|
||||||
desc = "Organize Imports",
|
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
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@ local M = {}
|
||||||
---@type LazyKeysLspSpec[]|nil
|
---@type LazyKeysLspSpec[]|nil
|
||||||
M._keys = {}
|
M._keys = {}
|
||||||
|
|
||||||
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun():boolean), code_action?:string}
|
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:(fun(buf:number):boolean)}
|
||||||
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], code_action?:string, enabled?:fun():boolean}
|
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], enabled?:fun(buf:number):boolean}
|
||||||
|
|
||||||
---@deprecated
|
---@deprecated
|
||||||
---@return LazyKeysLspSpec[]
|
---@return LazyKeysLspSpec[]
|
||||||
|
|
@ -44,41 +44,7 @@ function M.set(filter, spec)
|
||||||
for _, keys in pairs(Keys.resolve(spec)) do
|
for _, keys in pairs(Keys.resolve(spec)) do
|
||||||
---@cast keys LazyKeysLsp
|
---@cast keys LazyKeysLsp
|
||||||
local filters = {} ---@type vim.lsp.get_clients.Filter[]
|
local filters = {} ---@type vim.lsp.get_clients.Filter[]
|
||||||
if keys.code_action then
|
if keys.has 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[] ]]
|
local methods = type(keys.has) == "string" and { keys.has } or keys.has --[[@as string[] ]]
|
||||||
for _, method in ipairs(methods) do
|
for _, method in ipairs(methods) do
|
||||||
method = method:find("/") and method or ("textDocument/" .. method)
|
method = method:find("/") and method or ("textDocument/" .. method)
|
||||||
|
|
|
||||||
|
|
@ -93,4 +93,21 @@ function M.execute(opts)
|
||||||
end
|
end
|
||||||
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
|
return M
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue