dotfiles/nvim/lua/config/helpers.lua

41 lines
1 KiB
Lua
Raw Normal View History

2023-09-02 22:09:00 +02:00
local M = {}
2024-01-21 23:32:15 +01:00
2023-09-02 22:09:00 +02:00
2024-01-23 22:31:09 +01:00
local defaults = { noremap = true, silent = true }
2023-09-05 18:33:45 +02:00
---@class KeymapOpts
2023-09-05 18:33:45 +02:00
---@field [1]? string Shorthand description
---@field desc? string Description
---@field [2]? number|boolean Shorthand buffer
---@field buffer? number|boolean Buffer
2023-09-02 22:09:00 +02:00
---Wrapper around `vim.keymap.set`
---@param modes string|string[]
2023-09-04 23:01:56 +02:00
function M.keymap(modes)
2023-09-02 22:09:00 +02:00
---@param lhs string
return function(lhs)
---@param rhs string|function
return function(rhs)
---@param opts string|KeymapOpts
2023-09-02 22:09:00 +02:00
return function(opts)
2024-01-23 22:23:58 +01:00
-- Either 'string' or 'table'
2023-09-05 18:33:45 +02:00
local type = type(opts)
2024-01-23 22:23:58 +01:00
---@type table
2023-09-02 22:09:00 +02:00
local options
2024-01-21 23:32:15 +01:00
if type == 'string' then
options = vim.tbl_extend('force', defaults, {desc=opts})
elseif type == 'table' then
opts.desc = opts[1] or opts.desc
opts.buffer = opts[2] or opts.buffer
opts[1], opts[2] = nil, nil
2024-01-21 23:32:15 +01:00
options = vim.tbl_extend('force', defaults, opts)
2023-09-02 22:09:00 +02:00
end
vim.keymap.set(modes, lhs, rhs, options)
end
end
end
end
2023-12-24 22:54:03 +01:00
2023-09-02 22:09:00 +02:00
return M