Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Register readable prefix for grouped keymap settings #1134

Merged
merged 19 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions lua/keymap/bind.lua
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,18 @@ function bind.nvim_load_mapping(mapping)
local rhs = value.cmd
local options = value.options
local buf = value.buffer
local M = require("modules.utils.keymap")
if buf and type(buf) == "number" then
vim.api.nvim_buf_set_keymap(buf, mode, keymap, rhs, options)
--- if begin with <leader>
if keymap:sub(1, #"<leader>") == "<leader>" and #keymap > #"<leader>x" then
M.insert_queue(keymap:sub(1, #"<leader>x"), mode, buf)
end
else
vim.api.nvim_set_keymap(mode, keymap, rhs, options)
if keymap:sub(1, #"<leader>") == "<leader>" and #keymap > #"<leader>x" then
M.insert_queue(keymap:sub(1, #"<leader>x"), mode, nil)
end
end
end
end
Expand Down
10 changes: 10 additions & 0 deletions lua/keymap/prefix.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
local icons = {
ui_sep = require("modules.utils.icons").get("ui", true),
}

--- Need to expand by hand
local prefix_desc = {
["<leader>b"] = icons.ui_sep.Buffer .. "Buffer",
}

return prefix_desc
4 changes: 3 additions & 1 deletion lua/modules/configs/tool/which-key.lua
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ return function()
icons = {
breadcrumb = icons.ui.Separator,
separator = icons.misc.Vbar,
group = icons.misc.Add,
group = "",
},

window = {
Expand All @@ -31,4 +31,6 @@ return function()
winblend = 0,
},
})

require("modules.utils.keymap").which_key_register()
end
1 change: 1 addition & 0 deletions lua/modules/utils/icons.lua
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ local data = {
BigCircle = "",
BigUnfilledCircle = "",
BookMark = "󰃃",
Buffer = "󰓩",
Bug = "",
Calendar = "",
Check = "󰄳",
Expand Down
35 changes: 35 additions & 0 deletions lua/modules/utils/keymap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -202,4 +202,39 @@ function M.replace(mapping)
end
end

--- Register queued which-key mappings
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, opts in pairs(M.which_key_queue) do
wk.register(registration, opts)
end
M.which_key_queue = nil
end
end
end

--- Insert a prefix keymap into register queue.
function M.insert_queue(prefix_keymap, mode, buffer)
local registration = {}
local options = {
mode = mode,
buffer = buffer,
}
local P = require("keymap.prefix")
if not P[prefix_keymap] then
return
end
--- set name with icon
registration[prefix_keymap] = {}
registration[prefix_keymap]["name"] = P[prefix_keymap:sub(1, #"<leader>x")]
if not M.which_key_queue then
M.which_key_queue = {}
end
if not M.which_key_queue[registration] then
M.which_key_queue[registration] = options
end
end

return M