84
84
--- @return number maximum length of text
85
85
local function compute ()
86
86
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" )
88
89
89
90
-- formatted lhs and desc from active keymap
90
91
local mappings = vim .tbl_map (function (map )
91
92
return { lhs = tidy_lhs (map .lhs ), desc = tidy_desc (map .desc ) }
92
93
end , keymap .get_keymap ())
93
94
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 )
98
110
99
111
-- longest lhs and description
100
112
local max_lhs = 0
@@ -105,11 +117,14 @@ local function compute()
105
117
end
106
118
107
119
-- 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 )
109
121
110
122
-- header, not padded
111
123
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
+ }
113
128
local width = # lines [1 ]
114
129
115
130
-- mappings, left padded 1
@@ -121,7 +136,7 @@ local function compute()
121
136
width = math.max (# line , width )
122
137
123
138
-- highlight lhs
124
- table.insert (hl , { " NvimTreeFolderName" , i , 1 , # l .lhs + 1 })
139
+ table.insert (hl , { " NvimTreeFolderName" , i + 1 , 1 , # l .lhs + 1 })
125
140
end
126
141
127
142
return lines , hl , width
@@ -175,14 +190,25 @@ local function open()
175
190
vim .wo [M .winnr ].winhl = WIN_HL
176
191
vim .wo [M .winnr ].cursorline = M .config .cursorline
177
192
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
186
212
187
213
-- close window and delete buffer on leave
188
214
vim .api .nvim_create_autocmd ({ " BufLeave" , " WinLeave" }, {
202
228
203
229
function M .setup (opts )
204
230
M .config .cursorline = opts .view .cursorline
231
+ M .config .sort_by = opts .help .sort_by
205
232
end
206
233
207
234
return M
0 commit comments