8484--- @return number maximum length of text
8585local function compute ()
8686 local head_lhs = " nvim-tree mappings"
87- local head_rhs = " exit: q"
87+ local head_rhs1 = " exit: q"
88+ local head_rhs2 = string.format (" sort by %s: s" , M .config .sort_by == " key" and " description" or " keymap" )
8889
8990 -- formatted lhs and desc from active keymap
9091 local mappings = vim .tbl_map (function (map )
9192 return { lhs = tidy_lhs (map .lhs ), desc = tidy_desc (map .desc ) }
9293 end , keymap .get_keymap ())
9394
94- -- sort roughly by lhs
95- table.sort (mappings , function (a , b )
96- return sort_lhs (a .lhs , b .lhs )
97- end )
95+ -- sorter function for mappings
96+ local sort_fn
97+
98+ if M .config .sort_by == " desc" then
99+ sort_fn = function (a , b )
100+ return a .desc :lower () < b .desc :lower ()
101+ end
102+ else
103+ -- by default sort roughly by lhs
104+ sort_fn = function (a , b )
105+ return sort_lhs (a .lhs , b .lhs )
106+ end
107+ end
108+
109+ table.sort (mappings , sort_fn )
98110
99111 -- longest lhs and description
100112 local max_lhs = 0
@@ -105,11 +117,14 @@ local function compute()
105117 end
106118
107119 -- increase desc if lines are shorter than the header
108- max_desc = math.max (max_desc , # head_lhs + # head_rhs - max_lhs )
120+ max_desc = math.max (max_desc , # head_lhs + # head_rhs1 - max_lhs )
109121
110122 -- header, not padded
111123 local hl = { { " NvimTreeRootFolder" , 0 , 0 , # head_lhs } }
112- local lines = { (" %s%s%s" ):format (head_lhs , string.rep (" " , max_desc + max_lhs - # head_lhs - # head_rhs + 2 ), head_rhs ) }
124+ local lines = {
125+ head_lhs .. string.rep (" " , max_desc + max_lhs - # head_lhs - # head_rhs1 + 2 ) .. head_rhs1 ,
126+ string.rep (" " , max_desc + max_lhs - # head_rhs2 + 2 ) .. head_rhs2 ,
127+ }
113128 local width = # lines [1 ]
114129
115130 -- mappings, left padded 1
@@ -121,7 +136,7 @@ local function compute()
121136 width = math.max (# line , width )
122137
123138 -- highlight lhs
124- table.insert (hl , { " NvimTreeFolderName" , i , 1 , # l .lhs + 1 })
139+ table.insert (hl , { " NvimTreeFolderName" , i + 1 , 1 , # l .lhs + 1 })
125140 end
126141
127142 return lines , hl , width
@@ -175,14 +190,25 @@ local function open()
175190 vim .wo [M .winnr ].winhl = WIN_HL
176191 vim .wo [M .winnr ].cursorline = M .config .cursorline
177192
178- -- quit binding
179- vim .keymap .set (" n" , " q" , close , {
180- desc = " nvim-tree: exit help" ,
181- buffer = M .bufnr ,
182- noremap = true ,
183- silent = true ,
184- nowait = true ,
185- })
193+ local function toggle_sort ()
194+ M .config .sort_by = (M .config .sort_by == " desc" ) and " key" or " desc"
195+ open ()
196+ end
197+
198+ local keymaps = {
199+ q = { fn = close , desc = " nvim-tree: exit help" },
200+ s = { fn = toggle_sort , desc = " nvim-tree: toggle sorting method" },
201+ }
202+
203+ for k , v in pairs (keymaps ) do
204+ vim .keymap .set (" n" , k , v .fn , {
205+ desc = v .desc ,
206+ buffer = M .bufnr ,
207+ noremap = true ,
208+ silent = true ,
209+ nowait = true ,
210+ })
211+ end
186212
187213 -- close window and delete buffer on leave
188214 vim .api .nvim_create_autocmd ({ " BufLeave" , " WinLeave" }, {
202228
203229function M .setup (opts )
204230 M .config .cursorline = opts .view .cursorline
231+ M .config .sort_by = opts .help .sort_by
205232end
206233
207234return M
0 commit comments