Skip to content

Commit 78a9ca5

Browse files
feat: mapping and options to sort entries in help window (#2482)
* feat: add option to sort entries in help window * stylua * Add keymap to toggle sorting methods * Bug fix --------- Co-authored-by: Alexander Courtis <[email protected]>
1 parent c2194e9 commit 78a9ca5

File tree

3 files changed

+68
-22
lines changed

3 files changed

+68
-22
lines changed

doc/nvim-tree-lua.txt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,10 @@ CONTENTS *nvim-tree*
2929
5.14 Opts: Trash |nvim-tree-opts-trash|
3030
5.15 Opts: Tab |nvim-tree-opts-tab|
3131
5.16 Opts: Notify |nvim-tree-opts-notify|
32-
5.17 Opts: UI |nvim-tree-opts-ui|
33-
5.18 Opts: Experimental |nvim-tree-opts-experimental|
34-
5.19 Opts: Log |nvim-tree-opts-log|
32+
5.17 Opts: Help |nvim-tree-opts-help|
33+
5.18 Opts: UI |nvim-tree-opts-ui|
34+
5.19 Opts: Experimental |nvim-tree-opts-experimental|
35+
5.20 Opts: Log |nvim-tree-opts-log|
3536
6. API |nvim-tree-api|
3637
6.1 API Tree |nvim-tree-api.tree|
3738
6.2 API File System |nvim-tree-api.fs|
@@ -565,6 +566,9 @@ Following is the default configuration. See |nvim-tree-opts| for details.
565566
threshold = vim.log.levels.INFO,
566567
absolute_path = true,
567568
},
569+
help = {
570+
sort_by = "key",
571+
},
568572
ui = {
569573
confirm = {
570574
remove = true,
@@ -1433,7 +1437,16 @@ Whether to use absolute paths or item names in fs action notifications.
14331437
Type: `boolean`, Default: `true`
14341438

14351439
==============================================================================
1436-
5.17 OPTS: UI *nvim-tree-opts-ui*
1440+
5.17 OPTS: HELP *nvim-tree-opts-help*
1441+
1442+
*nvim-tree.help.sort_by*
1443+
Defines how mappings are sorted in the help window.
1444+
Can be `"key"` (sort alphabetically by keymap)
1445+
or `"desc"` (sort alphabetically by description).
1446+
Type: `string`, Default: `"key"`
1447+
1448+
==============================================================================
1449+
5.18 OPTS: UI *nvim-tree-opts-ui*
14371450

14381451
*nvim-tree.ui.confirm*
14391452
Confirmation prompts.
@@ -1447,14 +1460,14 @@ Confirmation prompts.
14471460
Type: `boolean`, Default: `true`
14481461

14491462
==============================================================================
1450-
5.18 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental*
1463+
5.19 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental*
14511464

14521465
*nvim-tree.experimental*
14531466
Experimental features that may become default or optional functionality.
14541467
In the event of a problem please disable the experiment and raise an issue.
14551468

14561469
==============================================================================
1457-
5.19 OPTS: LOG *nvim-tree-opts-log*
1470+
5.20 OPTS: LOG *nvim-tree-opts-log*
14581471

14591472
Configuration for diagnostic logging.
14601473

lua/nvim-tree.lua

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -588,6 +588,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS
588588
threshold = vim.log.levels.INFO,
589589
absolute_path = true,
590590
},
591+
help = {
592+
sort_by = "key",
593+
},
591594
ui = {
592595
confirm = {
593596
remove = true,
@@ -667,6 +670,9 @@ local ACCEPTED_STRINGS = {
667670
bookmarks_placement = { "before", "after", "signcolumn" },
668671
},
669672
},
673+
help = {
674+
sort_by = { "key", "desc" },
675+
},
670676
}
671677

672678
local function validate_options(conf)

lua/nvim-tree/help.lua

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,17 +84,29 @@ end
8484
--- @return number maximum length of text
8585
local 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" }, {
@@ -202,6 +228,7 @@ end
202228

203229
function M.setup(opts)
204230
M.config.cursorline = opts.view.cursorline
231+
M.config.sort_by = opts.help.sort_by
205232
end
206233

207234
return M

0 commit comments

Comments
 (0)