fix(lsp.keymaps): make cond -> enabled work again. Closes #6697

This commit is contained in:
Folke Lemaitre
2025-10-26 12:21:22 +01:00
parent 235cadf4cf
commit 1a40162714
2 changed files with 21 additions and 22 deletions

View File

@@ -92,13 +92,13 @@ return {
{ "<leader>cr", vim.lsp.buf.rename, desc = "Rename", has = "rename" },
{ "<leader>cA", LazyVim.lsp.action.source, desc = "Source Action", has = "codeAction" },
{ "]]", function() Snacks.words.jump(vim.v.count1) end, has = "documentHighlight",
desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
desc = "Next Reference", enabled = function() return Snacks.words.is_enabled() end },
{ "[[", function() Snacks.words.jump(-vim.v.count1) end, has = "documentHighlight",
desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
desc = "Prev Reference", enabled = function() return Snacks.words.is_enabled() end },
{ "<a-n>", function() Snacks.words.jump(vim.v.count1, true) end, has = "documentHighlight",
desc = "Next Reference", cond = function() return Snacks.words.is_enabled() end },
desc = "Next Reference", enabled = function() return Snacks.words.is_enabled() end },
{ "<a-p>", function() Snacks.words.jump(-vim.v.count1, true) end, has = "documentHighlight",
desc = "Prev Reference", cond = function() return Snacks.words.is_enabled() end },
desc = "Prev Reference", enabled = function() return Snacks.words.is_enabled() end },
},
},
stylua = { enabled = false },

View File

@@ -3,8 +3,8 @@ local M = {}
---@type LazyKeysLspSpec[]|nil
M._keys = {}
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], cond?:fun():boolean}
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], cond?:fun():boolean}
---@alias LazyKeysLspSpec LazyKeysSpec|{has?:string|string[], enabled?:fun():boolean}
---@alias LazyKeysLsp LazyKeys|{has?:string|string[], enabled?:fun():boolean}
---@deprecated
---@return LazyKeysLspSpec[]
@@ -43,24 +43,23 @@ function M.set(filter, spec)
local Keys = require("lazy.core.handler.keys")
for _, keys in pairs(Keys.resolve(spec)) do
---@cast keys LazyKeysLsp
if keys.cond == nil or keys.cond() then
local filters = {} ---@type vim.lsp.get_clients.Filter[]
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)
filters[#filters + 1] = vim.tbl_extend("force", vim.deepcopy(filter), { method = method })
end
else
filters[#filters + 1] = filter
local filters = {} ---@type vim.lsp.get_clients.Filter[]
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)
filters[#filters + 1] = vim.tbl_extend("force", vim.deepcopy(filter), { method = method })
end
else
filters[#filters + 1] = filter
end
for _, f in ipairs(filters) do
local opts = Keys.opts(keys)
---@cast opts snacks.keymap.set.Opts
opts.lsp = f
Snacks.keymap.set(keys.mode or "n", keys.lhs, keys.rhs, opts)
end
for _, f in ipairs(filters) do
local opts = Keys.opts(keys)
---@cast opts snacks.keymap.set.Opts
opts.lsp = f
opts.enabled = keys.enabled
Snacks.keymap.set(keys.mode or "n", keys.lhs, keys.rhs, opts)
end
end
end