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

feat: allow sorting by description #486

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ WhichKey comes with the following defaults:
hidden = { "<silent>", "<cmd>", "<Cmd>", "<CR>", "^:", "^ ", "^call ", "^lua " }, -- hide mapping boilerplate
show_help = true, -- show a help message in the command line for using WhichKey
show_keys = true, -- show the currently pressed key and its label as a message in the command line
sort_by_description = false, -- sort menu by description instead of keymaps
triggers = "auto", -- automatically setup triggers
-- triggers = {"<leader>"} -- or specifiy a list manually
-- list of triggers, where WhichKey should not wait for timeoutlen and show immediately
Expand Down
1 change: 1 addition & 0 deletions lua/which-key/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ local defaults = {
show_help = true, -- show a help message in the command line for using WhichKey
show_keys = true, -- show the currently pressed key and its label as a message in the command line
triggers = "auto", -- automatically setup triggers
sort_by_description = false, -- sort menu by description instead of keymaps
-- triggers = {"<leader>"} -- or specifiy a list manually
-- list of triggers, where WhichKey should not wait for timeoutlen and show immediately
triggers_nowait = {
Expand Down
42 changes: 32 additions & 10 deletions lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,29 @@ function M.process_motions(ret, mode, prefix_i, buf)
end
end

function M.compare_by_keys(a, b)
local ak = (a.key or ""):lower()
local bk = (b.key or ""):lower()
local aw = ak:match("[a-z]") and 1 or 0
local bw = bk:match("[a-z]") and 1 or 0
if aw == bw then
return ak < bk
end
return aw < bw
end

function M.compare_by_desc(a, b)
local ad = a.desc or a.label
local bd = b.desc or b.label
if not ad then
return true
end
if not bd then
return false
end
return ad < bd
end

---@return MappingGroup
function M.get_mappings(mode, prefix_i, buf)
---@class MappingGroup
Expand Down Expand Up @@ -159,24 +182,23 @@ function M.get_mappings(mode, prefix_i, buf)
end
end

-- Sort items, but not for plugins
table.sort(tmp, function(a, b)
local comparator = function(a, b)
if a.order and b.order then
return a.order < b.order
end
if a.group == b.group then
local ak = (a.key or ""):lower()
local bk = (b.key or ""):lower()
local aw = ak:match("[a-z]") and 1 or 0
local bw = bk:match("[a-z]") and 1 or 0
if aw == bw then
return ak < bk
if Config.options.sort_by_description then
return M.compare_by_desc(a, b)
else
return M.compare_by_keys(a, b)
end
return aw < bw
else
return (a.group and 1 or 0) < (b.group and 1 or 0)
end
end)
end

-- Sort items, but not for plugins
table.sort(tmp, comparator)
ret.mappings = tmp

return ret
Expand Down