refactor: improve add_keymap function to handle multiple keybind formats

This commit is contained in:
2024-08-31 02:58:02 -04:00
parent 2676cf5157
commit af77292f5f
+66 -30
View File
@@ -184,49 +184,85 @@ function M.is_installed(plugin)
end end
-- Helper function to add keymaps with common properties -- Helper function to add keymaps with common properties
--- Helper function to add keymaps with common properties
---@param lhs string|table The keybind (or list of keybinds)
---@param rhs string|function The function to execute when the key is pressed
---@param desc string|nil The description of the keybind (optional)
---@param mode string|table|nil The mode(s) in which the keybind should be added (optional)
---@param icon string|nil The icon to use for the keybind (optional)
---@param group string|nil The group to add the keybind to (optional)
---@return boolean condition true if the keybind was added, false otherwise
function M.add_keymap(lhs, rhs, desc, mode, icon, group) function M.add_keymap(lhs, rhs, desc, mode, icon, group)
-- Check if which-key.nvim is installed -- Check if which-key.nvim is installed
if M.is_installed("which-key.nvim") then if M.is_installed("which-key.nvim") then
require("which-key").add({ require("which-key").add({
-- stylua: ignore start -- stylua: ignore start
{ {
lhs, -- The keybind lhs, -- The keybind
rhs = rhs, -- Function to execute when the key is pressed rhs = rhs, -- Function to execute when the key is pressed
desc = desc, -- Description of the keybind desc = desc, -- Description of the keybind
mode = mode or "n", -- Default to "n" (normal mode) if mode is not provided mode = mode or "n", -- Default to "n" (normal mode) if mode is not provided
icon = icon, -- Icon to use for the keybind icon = icon, -- Icon to use for the keybind
group = group, -- Default to nil (no group) if group is not provided group = group, -- Default to nil (no group) if group is not provided
}, },
-- stylua: ignore end -- stylua: ignore end
}) })
return true
else else
-- Handle the case where lhs is a table -- Handle the case where lhs is a table
if type(lhs) == "table" then if type(lhs) == "table" then
rhs = lhs.rhs or rhs -- If lhs is a list of keymaps, iterate over each item
desc = lhs.desc or desc for _, keymap in ipairs(lhs) do
mode = lhs.mode or mode -- Set default values or use provided ones
icon = lhs.icon or icon local keymap_rhs = keymap.rhs or rhs
group = lhs.group or group local keymap_desc = keymap.desc or desc
lhs = lhs.lhs or lhs local keymap_mode = keymap.mode or mode or "n"
end local keymap_icon = keymap.icon or icon
-- If which-key.nvim is not installed, use vim.keymap.set local keymap_group = keymap.group or group
if not group and not icon then local keymap_lhs = keymap.lhs
-- stylua: ignore start
vim.keymap.set( if not keymap_group and not keymap_icon then
mode or "n", -- Default to "n" (normal mode) if mode is not provided -- Apply the keymap using vim.keymap.set
lhs, -- The keybind vim.keymap.set(
rhs, -- Function to execute when the key is pressed keymap_mode, -- Mode(s) in which the keybind should be added
{ desc = desc } -- Description of the keybind keymap_lhs, -- The keybind
) keymap_rhs, -- Function to execute when the key is pressed
-- stylua: ignore end { desc = keymap_desc } -- Description of the keybind
)
else
-- If which-key.nvim is not installed, use vim.notify with a detailed error message
local msg = string.format(
"Mapping requires which-key.nvim:\n%s%s",
vim.inspect(keymap_lhs), -- Convert lhs to a readable format
keymap_desc and ("\nDescription: " .. keymap_desc) or ""
)
M.notify(msg, "WARN")
return false
end
end
return true
else else
-- If which-key.nvim is not installed, use vim.notify with detailed error message -- If lhs is a single keymap (not a table)
local msg = string.format( if not group and not icon then
"Mapping requires which-key.nvim:\n%s%s", -- stylua: ignore start
vim.inspect(lhs), -- Convert lhs to a readable format vim.keymap.set(
desc and ("\nDescription: " .. desc) or "" mode or "n", -- Default to "n" (normal mode) if mode is not provided
) lhs, -- The keybind
M.notify(msg, "WARN") rhs, -- Function to execute when the key is pressed
{ desc = desc } -- Description of the keybind
)
-- stylua: ignore end
return true
else
-- If which-key.nvim is not installed, use vim.notify with a detailed error message
local msg = string.format(
"Mapping requires which-key.nvim:\n%s%s",
vim.inspect(lhs), -- Convert lhs to a readable format
desc and ("\nDescription: " .. desc) or ""
)
M.notify(msg, "WARN")
return false
end
end end
end end
end end