Skip to content

Commit a9e56ca

Browse files
authored
feat(help): custom sorter option for show_help command (#1867)
1 parent 857008f commit a9e56ca

File tree

4 files changed

+52
-23
lines changed

4 files changed

+52
-23
lines changed

lua/neo-tree/defaults.lua

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,9 @@ local config = {
445445
["e"] = "toggle_auto_expand_width",
446446
["q"] = "close_window",
447447
["?"] = "show_help",
448+
-- You can sort by command name with:
449+
-- ["?"] = { "show_help", config = { sorter = function(a, b) return a.mapping.text < b.mapping.text end } },
450+
-- The type of a and b are neotree.Help.Mapping
448451
["<"] = "prev_source",
449452
[">"] = "next_source",
450453
},

lua/neo-tree/sources/common/commands.lua

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -959,7 +959,8 @@ end
959959
M.show_help = function(state)
960960
local title = state.config and state.config.title or nil
961961
local prefix_key = state.config and state.config.prefix_key or nil
962-
help.show(state, title, prefix_key)
962+
local sorter = state.config and state.config.sorter or nil
963+
help.show(state, title, prefix_key, sorter)
963964
end
964965

965966
return M

lua/neo-tree/sources/common/help.lua

Lines changed: 46 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ local M = {}
77

88
---@param text string
99
---@param highlight string?
10+
---@return NuiLine
1011
local add_text = function(text, highlight)
1112
local line = NuiLine()
1213
line:append(text, highlight)
@@ -16,19 +17,19 @@ end
1617
---@param state neotree.State
1718
---@param prefix_key string?
1819
local get_sub_keys = function(state, prefix_key)
19-
local keys = utils.get_keys(state.resolved_mappings, true)
20-
if prefix_key then
21-
local len = prefix_key:len()
22-
local sub_keys = {}
23-
for _, key in ipairs(keys) do
24-
if #key > len and key:sub(1, len) == prefix_key then
25-
table.insert(sub_keys, key)
26-
end
27-
end
28-
return sub_keys
29-
else
20+
local keys = utils.get_keys(state.resolved_mappings)
21+
if not prefix_key then
3022
return keys
3123
end
24+
25+
local len = prefix_key:len()
26+
local sub_keys = {}
27+
for _, key in ipairs(keys) do
28+
if #key > len and key:sub(1, len) == prefix_key then
29+
table.insert(sub_keys, key)
30+
end
31+
end
32+
return sub_keys
3233
end
3334

3435
---@param key string
@@ -41,15 +42,28 @@ local function key_minus_prefix(key, prefix)
4142
end
4243
end
4344

45+
---@class neotree.Help.Mapping
46+
---@field key string
47+
---@field mapping neotree.State.ResolvedMapping
48+
49+
---@alias neotree.Help.Sorter fun(a: neotree.Help.Mapping, b: neotree.Help.Mapping):boolean
50+
51+
---@type neotree.Help.Sorter
52+
local default_help_sort = function(a, b)
53+
return a.key < b.key
54+
end
55+
4456
---Shows a help screen for the mapped commands when will execute those commands
4557
---when the corresponding key is pressed.
4658
---@param state neotree.State state of the source.
4759
---@param title string? if this is a sub-menu for a multi-key mapping, the title for the window.
4860
---@param prefix_key string? if this is a sub-menu, the start of tehe multi-key mapping
49-
M.show = function(state, title, prefix_key)
61+
---@param sorter neotree.Help.Sorter?
62+
M.show = function(state, title, prefix_key, sorter)
5063
local tree_width = vim.api.nvim_win_get_width(state.winid)
5164
local keys = get_sub_keys(state, prefix_key)
5265

66+
---@type NuiLine[]
5367
local lines = { add_text("") }
5468
lines[1] = add_text(" Press the corresponding key to execute the command.", "Comment")
5569
lines[2] = add_text(" Press <Esc> to cancel.", "Comment")
@@ -60,19 +74,30 @@ M.show = function(state, title, prefix_key)
6074
header:append("COMMAND", highlights.ROOT_NAME)
6175
lines[4] = header
6276
local max_width = #lines[1]:content()
77+
---@type neotree.Help.Mapping[]
78+
local maps = {}
6379
for _, key in ipairs(keys) do
64-
---@type neotree.State.ResolvedMapping
65-
local value = state.resolved_mappings[key]
66-
or { text = "<error mapping for key " .. key .. ">", handler = function() end }
67-
local nline = NuiLine()
68-
nline:append(string.format(" %14s", key_minus_prefix(key, prefix_key)), highlights.FILTER_TERM)
69-
nline:append(" -> ", highlights.DIM_TEXT)
70-
nline:append(value.text, highlights.NORMAL)
71-
local line = nline:content()
80+
maps[#maps + 1] = {
81+
key = key,
82+
mapping = state.resolved_mappings[key]
83+
or { text = "<error mapping for key " .. key .. ">", handler = function() end },
84+
}
85+
end
86+
87+
table.sort(maps, sorter or default_help_sort)
88+
for _, val in ipairs(maps) do
89+
local nuiline = NuiLine()
90+
nuiline:append(
91+
string.format(" %14s", key_minus_prefix(val.key, prefix_key)),
92+
highlights.FILTER_TERM
93+
)
94+
nuiline:append(" -> ", highlights.DIM_TEXT)
95+
nuiline:append(val.mapping.text, highlights.NORMAL)
96+
local line = nuiline:content()
7297
if #line > max_width then
7398
max_width = #line
7499
end
75-
table.insert(lines, nline)
100+
lines[#lines + 1] = nuiline
76101
end
77102

78103
local width = math.min(60, max_width + 1)

lua/neo-tree/utils/init.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ end
427427

428428
---Return the keys of a given table.
429429
---@param tbl string[] The table to get the keys of.
430-
---@param sorted boolean Whether to sort the keys.
430+
---@param sorted boolean? Whether to sort the keys.
431431
---@return string[] keys The keys of the table.
432432
M.get_keys = function(tbl, sorted)
433433
local keys = {}

0 commit comments

Comments
 (0)