Skip to content

Commit

Permalink
fix(api): maps.nop behaviour improved.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed May 31, 2024
1 parent d5e5597 commit 2e19c26
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
5 changes: 1 addition & 4 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,8 @@ local desc = Kmap.desc

_G.is_windows = vim_has('win32')

-- WARNING: DO NOT CHANGE ANYTHING BUT THE FIRST PARAMETER
-- Keymaps will break for yet unknown reasons.
--
-- Set `<Space>` as Leader Key.
nop('<Space>', User.maps.map.desc('Leader Key', true, true))
nop('<Space>', desc('Leader Key', true, nil, true))
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

Expand Down
46 changes: 44 additions & 2 deletions lua/user/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,44 @@ local bufmap = vim.api.nvim_buf_set_keymap
---@type Modes
local MODES = { 'n', 'i', 'v', 't', 'o', 'x' }

---@type fun(T: UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts, fields: string|string[]): UserMaps.Api.Opts|UserMaps.Keymap.Opts|UserMaps.Buf.Opts
local strip_options = function(T, fields)
if not is_tbl(T) then
error('(maps:strip_options): Empty table')
end

if empty(T) then
return T
end

if not (is_str(fields) or is_tbl(fields)) or empty(fields) then
return T
end

---@type UserMaps.Keymap.Opts
local res = {}

if is_str(fields) then
if not field(fields, T) then
return T
end

for k, v in next, T do
if k ~= fields then
res[k] = v
end
end
else
for k, v in next, T do
if not vim.tbl_contains(fields, k) then
res[k] = v
end
end
end

return res
end

---@type fun(mode: string, func: MapFuncs, with_buf: boolean?): KeyMapFunction|ApiMapFunction|BufMapFunction
local function variant(mode, func, with_buf)
if not (is_fun(func) and is_str(mode) and vim.tbl_contains(MODES, mode)) then
Expand Down Expand Up @@ -138,11 +176,15 @@ function M.nop(T, opts, mode)

opts.silent = is_bool(opts.silent) and opts.silent or true

if is_int(opts.buffer) then
opts = strip_options(vim.deepcopy(opts), 'buffer')
end

if is_str(T) then
M.map[mode](T, '<Nop>', opts)
M.kmap[mode](T, '<Nop>', opts)
else
for _, v in next, T do
M.map[mode](v, '<Nop>', opts)
M.kmap[mode](v, '<Nop>', opts)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lua/user/types/user/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,6 @@ require('user.types.which_key')
---@field kmap UserMaps.Keymap
---@field map UserMaps.Api
---@field buf_map UserMaps.Buf
---@field nop fun(T: string|string[], opts: UserMaps.Api.Opts?, mode: MapModes?)
---@field nop fun(T: string|string[], opts: UserMaps.Keymap.Opts?, mode: MapModes?)
---@field wk UserMaps.WK
---@field modes Modes

0 comments on commit 2e19c26

Please sign in to comment.