Skip to content

Commit

Permalink
fix: ensure consistent order with descr sort
Browse files Browse the repository at this point in the history
  • Loading branch information
willothy committed Jul 25, 2023
1 parent 6f44662 commit ee9df03
Showing 1 changed file with 29 additions and 30 deletions.
59 changes: 29 additions & 30 deletions lua/which-key/keys.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,35 +81,26 @@ function M.process_motions(ret, mode, prefix_i, buf)
end

function M.compare_by_keys(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
end
return aw < bw
else
return (a.group and 1 or 0) < (b.group and 1 or 0)
end
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)
if a.group == b.group then
if not a.desc then
return false
end
if not b.desc then
return true
end
return a.desc < b.desc
else
return (a.group and 1 or 0) < (b.group and 1 or 0)
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
Expand Down Expand Up @@ -191,11 +182,19 @@ function M.get_mappings(mode, prefix_i, buf)
end
end

local comparator
if Config.options.sort_by_description then
comparator = M.compare_by_desc
else
comparator = M.compare_by_keys
local comparator = function(a, b)
if a.order and b.order then
return a.order < b.order
end
if a.group == b.group then
if Config.options.sort_by_description then
return M.compare_by_desc(a, b)
else
return M.compare_by_keys(a, b)
end
else
return (a.group and 1 or 0) < (b.group and 1 or 0)
end
end

-- Sort items, but not for plugins
Expand Down

0 comments on commit ee9df03

Please sign in to comment.