From 3884d676cc3823db36c11c463258e08450aea62a Mon Sep 17 00:00:00 2001 From: Guennadi Maximov C Date: Wed, 29 May 2024 09:23:35 -0600 Subject: [PATCH] refactor: Partial keymap overhaul. I have successfully integrated `which_key` conditionally. Now it's a question to integrate it to the whole config. --- init.lua | 87 +++++++++-- lua/lazy_cfg/init.lua | 72 +++++---- lua/lazy_cfg/lspconfig/init.lua | 119 +++++++++------ lua/lazy_cfg/todo_comments/init.lua | 119 ++++++++------- lua/lazy_cfg/which_key/register/init.lua | 186 +---------------------- lua/user/maps.lua | 22 ++- lua/user/types/user/maps.lua | 1 + lua/user/types/which_key.lua | 13 +- 8 files changed, 278 insertions(+), 341 deletions(-) diff --git a/init.lua b/init.lua index c4c47324..45256396 100644 --- a/init.lua +++ b/init.lua @@ -126,10 +126,10 @@ local map_tbl = { }, ['fvV'] = { ':so ', desc('Source VimScript File (Interactively)', false) }, ['fvL'] = { ':luafile ', desc('Source Lua File (Interactively)', false) }, - ['fvet'] = { ':tabnew $MYVIMRC' }, - ['fvee'] = { ':ed $MYVIMRC' }, - ['fves'] = { ':split $MYVIMRC' }, - ['fvev'] = { ':vsplit $MYVIMRC' }, + ['fvet'] = { ':tabnew $MYVIMRC', desc('Open In New Tab') }, + ['fvee'] = { ':ed $MYVIMRC', desc('Open In Current Window') }, + ['fves'] = { ':split $MYVIMRC', desc('Open In Horizontal Split') }, + ['fvev'] = { ':vsplit $MYVIMRC', desc('Open In Vertical Split') }, ['vh'] = { ':checkhealth', desc('Run Checkhealth', false) }, @@ -147,8 +147,8 @@ local map_tbl = { ['wsS'] = { ':split ', desc('Horizontal Split (Interactively)', false) }, ['wsV'] = { ':vsplit ', desc('Vertical Split (Interactively)', false) }, - ['qq'] = { ':qa' }, - ['qQ'] = { ':qa!' }, + ['qq'] = { ':qa', desc('Quit Nvim') }, + ['qQ'] = { ':qa!', desc('Quit Nvim Forcefully') }, ['tn'] = { ':tabN', desc('Next Tab', false) }, ['tp'] = { ':tabp', desc('Previous Tab', false) }, @@ -177,12 +177,48 @@ local map_tbl = { ['r'] = { ':s/', desc('Run Search-Replace Interactively', false) }, ['ir'] = { ':%retab', desc('Retab Selection') }, }, - t = { - -- Escape terminl by pressing `` - [''] = { '', { noremap = true } }, +} +---@type table +local Names = { + n = { + -- File Handling + ['f'] = { name = '+File' }, + --- Source File Handling + ['fv'] = { name = '+Vim Files' }, + --- `init.lua` Editing + ['fve'] = { name = '+Edit `init.lua`' }, + + -- Tabs Handling + ['t'] = { name = '+Tabs' }, + + -- Buffer Handling + ['b'] = { name = '+Buffer' }, + + -- Window Handling + ['w'] = { name = '+Window' }, + + -- Window Splitting + ['ws'] = { name = '+Split' }, + + -- Exiting + ['q'] = { name = '+Quit Nvim' }, + + -- Help + ['h'] = { name = '+Help' }, + + -- Session + ['S'] = { name = '+Session' }, + + -- Vim + ['v'] = { name = '+Vim' }, + }, + v = { + ['i'] = { name = '+Indent' }, }, } +Kmap.t('', '') + if not called_lazy then -- List of manually-callable plugins. _G.Pkg = require('lazy_cfg') @@ -191,8 +227,18 @@ end -- 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 }) + if WK.available() then + if is_tbl(Names[mode]) and not empty(Names[mode]) then + register(Names[mode], { mode = mode }) + end + + register(WK.convert_dict(t), { mode = mode }) + else + for lhs, v in next, t do + v[2] = is_tbl(v[2]) and v[2] or {} + Kmap[mode](lhs, v[1], v[2]) + end + end end ---@type fun(T: CscSubMod|ODSubMod): boolean @@ -204,7 +250,7 @@ if is_tbl(Pkg.colorschemes) and not empty(Pkg.colorschemes) then -- A table containing various possible colorschemes. local Csc = Pkg.colorschemes - ---@type RegKeys + ---@type KeyMapDict local CscKeys = {} ---@type ('nightfox'|'tokyonight'|'catppuccin'|'onedark'|'spaceduck'|'molokai'|'dracula'|'oak')[] @@ -225,13 +271,24 @@ if is_tbl(Pkg.colorschemes) and not empty(Pkg.colorschemes) then for _, c in next, selected do if color_exists(Csc[c]) then found_csc = empty(found_csc) and i or found_csc - CscKeys['vc' .. tostring(i)] = { Csc[c].setup, 'Setup Colorscheme `' .. c .. '`' } + CscKeys['vc' .. tostring(i)] = { Csc[c].setup, desc('Setup Colorscheme `' .. c .. '`') } i = i + 1 end end - register({ ['vc'] = { name = '+Colorschemes' } }) - register(CscKeys) + if WK.available() then + register({ ['vc'] = { name = '+Colorschemes' } }, { mode = 'n' }) + register(WK.convert_dict(CscKeys), { mode = 'n' }) + + register({ ['vc'] = { name = '+Colorschemes' } }, { mode = 'v' }) + register(WK.convert_dict(CscKeys), { mode = 'v' }) + else + for lhs, v in next, CscKeys do + v[2] = is_tbl(v[2]) and v[2] or {} + Kmap.n(lhs, v[1], v[2]) + Kmap.v(lhs, v[1], v[2]) + end + end if not empty(found_csc) then Csc[selected[found_csc]].setup() diff --git a/lua/lazy_cfg/init.lua b/lua/lazy_cfg/init.lua index eb46a10e..980d462c 100644 --- a/lua/lazy_cfg/init.lua +++ b/lua/lazy_cfg/init.lua @@ -4,6 +4,8 @@ local User = require('user') local Check = User.check local types = User.types.lazy +local kmap = User.maps.kmap +local WK = User.maps.wk local exists = Check.exists.module local executable = Check.exists.executable @@ -14,8 +16,7 @@ local is_fun = Check.value.is_fun local is_tbl = Check.value.is_tbl local empty = Check.value.empty local in_console = Check.in_console -local nmap = User.maps.kmap.n -local desc = User.maps.kmap.desc +local desc = kmap.desc local fs_stat = vim.uv.fs_stat local stdpath = vim.fn.stdpath @@ -859,35 +860,52 @@ local key_variant = function(cmd) end end ----@type KeyMapDict +---@type table local Keys = { - ['Lee'] = { key_variant('ed'), desc('Open `Lazy` File') }, - ['Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') }, - ['Let'] = { key_variant('tabnew'), desc('Open `Lazy` File Tab') }, - ['Lev'] = { key_variant('vsplit'), desc('Open `Lazy`File Vertical Window') }, - ['Ll'] = { Lazy.show, desc('Show Lazy Home') }, - ['Ls'] = { Lazy.sync, desc('Sync Lazy Plugins') }, - ['Lx'] = { Lazy.clear, desc('Clear Lazy Plugins') }, - ['Lc'] = { Lazy.check, desc('Check Lazy Plugins') }, - ['Li'] = { Lazy.install, desc('Install Lazy Plugins') }, - ['Lr'] = { Lazy.reload, desc('Reload Lazy Plugins') }, - ['LL'] = { ':Lazy ', desc('Select `Lazy` Operation (Interactively)', false) }, + n = { + ['Lee'] = { key_variant('ed'), desc('Open `Lazy` File') }, + ['Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') }, + ['Let'] = { key_variant('tabnew'), desc('Open `Lazy` File Tab') }, + ['Lev'] = { key_variant('vsplit'), desc('Open `Lazy`File Vertical Window') }, + ['Ll'] = { Lazy.show, desc('Show Lazy Home') }, + ['Ls'] = { Lazy.sync, desc('Sync Lazy Plugins') }, + ['Lx'] = { Lazy.clear, desc('Clear Lazy Plugins') }, + ['Lc'] = { Lazy.check, desc('Check Lazy Plugins') }, + ['Li'] = { Lazy.install, desc('Install Lazy Plugins') }, + ['Lr'] = { Lazy.reload, desc('Reload Lazy Plugins') }, + ['LL'] = { ':Lazy ', desc('Select `Lazy` Operation (Interactively)', false) }, + }, + v = { + ['Lee'] = { key_variant('ed'), desc('Open `Lazy` File') }, + ['Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') }, + ['Let'] = { key_variant('tabnew'), desc('Open `Lazy` File Tab') }, + ['Lev'] = { key_variant('vsplit'), desc('Open `Lazy`File Vertical Window') }, + ['Ll'] = { Lazy.show, desc('Show Lazy Home') }, + ['Ls'] = { Lazy.sync, desc('Sync Lazy Plugins') }, + ['Lx'] = { Lazy.clear, desc('Clear Lazy Plugins') }, + ['Lc'] = { Lazy.check, desc('Check Lazy Plugins') }, + ['Li'] = { Lazy.install, desc('Install Lazy Plugins') }, + ['Lr'] = { Lazy.reload, desc('Reload Lazy Plugins') }, + }, } -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) - goto continue - end +for mode, maps in next, Keys do + if WK.available() then + WK.register(WK.convert_dict(maps), { mode = mode }) + else + for lhs, v in next, maps 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) + goto continue + end - v[2] = is_tbl(v[2]) and v[2] or {} - nmap(lhs, v[1], v[2]) + v[2] = is_tbl(v[2]) and v[2] or {} + kmap[mode](lhs, v[1], v[2]) - ::continue:: -end ]] + ::continue:: + end + end +end return P diff --git a/lua/lazy_cfg/lspconfig/init.lua b/lua/lazy_cfg/lspconfig/init.lua index 4239fbdf..b48631e4 100644 --- a/lua/lazy_cfg/lspconfig/init.lua +++ b/lua/lazy_cfg/lspconfig/init.lua @@ -199,13 +199,15 @@ local Keys = { }, } -local Keys_WK = WK.convert_dict(Keys) -WK.register(Keys_WK) - ---[[ for lhs, v in next, Keys do - v[2] = is_tbl(v[2]) and v[2] or {} - nmap(lhs, v[1], v[2]) -end ]] +if WK.available() then + WK.register({ [''] = { name = '+LSP' } }) + WK.register(WK.convert_dict(Keys)) +else + for lhs, v in next, Keys do + v[2] = is_tbl(v[2]) and v[2] or {} + nmap(lhs, v[1], v[2]) + end +end au('LspAttach', { group = augroup('UserLspConfig', { clear = true }), @@ -216,49 +218,76 @@ au('LspAttach', { bo[buf].omnifunc = 'v:lua.lsp.omnifunc' + ---@type table local K = { - ['lgD'] = { lsp_buf.declaration, desc('Declaration', true, buf) }, - ['lgd'] = { lsp_buf.definition, desc('Definition', true, buf) }, - ['lk'] = { lsp_buf.hover, desc('Hover', true, buf) }, - ['K'] = { lsp_buf.hover, desc('Hover', true, buf) }, - ['lgi'] = { lsp_buf.implementation, desc('Implementation', true, buf) }, - ['lS'] = { lsp_buf.signature_help, desc('Signature Help', true, buf) }, - ['lwa'] = { lsp_buf.add_workspace_folder, desc('Add Workspace Folder', true, buf) }, - ['lwr'] = { lsp_buf.remove_workspace_folder, desc('Remove Workspace Folder', true, buf) }, - ['lwl'] = { - function() - local out = lsp_buf.list_workspace_folders() - local msg = '' - for _, v in next, out do - msg = msg .. '\n - ' .. v - end - - -- Try doing it with `notify` plugin. - if exists('notify') then - local Notify = require('notify') - - Notify(msg, 'info', { title = 'Workspace Folders' }) - else - vim.notify(msg, vim.log.levels.INFO) - end - end, - desc('List Workspace Folders', true, buf), + n = { + ['lfD'] = { lsp_buf.declaration, desc('Declaration', true, buf) }, + ['lfd'] = { lsp_buf.definition, desc('Definition', true, buf) }, + ['lk'] = { lsp_buf.hover, desc('Hover', true, buf) }, + ['K'] = { lsp_buf.hover, desc('Hover', true, buf) }, + ['lfi'] = { lsp_buf.implementation, desc('Implementation', true, buf) }, + ['lfS'] = { lsp_buf.signature_help, desc('Signature Help', true, buf) }, + ['lwa'] = { lsp_buf.add_workspace_folder, desc('Add Workspace Folder', true, buf) }, + ['lwr'] = { lsp_buf.remove_workspace_folder, desc('Remove Workspace Folder', true, buf) }, + ['lwl'] = { + function() + local out = lsp_buf.list_workspace_folders() + local msg = '' + for _, v in next, out do + msg = msg .. '\n - ' .. v + end + + -- Try doing it with `notify` plugin. + if exists('notify') then + local Notify = require('notify') + + Notify(msg, 'info', { title = 'Workspace Folders' }) + else + vim.notify(msg, vim.log.levels.INFO) + end + end, + desc('List Workspace Folders', true, buf), + }, + ['lfT'] = { lsp_buf.type_definition, desc('Type Definition', true, buf) }, + ['lfR'] = { lsp_buf.rename, desc('Rename...', true, buf) }, + ['lfr'] = { lsp_buf.references, desc('References', true, buf) }, + ['lff'] = { + function() + lsp_buf.format({ async = true }) + end, + desc('Format File', true, buf), + }, + ['lca'] = { lsp_buf.code_action, desc('Code Actions', true, buf) }, }, - ['lD'] = { lsp_buf.type_definition, desc('Type Definition', true, buf) }, - ['lrn'] = { lsp_buf.rename, desc('Rename...', true, buf) }, - ['lgr'] = { lsp_buf.references, desc('References', true, buf) }, - ['lf'] = { - function() - lsp_buf.format({ async = true }) - end, - desc('Format File', true, buf), + v = { + ['lca'] = { lsp_buf.code_action, desc('Code Actions', true, buf) }, + }, + } + ---@type table + local Names = { + n = { + ['lc'] = { name = '+Code Actions' }, + ['lw'] = { name = '+Workspace' }, + ['lf'] = { name = '+File Analysis' }, + }, + v = { + ['lc'] = { name = '+Code Actions' }, }, } - local K2 = WK.convert_dict(K) - WK.register(K2) - - vim.keymap.set({ 'n', 'v' }, 'lca', lsp_buf.code_action, desc('Code Actions', true, buf)) + for mode, keys in next, K do + if WK.available() then + if is_tbl(Names[mode]) and not empty(Names[mode]) then + WK.register(Names[mode]) + end + WK.register(WK.convert_dict(keys), { mode = mode }) + else + for lhs, v in next, keys do + v[2] = is_tbl(v[2]) and v[2] or {} + kmap[mode](lhs, v[1], v[2]) + end + end + end end, }) diff --git a/lua/lazy_cfg/todo_comments/init.lua b/lua/lazy_cfg/todo_comments/init.lua index 8db84292..a6f2b1de 100644 --- a/lua/lazy_cfg/todo_comments/init.lua +++ b/lua/lazy_cfg/todo_comments/init.lua @@ -5,13 +5,14 @@ local User = require('user') local Check = User.check local maps_t = User.types.user.maps local kmap = User.maps.kmap +local WK = User.maps.wk local exists = Check.exists.module local executable = Check.exists.executable local is_str = Check.value.is_str local is_tbl = Check.value.is_tbl local is_fun = Check.value.is_fun -local nmap = kmap.n +local empty = Check.value.empty if not exists('todo-comments') then return @@ -131,65 +132,73 @@ Todo.setup({ }, }) ----@type KeyMapDict -local maps = { - -- `TODO` - ['ctn'] = { - function() - Todo.jump_next() - end, - { desc = "Next 'TODO' Comment" }, - }, - ['ctp'] = { - function() - Todo.jump_prev() - end, - { desc = "Previous 'TODO' Comment" }, - }, +---@type table +local Keys = { + n = { + -- `TODO` + ['ctn'] = { + function() + Todo.jump_next() + end, + { desc = "Next 'TODO' Comment" }, + }, + ['ctp'] = { + function() + Todo.jump_prev() + end, + { desc = "Previous 'TODO' Comment" }, + }, - -- `ERROR` - ['cen'] = { - function() - Todo.jump_next({ keywords = { 'ERROR' } }) - end, - { desc = "Next 'ERROR' Comment" }, - }, - ['cep'] = { - function() - Todo.jump_prev({ keywords = { 'ERROR' } }) - end, - { desc = "Previous 'ERROR' Comment" }, - }, + -- `ERROR` + ['cen'] = { + function() + Todo.jump_next({ keywords = { 'ERROR' } }) + end, + { desc = "Next 'ERROR' Comment" }, + }, + ['cep'] = { + function() + Todo.jump_prev({ keywords = { 'ERROR' } }) + end, + { desc = "Previous 'ERROR' Comment" }, + }, - -- `WARNING` - ['cwn'] = { - function() - Todo.jump_next({ keywords = { 'WARNING' } }) - end, - { desc = "Next 'WARNING' Comment" }, - }, - ['cwp'] = { - function() - Todo.jump_prev({ keywords = { 'WARNING' } }) - end, - { desc = "Previous 'WARNING' Comment" }, + -- `WARNING` + ['cwn'] = { + function() + Todo.jump_next({ keywords = { 'WARNING' } }) + end, + { desc = "Next 'WARNING' Comment" }, + }, + ['cwp'] = { + function() + Todo.jump_prev({ keywords = { 'WARNING' } }) + end, + { desc = "Previous 'WARNING' Comment" }, + }, }, } -for lhs, t in next, maps do - if not is_fun(t[1]) and not is_str(t[1]) then - goto continue - end - local rhs = t[1] - - ---@type vim.keymap.set.Opts - local opts = {} +---@type table +local Names = { + n = { + ['c'] = { name = '+TODO Comments' }, + ['cw'] = { name = "+'WARNING'" }, + ['ce'] = { name = "+'ERROR'" }, + ['ct'] = { name = "+'TODO'" }, + }, +} - if is_tbl(t[2]) then - opts = t[2] +for mode, keys in next, Keys do + if WK.available() then + if is_tbl(Names[mode]) and not empty(Names[mode]) then + WK.register(Names[mode], { mode = mode }) + end + WK.register(WK.convert_dict(keys), { mode = mode }) + else + for lhs, v in next, keys do + v[2] = is_tbl(v[2]) and v[2] or {} + kmap[mode](lhs, v[1], v[2]) + end end - - nmap(lhs, rhs, opts) - - ::continue:: end diff --git a/lua/lazy_cfg/which_key/register/init.lua b/lua/lazy_cfg/which_key/register/init.lua index c72d91e9..eb938e7b 100644 --- a/lua/lazy_cfg/which_key/register/init.lua +++ b/lua/lazy_cfg/which_key/register/init.lua @@ -53,147 +53,10 @@ local function reg(maps, opts) end ---@type RegKeysNamed -local Names = { - -- File Handling - ['f'] = { name = '+File' }, - --- Source File Handling - ['fv'] = { name = '+Vim Files' }, - --- `init.lua` Editing - ['fve'] = { name = '+Edit `init.lua`' }, - - -- Tabs Handling - ['t'] = { name = '+Tabs' }, - - -- Buffer Handling - ['b'] = { name = '+Buffer' }, - - -- Window Handling - ['w'] = { name = '+Window' }, - - -- Window Splitting - ['ws'] = { name = '+Split' }, - - -- Exiting - ['q'] = { name = '+Quit Nvim' }, - - -- Help - ['h'] = { name = '+Help' }, - - -- Session - ['S'] = { name = '+Session' }, - - -- Vim - ['v'] = { name = '+Vim' }, -} +local Names = {} ---@type RegKeys -local Regs = { - -- File Handling - ['fs'] = { - 'w', - 'Save File', - }, - --- Source File Handling - ['fvs'] = { - 'luafile $MYVIMRC', - "Source Neovim's `init.lua`", - }, - --- `init.lua` Editing - ['fvee'] = { - 'ed $MYVIMRC', - 'Open `$MYVIMRC`', - }, - ['fvet'] = { - 'tabnew $MYVIMRC', - 'Open `$MYVIMRC` in new Tab', - }, - ['fves'] = { - 'split $MYVIMRC', - 'Open `$MYVIMRC` in Horizontal Window', - }, - ['fvev'] = { - 'vsplit $MYVIMRC', - 'Open `$MYVIMRC` in Vertical Window', - }, - - -- Tabs Handling - ['td'] = { - 'tabc', - 'Close Tab', - }, - ['tD'] = { - 'tabc!', - 'Close Tab (Forcefully)', - }, - ['tA'] = { - 'tabnew', - 'New Tab', - }, - ['tf'] = { - 'tabfirst', - 'First Tab', - }, - ['tl'] = { - 'tablast', - 'Last Tab', - }, - ['tn'] = { - 'tabN', - 'Next Tab', - }, - ['tp'] = { - 'tabp', - 'Previous Tab', - }, - - -- Buffer Handling - ['bd'] = { - 'bdel', - 'Close Buffer', - }, - ['bD'] = { - 'bdel!', - 'Close Buffer (Forcefully)', - }, - ['bf'] = { - 'bfirst', - 'First Buffer', - }, - ['bl'] = { - 'blast', - 'Last Buffer', - }, - ['bn'] = { - 'bN', - 'Next Buffer', - }, - ['bp'] = { - 'bp', - 'Previous Buffer', - }, - - -- Window Handling - ['wn'] = { 'w', 'Next Window' }, - -- Window Splitting - ['wss'] = { - 'split', - 'Split Horizontally', - }, - ['wsv'] = { - 'vsplit', - 'Split Vertically', - }, - - -- Exiting - ['qq'] = { - 'qa', - 'Quit All', - }, - ['qQ'] = { - 'qa!', - 'Quit All (Forcefully)', - }, -} +local Regs = {} ---@type fun(mod: string, names: RegKeysNamed, regs: RegKeys?) local function add_if_module(mod, names, regs) @@ -327,56 +190,11 @@ add_if_module('project_nvim', { ['p'] = { name = '+Project' }, }) --- LSP -add_if_module('lspconfig', { - ['l'] = { name = '+LSP' }, - ['lw'] = { name = '+Workspace' }, -}) - -- ToggleTerm add_if_module('toggleterm', { ['T'] = { name = '+ToggleTerm' }, }) --- TODO Comments -add_if_module('todo-comments', { - -- TODO Comments - ['c'] = { name = '+TODO Comments' }, - -- `TODO` Handling - ['ct'] = { name = '+TODO' }, - -- `ERROR` Handling - ['ce'] = { name = '+ERROR' }, - -- `ERROR` Handling - ['cw'] = { name = '+WARNING' }, -}, { - ['ctn'] = { - "lua require('todo-comments').jump_next()", - "Next 'TODO'", - }, - ['ctp'] = { - "lua require('todo-comments').jump_prev()", - "Previous 'TODO'", - }, - -- `ERROR` Handling - ['cen'] = { - "lua require('todo-comments').jump_next({ keywords = { 'ERROR' } })", - "Next 'ERROR'", - }, - ['cep'] = { - "lua require('todo-comments').jump_prev({ keywords = { 'ERROR' } })", - "Previous 'ERROR'", - }, - -- `ERROR` Handling - ['cwn'] = { - "lua require('todo-comments').jump_next({ keywords = { 'WARNING' } })", - "Next 'WARNING'", - }, - ['cwp'] = { - "lua require('todo-comments').jump_prev({ keywords = { 'WARNING' } })", - "Previous 'WARNING'", - }, -}) - reg(Names) reg(Regs) diff --git a/lua/user/maps.lua b/lua/user/maps.lua index 6df77f0d..d6d1f20a 100644 --- a/lua/user/maps.lua +++ b/lua/user/maps.lua @@ -140,7 +140,11 @@ function M.nop(T, opts, mode) end --- `which_key` API entrypoints -M.wk = {} +M.wk = { + available = function() + return Check.exists.module('which-key') + end, +} function M.wk.convert(rhs, opts) if not ((is_str(rhs) and not empty(rhs)) or is_fun(rhs)) then @@ -181,14 +185,12 @@ function M.wk.convert_dict(T) end function M.wk.register(T, opts) - if not is_tbl(T) or empty(T) then - error('(user.maps.wk.register): Table is not an argument!') + if not M.wk.available() then + return false end - local exists = Check.exists.module - - if not exists('which-key') then - return false + if not is_tbl(T) or empty(T) then + error('(user.maps.wk.register): Table is not an argument!') end local WK = require('which-key') @@ -213,11 +215,7 @@ function M.wk.register(T, opts) local tbl = vim.deepcopy(v) for _, o in next, DEFAULT_OPTS do - if is_str(v.name) and o == 'nowait' then - tbl[o] = false - else - tbl[o] = is_bool(tbl[o]) and tbl[o] or true - end + tbl[o] = is_bool(tbl[o]) and tbl[o] or opts[o] end filtered[lhs] = tbl diff --git a/lua/user/types/user/maps.lua b/lua/user/types/user/maps.lua index cb3c193b..96ab516f 100644 --- a/lua/user/types/user/maps.lua +++ b/lua/user/types/user/maps.lua @@ -110,6 +110,7 @@ require('user.types.which_key') ---@field desc BufDescFun ---@class UserMaps.WK +---@field available fun(): boolean ---@field convert fun(rhs: KeyRhs, opts: (ApiMapOpts|KeyMapOpts|BufMapOpts)?): RegKey ---@field convert_dict fun(T: KeyMapDict|ApiMapDict): RegKeys ---@field register fun(T: RegKeys|RegKeysNamed, opts: RegOpts?): false|nil diff --git a/lua/user/types/which_key.lua b/lua/user/types/which_key.lua index 9fd715f3..5e189b43 100644 --- a/lua/user/types/which_key.lua +++ b/lua/user/types/which_key.lua @@ -5,7 +5,7 @@ ---@class RegKey ---@field [1] string|fun() ----@field [2]? string +---@field [2] string ---@field noremap? boolean ---@field nowait? boolean ---@field silent? boolean @@ -16,10 +16,17 @@ ---@field nowait? boolean ---@field silent? boolean ----@alias RegKeys table +---@alias RegKeys table ---@alias RegKeysNamed table ----@alias RegOpts vim.keymap.set.Opts +---@class RegOpts +---@field buffer? integer|nil +---@field mode MapModes +---@field prefix? string +---@field silent? boolean +---@field noremap? boolean +---@field nowait? boolean +---@field expr? boolean ---@class WK ---@field reg fun(maps: RegKeys|RegKeysNamed, opts: RegOpts?)