Skip to content

Commit

Permalink
refactor: update keymaps and defer register operation to shorten star…
Browse files Browse the repository at this point in the history
…tup time.
  • Loading branch information
ayamir committed Jan 17, 2024
1 parent 3c6de70 commit a9bdd12
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 38 deletions.
7 changes: 7 additions & 0 deletions lua/core/event.lua
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,13 @@ vim.api.nvim_create_autocmd("LspAttach", {
end,
})

vim.api.nvim_create_autocmd({ "CursorHold", "CursorHoldI" }, {
group = vim.api.nvim_create_augroup("WhichKeyPrefixRegister", { clear = true }),
callback = function()
require("modules.utils.keymap").which_key_register()
end,
})

-- auto close NvimTree
vim.api.nvim_create_autocmd("BufEnter", {
group = vim.api.nvim_create_augroup("NvimTreeClose", { clear = true }),
Expand Down
4 changes: 2 additions & 2 deletions lua/keymap/bind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -169,12 +169,12 @@ function bind.nvim_load_mapping(mapping)
if buf and type(buf) == "number" then
vim.api.nvim_buf_set_keymap(buf, mode, keymap, rhs, options)
if should_register then
utils.which_key_register(prefix_to_register, mode, buf)
utils.insert_queue(prefix_to_register, mode, buf)
end
else
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
if should_register then
utils.which_key_register(prefix_to_register, mode, nil)
utils.insert_queue(prefix_to_register, mode, nil)
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions lua/keymap/completion.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ function mapping.lsp(buf)
:with_silent()
:with_buffer(buf)
:with_desc("lsp: Next diagnostic"),
["n|<leader>ld"] = map_cr("Lspsaga show_line_diagnostics ++unfocus")
["n|<leader>lx"] = map_cr("Lspsaga show_line_diagnostics ++unfocus")
:with_silent()
:with_buffer(buf)
:with_desc("lsp: Line diagnostic"),
Expand All @@ -50,11 +50,11 @@ function mapping.lsp(buf)
["n|gd"] = map_cr("Glance definitions"):with_silent():with_buffer(buf):with_desc("lsp: Preview definition"),
["n|gD"] = map_cr("Lspsaga goto_definition"):with_silent():with_buffer(buf):with_desc("lsp: Goto definition"),
["n|gh"] = map_cr("Glance references"):with_silent():with_buffer(buf):with_desc("lsp: Show reference"),
["n|<leader>ci"] = map_cr("Lspsaga incoming_calls")
["n|gci"] = map_cr("Lspsaga incoming_calls")
:with_silent()
:with_buffer(buf)
:with_desc("lsp: Show incoming calls"),
["n|<leader>co"] = map_cr("Lspsaga outgoing_calls")
["n|gco"] = map_cr("Lspsaga outgoing_calls")
:with_silent()
:with_buffer(buf)
:with_desc("lsp: Show outgoing calls"),
Expand Down
6 changes: 3 additions & 3 deletions lua/keymap/editor.lua
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,8 @@ local plug_map = {
:with_desc("edit: Toggle comment for block with selection"),

-- Plugin: diffview
["n|<leader>D"] = map_cr("DiffviewOpen"):with_silent():with_noremap():with_desc("git: Show diff"),
["n|<leader><space>D"] = map_cr("DiffviewClose"):with_silent():with_noremap():with_desc("git: Close diff"),
["n|<leader>gd"] = map_cr("DiffviewOpen"):with_silent():with_noremap():with_desc("git: Show diff"),
["n|<leader>gD"] = map_cr("DiffviewClose"):with_silent():with_noremap():with_desc("git: Close diff"),

-- Plugin: vim-easy-align
["nx|gea"] = map_cr("EasyAlign"):with_desc("edit: Align with delimiter"),
Expand All @@ -68,7 +68,7 @@ local plug_map = {
["nv|<leader>j"] = map_cmd("<Cmd>HopLineMW<CR>"):with_noremap():with_desc("jump: Goto line"),
["nv|<leader>k"] = map_cmd("<Cmd>HopLineMW<CR>"):with_noremap():with_desc("jump: Goto line"),
["nv|<leader>c"] = map_cmd("<Cmd>HopChar1MW<CR>"):with_noremap():with_desc("jump: Goto one char"),
["nv|<leader>cc"] = map_cmd("<Cmd>HopChar2MW<CR>"):with_noremap():with_desc("jump: Goto two chars"),
["nv|<leader>C"] = map_cmd("<Cmd>HopChar2MW<CR>"):with_noremap():with_desc("jump: Goto two chars"),

-- Plugin: treehopper
["o|m"] = map_cu("lua require('tsht').nodes()"):with_silent():with_desc("jump: Operate across syntax tree"),
Expand Down
3 changes: 1 addition & 2 deletions lua/keymap/prefix.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,12 @@ local prefix_desc = {
["<leader>c"] = icons.ui_sep.Character .. "Character",
["<leader>d"] = icons.ui_sep.Bug .. "Debug",
["<leader>f"] = icons.ui_sep.Telescope .. "Fuzzy Find",
["<leader>h"] = icons.git_sep.Git .. "Git Hunk",
["<leader>g"] = icons.git_sep.Git .. "Git",
["<leader>l"] = icons.misc_sep.LspAvailable .. "Lsp",
["<leader>n"] = icons.ui_sep.FolderOpen .. "Nvim Tree",
["<leader>p"] = icons.ui_sep.Package .. "Package",
["<leader>s"] = icons.cmp_sep.tmux .. "Session",
["<leader>t"] = icons.misc_sep.LspAvailable .. "Lsp",
["<leader><space>"] = icons.git_sep.Git .. "Git Diff",
}

return prefix_desc
16 changes: 8 additions & 8 deletions lua/keymap/tool.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ local plug_map = {
-- Plugin: vim-fugitive
["n|gps"] = map_cr("G push"):with_noremap():with_silent():with_desc("git: Push"),
["n|gpl"] = map_cr("G pull"):with_noremap():with_silent():with_desc("git: Pull"),
["n|<leader>G"] = map_cu("Git"):with_noremap():with_silent():with_desc("git: Open git-fugitive"),
["n|<leader>gG"] = map_cu("Git"):with_noremap():with_silent():with_desc("git: Open git-fugitive"),

-- Plugin: nvim-tree
["n|<C-n>"] = map_cr("NvimTreeToggle"):with_noremap():with_silent():with_desc("filetree: Toggle"),
Expand Down Expand Up @@ -55,7 +55,7 @@ local plug_map = {
:with_silent()
:with_desc("terminal: Toggle float"),
["t|<A-d>"] = map_cmd("<Cmd>ToggleTerm<CR>"):with_noremap():with_silent():with_desc("terminal: Toggle float"),
["n|<leader>g"] = map_callback(function()
["n|<leader>gg"] = map_callback(function()
_toggle_lazygit()
end)
:with_noremap()
Expand All @@ -64,23 +64,23 @@ local plug_map = {

-- Plugin: trouble
["n|gt"] = map_cr("TroubleToggle"):with_noremap():with_silent():with_desc("lsp: Toggle trouble list"),
["n|<leader>tr"] = map_cr("TroubleToggle lsp_references")
["n|<leader>ll"] = map_cr("TroubleToggle lsp_references")
:with_noremap()
:with_silent()
:with_desc("lsp: Show lsp references"),
["n|<leader>td"] = map_cr("TroubleToggle document_diagnostics")
["n|<leader>ld"] = map_cr("TroubleToggle document_diagnostics")
:with_noremap()
:with_silent()
:with_desc("lsp: Show document diagnostics"),
["n|<leader>tw"] = map_cr("TroubleToggle workspace_diagnostics")
["n|<leader>lw"] = map_cr("TroubleToggle workspace_diagnostics")
:with_noremap()
:with_silent()
:with_desc("lsp: Show workspace diagnostics"),
["n|<leader>tq"] = map_cr("TroubleToggle quickfix")
["n|<leader>lq"] = map_cr("TroubleToggle quickfix")
:with_noremap()
:with_silent()
:with_desc("lsp: Show quickfix list"),
["n|<leader>tl"] = map_cr("TroubleToggle loclist"):with_noremap():with_silent():with_desc("lsp: Show loclist"),
["n|<leader>lL"] = map_cr("TroubleToggle loclist"):with_noremap():with_silent():with_desc("lsp: Show loclist"),

-- Plugin: telescope
["n|<C-p>"] = map_callback(function()
Expand Down Expand Up @@ -121,7 +121,7 @@ local plug_map = {
:with_noremap()
:with_silent()
:with_desc("ui: Change colorscheme for current session"),
["n|<leader>fn"] = map_cu(":enew"):with_noremap():with_silent():with_desc("buffer: New"),
["n|<leader>bn"] = map_cu(":enew"):with_noremap():with_silent():with_desc("buffer: New"),
["n|<leader>fg"] = map_cu("Telescope git_files")
:with_noremap()
:with_silent()
Expand Down
16 changes: 8 additions & 8 deletions lua/keymap/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -54,42 +54,42 @@ function mapping.gitsigns(buf)
:with_buffer(buf)
:with_expr()
:with_desc("git: Goto prev hunk"),
["n|<leader>hs"] = bind.map_callback(function()
["n|<leader>gs"] = bind.map_callback(function()
actions.stage_hunk()
end)
:with_buffer(buf)
:with_desc("git: Stage hunk"),
["v|<leader>hs"] = bind.map_callback(function()
["v|<leader>gs"] = bind.map_callback(function()
actions.stage_hunk({ vim.fn.line("."), vim.fn.line("v") })
end)
:with_buffer(buf)
:with_desc("git: Stage hunk"),
["n|<leader>hu"] = bind.map_callback(function()
["n|<leader>gu"] = bind.map_callback(function()
actions.undo_stage_hunk()
end)
:with_buffer(buf)
:with_desc("git: Undo stage hunk"),
["n|<leader>hr"] = bind.map_callback(function()
["n|<leader>gr"] = bind.map_callback(function()
actions.reset_hunk()
end)
:with_buffer(buf)
:with_desc("git: Reset hunk"),
["v|<leader>hr"] = bind.map_callback(function()
["v|<leader>gr"] = bind.map_callback(function()
actions.reset_hunk({ vim.fn.line("."), vim.fn.line("v") })
end)
:with_buffer(buf)
:with_desc("git: Reset hunk"),
["n|<leader>hR"] = bind.map_callback(function()
["n|<leader>gR"] = bind.map_callback(function()
actions.reset_buffer()
end)
:with_buffer(buf)
:with_desc("git: Reset buffer"),
["n|<leader>hp"] = bind.map_callback(function()
["n|<leader>gp"] = bind.map_callback(function()
actions.preview_hunk()
end)
:with_buffer(buf)
:with_desc("git: Preview hunk"),
["n|<leader>hb"] = bind.map_callback(function()
["n|<leader>gb"] = bind.map_callback(function()
actions.blame_line({ full = true })
end)
:with_buffer(buf)
Expand Down
8 changes: 0 additions & 8 deletions lua/modules/configs/ui/alpha.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,6 @@ return function()
require("telescope.builtin").find_files()
end,
}),
button("space f n", " File new", leader, nil, {
noremap = true,
silent = true,
nowait = true,
callback = function()
vim.api.nvim_command("enew")
end,
}),
button("space f w", " Word find", leader, nil, {
noremap = true,
silent = true,
Expand Down
23 changes: 19 additions & 4 deletions lua/modules/utils/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,11 +202,23 @@ function M.replace(mapping)
end
end

function M.which_key_register()
if M.which_key_queue then
local wk_avail, wk = pcall(require, "which-key")
if wk_avail then
for registration, options in pairs(M.which_key_queue) do
wk.register(registration, options)
end
M.which_key_queue = nil
end
end
end

---Register prefix for grouped keymap.
---@param prefix string
---@param mode string
---@param buffer number|nil
function M.which_key_register(prefix, mode, buffer)
function M.insert_queue(prefix, mode, buffer)
local registration = {}
local options = {
mode = mode,
Expand All @@ -218,9 +230,12 @@ function M.which_key_register(prefix, mode, buffer)
end
registration[prefix] = {}
registration[prefix]["name"] = prefix_desc[prefix]
local wk_avail, wk = pcall(require, "which-key")
if wk_avail then
wk.register(registration, options)
if not M.which_key_queue then
M.which_key_queue = {}
end
M.which_key_queue[registration] = options
if buffer ~= nil then
M.which_key_register()
end
end

Expand Down

0 comments on commit a9bdd12

Please sign in to comment.