Skip to content

Commit

Permalink
fix(api): maps refactored and new check function
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed May 28, 2024
1 parent 424b860 commit ce71d26
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions lua/user/maps.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@
---@diagnostic disable:unused-function

require('user.types.user.maps')
require('user.types.user.check')

---@type UserCheck
local Check = require('user.check')

local is_nil = Check.value.is_nil
Expand All @@ -15,6 +13,7 @@ local is_num = Check.value.is_num
local is_int = Check.value.is_int
local is_bool = Check.value.is_bool
local empty = Check.value.empty
local field = Check.value.field

local kmap = vim.keymap.set
local map = vim.api.nvim_set_keymap
Expand Down Expand Up @@ -64,20 +63,20 @@ local function variant(mode, func, with_buf)
return res
end

---@type fun(field: 'api'|'key'|'buf'): UserKeyMaps|UserApiMaps|UserBufMaps
local mode_funcs = function(field)
---@type table<'api'|'key'|'buf', { integer: fun(...), integer: boolean }>
---@type fun(key: 'api'|'key'|'buf'): UserKeyMaps|UserApiMaps|UserBufMaps
local mode_funcs = function(key)
---@type table<'api'|'key'|'buf', { integer: fun(), integer: boolean }>
local VALID = { api = { map, false }, key = { kmap, false }, buf = { bufmap, true } }

if is_nil(VALID[field]) then
if not field(key, VALID) then
error('(user.maps:mode_funcs): Invalid variant ID `' .. field .. "`\nMust be `'api'|'key'|'buf'`")
end

---@type UserKeyMaps|UserApiMaps|UserBufMaps
local res = {}

for _, mode in next, MODES do
res[mode] = variant(mode, VALID[field][1], VALID[field][2])
res[mode] = variant(mode, VALID[key][1], VALID[key][2])
end

return res
Expand All @@ -100,8 +99,9 @@ function M.kmap.desc(msg, silent, bufnr, noremap, nowait)
nowait = is_bool(nowait) and nowait or true,
}
end
for _, field in next, { M.map, M.buf_map } do
function field.desc(msg, silent, noremap, nowait)

for _, key in next, { M.map, M.buf_map } do
function key.desc(msg, silent, noremap, nowait)
return {
desc = (is_str(msg) and not empty(msg)) and msg or 'Unnamed Key',
silent = is_bool(silent) and silent or true,
Expand All @@ -116,8 +116,6 @@ function M.nop(T, opts, mode)
error('(user.maps.nop): Field is neither a string nor a table.')
end

local map_tbl = M.map

mode = (is_str(mode) and vim.tbl_contains(M.modes, mode)) and mode or 'n'
if mode == 'i' then
return
Expand All @@ -132,10 +130,10 @@ function M.nop(T, opts, mode)
opts.silent = is_bool(opts.silent) and opts.silent or true

if is_str(T) then
map_tbl[mode](T, '<Nop>', opts)
M.map[mode](T, '<Nop>', opts)
else
for _, v in next, T do
map_tbl[mode](v, '<Nop>', opts)
M.map[mode](v, '<Nop>', opts)
end
end
end
Expand Down

0 comments on commit ce71d26

Please sign in to comment.