@@ -7,6 +7,7 @@ local M = {}
77
88--- @param text string
99--- @param highlight string ?
10+ --- @return NuiLine
1011local add_text = function (text , highlight )
1112 local line = NuiLine ()
1213 line :append (text , highlight )
1617--- @param state neotree.State
1718--- @param prefix_key string ?
1819local 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
3233end
3334
3435--- @param key string
@@ -41,15 +42,28 @@ local function key_minus_prefix(key, prefix)
4142 end
4243end
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 )
0 commit comments