feat(float): better key mappings

This commit is contained in:
Folke Lemaitre 2024-11-04 10:01:12 +01:00
parent 66b252535c
commit a171a815b3
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -12,6 +12,11 @@ local M = setmetatable({}, {
end, end,
}) })
---@class snacks.float.Keys: vim.api.keyset.keymap
---@field [1]? string
---@field [2]? string|fun(self: snacks.float): any
---@field mode? string|string[]
---@class snacks.float.Config ---@class snacks.float.Config
---@field buf? number ---@field buf? number
---@field file? string ---@field file? string
@ -135,12 +140,26 @@ function M:show()
end, end,
}) })
for key, fn in pairs(self.opts.keys) do for key, spec in pairs(self.opts.keys) do
if fn then if spec then
fn = type(fn) == "string" and self[fn] or fn if type(spec) == "string" then
vim.keymap.set("n", key, function() spec = { key, self[spec] and self[spec] or spec, desc = spec }
fn(self) elseif type(spec) == "function" then
end, { buffer = self.buf }) spec = { key, spec }
end
local opts = vim.deepcopy(spec)
opts[1] = nil
opts[2] = nil
opts.mode = nil
opts.buffer = self.buf
local rhs = spec[2]
if type(rhs) == "function" then
rhs = function()
return spec[2](self)
end
end
---@cast spec snacks.float.Keys
vim.keymap.set(spec.mode or "n", spec[1], rhs, opts)
end end
end end