Skip to content

Commit

Permalink
refactor: Partial keymap overhaul.
Browse files Browse the repository at this point in the history
I have successfully integrated `which_key` conditionally.
Now it's a question to integrate it to the whole config.
  • Loading branch information
DrKJeff16 committed May 29, 2024
1 parent 2c9d820 commit 3884d67
Show file tree
Hide file tree
Showing 8 changed files with 278 additions and 341 deletions.
87 changes: 72 additions & 15 deletions init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ local map_tbl = {
},
['<leader>fvV'] = { ':so ', desc('Source VimScript File (Interactively)', false) },
['<leader>fvL'] = { ':luafile ', desc('Source Lua File (Interactively)', false) },
['<leader>fvet'] = { ':tabnew $MYVIMRC<CR>' },
['<leader>fvee'] = { ':ed $MYVIMRC<CR>' },
['<leader>fves'] = { ':split $MYVIMRC<CR>' },
['<leader>fvev'] = { ':vsplit $MYVIMRC<CR>' },
['<leader>fvet'] = { ':tabnew $MYVIMRC<CR>', desc('Open In New Tab') },
['<leader>fvee'] = { ':ed $MYVIMRC<CR>', desc('Open In Current Window') },
['<leader>fves'] = { ':split $MYVIMRC<CR>', desc('Open In Horizontal Split') },
['<leader>fvev'] = { ':vsplit $MYVIMRC<CR>', desc('Open In Vertical Split') },

['<leader>vh'] = { ':checkhealth<CR>', desc('Run Checkhealth', false) },

Expand All @@ -147,8 +147,8 @@ local map_tbl = {
['<leader>wsS'] = { ':split ', desc('Horizontal Split (Interactively)', false) },
['<leader>wsV'] = { ':vsplit ', desc('Vertical Split (Interactively)', false) },

['<leader>qq'] = { ':qa<CR>' },
['<leader>qQ'] = { ':qa!<CR>' },
['<leader>qq'] = { ':qa<CR>', desc('Quit Nvim') },
['<leader>qQ'] = { ':qa!<CR>', desc('Quit Nvim Forcefully') },

['<leader>tn'] = { ':tabN<CR>', desc('Next Tab', false) },
['<leader>tp'] = { ':tabp<CR>', desc('Previous Tab', false) },
Expand Down Expand Up @@ -177,12 +177,48 @@ local map_tbl = {
['<leader>r'] = { ':s/', desc('Run Search-Replace Interactively', false) },
['<leader>ir'] = { ':%retab<CR>', desc('Retab Selection') },
},
t = {
-- Escape terminl by pressing `<Esc>`
['<Esc>'] = { '<C-\\><C-n>', { noremap = true } },
}
---@type table<MapModes, RegKeysNamed>
local Names = {
n = {
-- File Handling
['<leader>f'] = { name = '+File' },
--- Source File Handling
['<leader>fv'] = { name = '+Vim Files' },
--- `init.lua` Editing
['<leader>fve'] = { name = '+Edit `init.lua`' },

-- Tabs Handling
['<leader>t'] = { name = '+Tabs' },

-- Buffer Handling
['<leader>b'] = { name = '+Buffer' },

-- Window Handling
['<leader>w'] = { name = '+Window' },

-- Window Splitting
['<leader>ws'] = { name = '+Split' },

-- Exiting
['<leader>q'] = { name = '+Quit Nvim' },

-- Help
['<leader>h'] = { name = '+Help' },

-- Session
['<leader>S'] = { name = '+Session' },

-- Vim
['<leader>v'] = { name = '+Vim' },
},
v = {
['<leader>i'] = { name = '+Indent' },
},
}

Kmap.t('<Esc>', '<C-\\><C-n>')

if not called_lazy then
-- List of manually-callable plugins.
_G.Pkg = require('lazy_cfg')
Expand All @@ -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
Expand All @@ -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')[]
Expand All @@ -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['<leader>vc' .. tostring(i)] = { Csc[c].setup, 'Setup Colorscheme `' .. c .. '`' }
CscKeys['<leader>vc' .. tostring(i)] = { Csc[c].setup, desc('Setup Colorscheme `' .. c .. '`') }
i = i + 1
end
end

register({ ['<leader>vc'] = { name = '+Colorschemes' } })
register(CscKeys)
if WK.available() then
register({ ['<leader>vc'] = { name = '+Colorschemes' } }, { mode = 'n' })
register(WK.convert_dict(CscKeys), { mode = 'n' })

register({ ['<leader>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()
Expand Down
72 changes: 45 additions & 27 deletions lua/lazy_cfg/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -859,35 +860,52 @@ local key_variant = function(cmd)
end
end

---@type KeyMapDict
---@type table<MapModes, KeyMapDict>
local Keys = {
['<leader>Lee'] = { key_variant('ed'), desc('Open `Lazy` File') },
['<leader>Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') },
['<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>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) },
n = {
['<leader>Lee'] = { key_variant('ed'), desc('Open `Lazy` File') },
['<leader>Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') },
['<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>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) },
},
v = {
['<leader>Lee'] = { key_variant('ed'), desc('Open `Lazy` File') },
['<leader>Les'] = { key_variant('split'), desc('Open `Lazy` File Horizontal Window') },
['<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>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') },
},
}

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
119 changes: 74 additions & 45 deletions lua/lazy_cfg/lspconfig/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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({ ['<leaderl>'] = { 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 }),
Expand All @@ -216,49 +218,76 @@ au('LspAttach', {

bo[buf].omnifunc = 'v:lua.lsp.omnifunc'

---@type table<MapModes, KeyMapDict>
local K = {
['<leader>lgD'] = { lsp_buf.declaration, desc('Declaration', true, buf) },
['<leader>lgd'] = { lsp_buf.definition, desc('Definition', true, buf) },
['<leader>lk'] = { lsp_buf.hover, desc('Hover', true, buf) },
['K'] = { lsp_buf.hover, desc('Hover', true, buf) },
['<leader>lgi'] = { lsp_buf.implementation, desc('Implementation', true, buf) },
['<leader>lS'] = { lsp_buf.signature_help, desc('Signature Help', true, buf) },
['<leader>lwa'] = { lsp_buf.add_workspace_folder, desc('Add Workspace Folder', true, buf) },
['<leader>lwr'] = { lsp_buf.remove_workspace_folder, desc('Remove Workspace Folder', true, buf) },
['<leader>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 = {
['<leader>lfD'] = { lsp_buf.declaration, desc('Declaration', true, buf) },
['<leader>lfd'] = { lsp_buf.definition, desc('Definition', true, buf) },
['<leader>lk'] = { lsp_buf.hover, desc('Hover', true, buf) },
['K'] = { lsp_buf.hover, desc('Hover', true, buf) },
['<leader>lfi'] = { lsp_buf.implementation, desc('Implementation', true, buf) },
['<leader>lfS'] = { lsp_buf.signature_help, desc('Signature Help', true, buf) },
['<leader>lwa'] = { lsp_buf.add_workspace_folder, desc('Add Workspace Folder', true, buf) },
['<leader>lwr'] = { lsp_buf.remove_workspace_folder, desc('Remove Workspace Folder', true, buf) },
['<leader>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),
},
['<leader>lfT'] = { lsp_buf.type_definition, desc('Type Definition', true, buf) },
['<leader>lfR'] = { lsp_buf.rename, desc('Rename...', true, buf) },
['<leader>lfr'] = { lsp_buf.references, desc('References', true, buf) },
['<leader>lff'] = {
function()
lsp_buf.format({ async = true })
end,
desc('Format File', true, buf),
},
['<leader>lca'] = { lsp_buf.code_action, desc('Code Actions', true, buf) },
},
['<leader>lD'] = { lsp_buf.type_definition, desc('Type Definition', true, buf) },
['<leader>lrn'] = { lsp_buf.rename, desc('Rename...', true, buf) },
['<leader>lgr'] = { lsp_buf.references, desc('References', true, buf) },
['<leader>lf'] = {
function()
lsp_buf.format({ async = true })
end,
desc('Format File', true, buf),
v = {
['<leader>lca'] = { lsp_buf.code_action, desc('Code Actions', true, buf) },
},
}
---@type table<MapModes, RegKeysNamed>
local Names = {
n = {
['<leader>lc'] = { name = '+Code Actions' },
['<leader>lw'] = { name = '+Workspace' },
['<leader>lf'] = { name = '+File Analysis' },
},
v = {
['<leader>lc'] = { name = '+Code Actions' },
},
}

local K2 = WK.convert_dict(K)
WK.register(K2)

vim.keymap.set({ 'n', 'v' }, '<leader>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,
})

Expand Down
Loading

0 comments on commit 3884d67

Please sign in to comment.