diff --git a/lua/data/func.lua b/lua/data/func.lua index 07e6ae5..889ad47 100644 --- a/lua/data/func.lua +++ b/lua/data/func.lua @@ -741,151 +741,6 @@ function M.add_keymap( end end ----@function Add a new keymap with optional which-key integration ----@param lhs string|table The keybind or a table containing multiple keymaps ----@param rhs string|function|nil The function to execute when the keybind is pressed ----@param desc string|nil The description of the keybind ----@param mode string|table|nil The mode(s) in which the keybind should be added ----@param icon string|nil The icon to associate with the which-key entry (optional) ----@param group string|nil The which-key group to add the keybind to (optional) ----@param bufnr number|nil The buffer number to add the keymap to (optional, for buffer-specific mappings) ----@return boolean success True if all keymaps were successfully added, false otherwise -function M.add_km(lhs, rhs, desc, mode, icon, group, bufnr) - local function is_mode(value) - -- Identify common vim modes; this can be expanded based on requirements - local common_modes = - { n = true, i = true, v = true, x = true, s = true, o = true, c = true } - if type(value) == "string" and #value == 1 then - return common_modes[value] ~= nil - elseif type(value) == "table" then - for _, v in ipairs(value) do - if not common_modes[v] then - return false - end - end - return true - end - return false - end - - local function correct_swapped_params() - -- Try to correct common mistakes where rhs and mode might be swapped - - if type(rhs) == "string" and is_mode(rhs) then - if type(mode) == "string" or type(mode) == "function" then - -- Swap them if it looks like rhs is mode and mode is rhs - ---@diagnostic disable-next-line: cast-local-type - rhs, mode = mode, rhs - elseif type(mode) == "table" and is_mode(mode) then - -- Swap if rhs is mode and mode is list of modes - ---@diagnostic disable-next-line: cast-local-type - rhs, mode = mode, rhs - end - elseif - type(rhs) == "function" - and type(mode) == "string" - and not is_mode(mode) - then - -- This checks if mode is actually an rhs-like string or command - rhs, mode = mode, rhs - end - end - - -- Attempt to correct any common parameter swapping issues - correct_swapped_params() - - -- Helper function to set keymaps using native functions - local function set_keymap( - keymap_lhs, - keymap_rhs, - keymap_desc, - keymap_mode, - keymap_bufnr - ) - if keymap_bufnr then - vim.api.nvim_buf_set_keymap( - keymap_bufnr, - keymap_mode, - keymap_lhs, - keymap_rhs, - { desc = keymap_desc } - ) - else - vim.keymap.set( - keymap_mode, - keymap_lhs, - keymap_rhs, - { desc = keymap_desc } - ) - end - end - - -- Determine if which-key is available and should be used - local use_which_key = M.is_installed - and M.is_installed("which-key.nvim") - and not bufnr - - -- Process a single keymap entry - ---@function Helper function to process a single keymap - ---@param keymap table The table containing a keymap - ---@return boolean success True if the keymap was processed successfully, false otherwise - local function process_keymap(keymap) - local keymap_lhs = type(keymap) == "table" and keymap.lhs or lhs - local keymap_rhs = type(keymap) == "table" and (keymap.rhs or rhs) or rhs - local keymap_desc = type(keymap) == "table" and (keymap.desc or desc) - or desc - local keymap_mode = type(keymap) == "table" and (keymap.mode or mode) - or mode - or "n" - local keymap_icon = type(keymap) == "table" and (keymap.icon or icon) - or icon - local keymap_group = type(keymap) == "table" and (keymap.group or group) - or group - - -- Use which-key if possible; fallback to native keymap functions if not - if use_which_key then - require("which-key").add({ - [keymap_lhs] = { - keymap_rhs, - keymap_desc, - mode = keymap_mode, - icon = keymap_icon, - group = keymap_group, - }, - }) - else - -- Attempt to fall back to the native keymap functions - local success, err_msg = pcall( - set_keymap, - keymap_lhs, - keymap_rhs, - keymap_desc, - keymap_mode, - bufnr - ) - if not success then - -- Notify the user about the failed mapping - M.notify(("Failed to map %s: %s"):format(keymap_lhs, err_msg), "WARN") - return false - end - end - return true - end - - -- Handle the scenario where lhs is a table containing multiple keymaps - if type(lhs) == "table" then - for _, keymap in ipairs(lhs) do - if not process_keymap(keymap) then - return false - end - end - return true - else - -- Handle a single keymap entry - return process_keymap({}) - end -end - ---@function Helper function to remove keymaps ---@param lhs string The keybind ---@param mode string|nil The mode in which the keybind should be removed (optional)