From 95c30a28c0698af0f233a8f928b310acbcbfef1e Mon Sep 17 00:00:00 2001 From: Akmadan23 Date: Fri, 20 Oct 2023 11:13:11 +0200 Subject: [PATCH 1/4] feat: add option to sort entries in help window --- doc/nvim-tree-lua.txt | 25 +++++++++++++++++++------ lua/nvim-tree.lua | 6 ++++++ lua/nvim-tree/help.lua | 20 ++++++++++++++++---- 3 files changed, 41 insertions(+), 10 deletions(-) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index 1dd7821959b..14fcd467eff 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -29,9 +29,10 @@ CONTENTS *nvim-tree* 5.14 Opts: Trash |nvim-tree-opts-trash| 5.15 Opts: Tab |nvim-tree-opts-tab| 5.16 Opts: Notify |nvim-tree-opts-notify| - 5.17 Opts: UI |nvim-tree-opts-ui| - 5.18 Opts: Experimental |nvim-tree-opts-experimental| - 5.19 Opts: Log |nvim-tree-opts-log| + 5.17 Opts: Help |nvim-tree-opts-help| + 5.18 Opts: UI |nvim-tree-opts-ui| + 5.19 Opts: Experimental |nvim-tree-opts-experimental| + 5.20 Opts: Log |nvim-tree-opts-log| 6. API |nvim-tree-api| 6.1 API Tree |nvim-tree-api.tree| 6.2 API File System |nvim-tree-api.fs| @@ -564,6 +565,9 @@ Following is the default configuration. See |nvim-tree-opts| for details. threshold = vim.log.levels.INFO, absolute_path = true, }, + help = { + sort_by = "key", + }, ui = { confirm = { remove = true, @@ -1428,7 +1432,16 @@ Whether to use absolute paths or item names in fs action notifications. Type: `boolean`, Default: `true` ============================================================================== - 5.17 OPTS: UI *nvim-tree-opts-ui* + 5.17 OPTS: HELP *nvim-tree-opts-help* + +*nvim-tree.help.sort_by* +Defines how mappings are sorted in the help window. +Can be `"key"` (sort alphabetically by keymap) +or `"desc"` (sort alphabetically by description). + Type: `string`, Default: `"key"` + +============================================================================== + 5.18 OPTS: UI *nvim-tree-opts-ui* *nvim-tree.ui.confirm* Confirmation prompts. @@ -1442,14 +1455,14 @@ Confirmation prompts. Type: `boolean`, Default: `true` ============================================================================== - 5.18 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental* + 5.19 OPTS: EXPERIMENTAL *nvim-tree-opts-experimental* *nvim-tree.experimental* Experimental features that may become default or optional functionality. In the event of a problem please disable the experiment and raise an issue. ============================================================================== - 5.19 OPTS: LOG *nvim-tree-opts-log* + 5.20 OPTS: LOG *nvim-tree-opts-log* Configuration for diagnostic logging. diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index d2552b84a53..a5005f378b1 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -587,6 +587,9 @@ local DEFAULT_OPTS = { -- BEGIN_DEFAULT_OPTS threshold = vim.log.levels.INFO, absolute_path = true, }, + help = { + sort_by = "key", + }, ui = { confirm = { remove = true, @@ -666,6 +669,9 @@ local ACCEPTED_STRINGS = { bookmarks_placement = { "before", "after", "signcolumn" }, }, }, + help = { + sort_by = { "key", "desc" } + }, } local function validate_options(conf) diff --git a/lua/nvim-tree/help.lua b/lua/nvim-tree/help.lua index 78617cb025e..aa369dbc184 100644 --- a/lua/nvim-tree/help.lua +++ b/lua/nvim-tree/help.lua @@ -91,10 +91,21 @@ local function compute() return { lhs = tidy_lhs(map.lhs), desc = tidy_desc(map.desc) } end, keymap.get_keymap()) - -- sort roughly by lhs - table.sort(mappings, function(a, b) - return sort_lhs(a.lhs, b.lhs) - end) + -- sorter function for mappings + local sort_fn + + if M.config.sort_by == "desc" then + sort_fn = function(a, b) + return a.desc:lower() < b.desc:lower() + end + else + -- by default sort roughly by lhs + sort_fn = function(a, b) + return sort_lhs(a.lhs, b.lhs) + end + end + + table.sort(mappings, sort_fn) -- longest lhs and description local max_lhs = 0 @@ -202,6 +213,7 @@ end function M.setup(opts) M.config.cursorline = opts.view.cursorline + M.config.sort_by = opts.help.sort_by end return M From 109a0ac7775b77db34f96cd7d8996432e7231373 Mon Sep 17 00:00:00 2001 From: Akmadan23 Date: Fri, 20 Oct 2023 11:15:48 +0200 Subject: [PATCH 2/4] stylua --- lua/nvim-tree.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree.lua b/lua/nvim-tree.lua index a5005f378b1..050385c4ce6 100644 --- a/lua/nvim-tree.lua +++ b/lua/nvim-tree.lua @@ -670,7 +670,7 @@ local ACCEPTED_STRINGS = { }, }, help = { - sort_by = { "key", "desc" } + sort_by = { "key", "desc" }, }, } From f72dec60280a91d857fa0c173f6c95b81ac0cbb8 Mon Sep 17 00:00:00 2001 From: Akmadan23 Date: Sat, 21 Oct 2023 13:14:07 +0200 Subject: [PATCH 3/4] Add keymap to toggle sorting methods --- lua/nvim-tree/help.lua | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/lua/nvim-tree/help.lua b/lua/nvim-tree/help.lua index aa369dbc184..6237d169a36 100644 --- a/lua/nvim-tree/help.lua +++ b/lua/nvim-tree/help.lua @@ -84,7 +84,8 @@ end --- @return number maximum length of text local function compute() local head_lhs = "nvim-tree mappings" - local head_rhs = "exit: q" + local head_rhs1 = "exit: q" + local head_rhs2 = string.format("sort by %s: s", M.config.sort_by) -- formatted lhs and desc from active keymap local mappings = vim.tbl_map(function(map) @@ -116,11 +117,14 @@ local function compute() end -- increase desc if lines are shorter than the header - max_desc = math.max(max_desc, #head_lhs + #head_rhs - max_lhs) + max_desc = math.max(max_desc, #head_lhs + #head_rhs1 - max_lhs) -- header, not padded local hl = { { "NvimTreeRootFolder", 0, 0, #head_lhs } } - local lines = { ("%s%s%s"):format(head_lhs, string.rep(" ", max_desc + max_lhs - #head_lhs - #head_rhs + 2), head_rhs) } + local lines = { + head_lhs .. string.rep(" ", max_desc + max_lhs - #head_lhs - #head_rhs1 + 2) .. head_rhs1, + string.rep(" ", max_desc + max_lhs - #head_rhs2 + 2) .. head_rhs2, + } local width = #lines[1] -- mappings, left padded 1 @@ -132,7 +136,7 @@ local function compute() width = math.max(#line, width) -- highlight lhs - table.insert(hl, { "NvimTreeFolderName", i, 1, #l.lhs + 1 }) + table.insert(hl, { "NvimTreeFolderName", i + 1, 1, #l.lhs + 1 }) end return lines, hl, width @@ -186,14 +190,25 @@ local function open() vim.wo[M.winnr].winhl = WIN_HL vim.wo[M.winnr].cursorline = M.config.cursorline - -- quit binding - vim.keymap.set("n", "q", close, { - desc = "nvim-tree: exit help", - buffer = M.bufnr, - noremap = true, - silent = true, - nowait = true, - }) + local function toggle_sort() + M.config.sort_by = (M.config.sort_by == "desc") and "key" or "desc" + open() + end + + local keymaps = { + q = { fn = close, desc = "nvim-tree: exit help" }, + s = { fn = toggle_sort, desc = "nvim-tree: toggle sorting method" }, + } + + for k, v in pairs(keymaps) do + vim.keymap.set("n", k, v.fn, { + desc = v.desc, + buffer = M.bufnr, + noremap = true, + silent = true, + nowait = true, + }) + end -- close window and delete buffer on leave vim.api.nvim_create_autocmd({ "BufLeave", "WinLeave" }, { From 51237ece755db5e76f368531b798f30da76448f0 Mon Sep 17 00:00:00 2001 From: Akmadan23 Date: Sat, 21 Oct 2023 20:26:41 +0200 Subject: [PATCH 4/4] Bug fix --- lua/nvim-tree/help.lua | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lua/nvim-tree/help.lua b/lua/nvim-tree/help.lua index 6237d169a36..c7342be54d1 100644 --- a/lua/nvim-tree/help.lua +++ b/lua/nvim-tree/help.lua @@ -85,7 +85,7 @@ end local function compute() local head_lhs = "nvim-tree mappings" local head_rhs1 = "exit: q" - local head_rhs2 = string.format("sort by %s: s", M.config.sort_by) + local head_rhs2 = string.format("sort by %s: s", M.config.sort_by == "key" and "description" or "keymap") -- formatted lhs and desc from active keymap local mappings = vim.tbl_map(function(map)