Skip to content

Commit

Permalink
refactor: Use maps.wk for mapping keys.
Browse files Browse the repository at this point in the history
  • Loading branch information
DrKJeff16 committed May 29, 2024
1 parent bf9cbdb commit 26dcf6c
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 25 deletions.
32 changes: 11 additions & 21 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ local Check = User.check
local Types = User.types -- Import docstrings and annotations.
local maps_t = Types.user.maps
local Kmap = User.maps.kmap
local WK = User.maps.wk

local exists = Check.exists.module -- Checks for missing modules
local is_nil = Check.value.is_nil
Expand All @@ -15,9 +16,10 @@ local is_fun = Check.value.is_fun
local empty = Check.value.empty
local nop = User.maps.nop
local desc = Kmap.desc
local register = WK.register

-- Set `<Space>` as Leader Key.
nop('<Space>', { noremap = true, desc = 'Leader Key' })
nop('<Space>', User.maps.map.desc('Leader Key', true, true))
vim.g.mapleader = ' '
vim.g.maplocalleader = ' '

Expand Down Expand Up @@ -181,29 +183,17 @@ local map_tbl = {
},
}

-- Set the keymaps previously stated.
for mode, t in next, map_tbl do
local func = Kmap[mode]

for lhs, v in next, t do
if not (is_fun(v[1]) or is_str(v[1])) then
error('(init.lua): Could not process keymap `' .. lhs .. '`')
end

v[2] = is_tbl(v[2]) and v[2] or {}

func(lhs, v[1], v[2])
end
end

if not called_lazy then
-- List of manually-callable plugins.
_G.Pkg = require('lazy_cfg')
_G.called_lazy = true
end

local wk_t = User.types.which_key
local reg = Pkg.wk.reg
-- Set the keymaps previously stated
for mode, t in next, map_tbl do
local wk_maps = WK.convert_dict(t)
register(wk_maps, { mode = mode })
end

---@type fun(T: CscSubMod|ODSubMod): boolean
local function color_exists(T)
Expand All @@ -214,7 +204,7 @@ if is_tbl(Pkg.colorschemes) and not empty(Pkg.colorschemes) then
-- A table containing various possible colorschemes.
local Csc = Pkg.colorschemes

---@type table<string, RegKey>
---@type RegKeys
local CscKeys = {}

---@type ('nightfox'|'tokyonight'|'catppuccin'|'onedark'|'spaceduck'|'molokai'|'dracula'|'oak')[]
Expand All @@ -240,8 +230,8 @@ if is_tbl(Pkg.colorschemes) and not empty(Pkg.colorschemes) then
end
end

reg({ ['<leader>vc'] = { name = '+Colorschemes' } })
reg(CscKeys)
register({ ['<leader>vc'] = { name = '+Colorschemes' } })
register(CscKeys)

if not empty(found_csc) then
Csc[selected[found_csc]].setup()
Expand Down
10 changes: 6 additions & 4 deletions lua/lazy_cfg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -844,7 +844,6 @@ Lazy.setup(T)
---@type LazyMods
local P = {
colorschemes = require('lazy_cfg.colorschemes'),
wk = require('lazy_cfg.which_key'),
}

---@type fun(cmd: 'ed'|'tabnew'|'split'|'vsplit'): fun()
Expand All @@ -867,15 +866,18 @@ local Keys = {
['<leader>Let'] = { key_variant('tabnew'), desc('Open `Lazy` File Tab') },
['<leader>Lev'] = { key_variant('vsplit'), desc('Open `Lazy`File Vertical Window') },
['<leader>Ll'] = { Lazy.show, desc('Show Lazy Home') },
['<leader>LL'] = { ':Lazy ', desc('Select `Lazy` Operation (Interactively)', false) },
['<leader>Ls'] = { Lazy.sync, desc('Sync Lazy Plugins') },
['<leader>Lx'] = { Lazy.clear, desc('Clear Lazy Plugins') },
['<leader>Lc'] = { Lazy.check, desc('Check Lazy Plugins') },
['<leader>Li'] = { Lazy.install, desc('Install Lazy Plugins') },
['<leader>Lr'] = { Lazy.reload, desc('Reload Lazy Plugins') },
['<leader>LL'] = { ':Lazy ', desc('Select `Lazy` Operation (Interactively)', false) },
}

for lhs, v in next, Keys do
local Keys_WK = User.maps.wk.convert_dict(Keys)
User.maps.wk.register(Keys_WK, { mode = 'n' })

--[[ for lhs, v in next, Keys do
local msg = '(lazy_cfg): Could not set keymap `' .. lhs .. '`'
if not (is_str(v[1]) or is_fun(v[1])) then
vim.notify(msg)
Expand All @@ -886,6 +888,6 @@ for lhs, v in next, Keys do
nmap(lhs, v[1], v[2])
::continue::
end
end ]]

return P

0 comments on commit 26dcf6c

Please sign in to comment.