From f2b324933f4d505cff6f7d445fd61fad02dcd9ae Mon Sep 17 00:00:00 2001 From: Steven Arcangeli Date: Tue, 3 Dec 2024 09:44:07 -0800 Subject: [PATCH] feat: better merging of action desc when overriding keymaps --- lua/oil/config.lua | 26 ++++++++++++-------------- lua/oil/keymap_util.lua | 13 ++++++++++++- 2 files changed, 24 insertions(+), 15 deletions(-) diff --git a/lua/oil/config.lua b/lua/oil/config.lua index 0374e43..9102727 100644 --- a/lua/oil/config.lua +++ b/lua/oil/config.lua @@ -1,5 +1,3 @@ ---stylua: ignore - local default_config = { -- Oil will take over directory buffers (e.g. `vim .` or `:e src/`) -- Set to false if you want some other plugin (e.g. netrw) to open when you edit directories. @@ -60,22 +58,22 @@ local default_config = { -- Set to `false` to remove a keymap -- See :help oil-actions for a list of all available actions keymaps = { - ["g?"] = "actions.show_help", + ["g?"] = { "actions.show_help", mode = "n" }, [""] = "actions.select", - [""] = { "actions.select", opts = { vertical = true }, desc = "Open the entry in a vertical split" }, - [""] = { "actions.select", opts = { horizontal = true }, desc = "Open the entry in a horizontal split" }, - [""] = { "actions.select", opts = { tab = true }, desc = "Open the entry in new tab" }, + [""] = { "actions.select", opts = { vertical = true } }, + [""] = { "actions.select", opts = { horizontal = true } }, + [""] = { "actions.select", opts = { tab = true } }, [""] = "actions.preview", - [""] = "actions.close", + [""] = { "actions.close", mode = "n" }, [""] = "actions.refresh", - ["-"] = "actions.parent", - ["_"] = "actions.open_cwd", - ["`"] = "actions.cd", - ["~"] = { "actions.cd", opts = { scope = "tab" }, desc = ":tcd to the current oil directory", mode = "n" }, - ["gs"] = "actions.change_sort", + ["-"] = { "actions.parent", mode = "n" }, + ["_"] = { "actions.open_cwd", mode = "n" }, + ["`"] = { "actions.cd", mode = "n" }, + ["~"] = { "actions.cd", opts = { scope = "tab" }, mode = "n" }, + ["gs"] = { "actions.change_sort", mode = "n" }, ["gx"] = "actions.open_external", - ["g."] = "actions.toggle_hidden", - ["g\\"] = "actions.toggle_trash", + ["g."] = { "actions.toggle_hidden", mode = "n" }, + ["g\\"] = { "actions.toggle_trash", mode = "n" }, }, -- Set to false to disable all of the above keymaps use_default_keymaps = true, diff --git a/lua/oil/keymap_util.lua b/lua/oil/keymap_util.lua index b62756e..04b8066 100644 --- a/lua/oil/keymap_util.lua +++ b/lua/oil/keymap_util.lua @@ -19,7 +19,18 @@ local function resolve(rhs) elseif type(rhs) == "table" then local opts = vim.deepcopy(rhs) -- We support passing in a `callback` key, or using the 1 index as the rhs of the keymap - local callback = resolve(opts.callback or opts[1]) + local callback, parent_opts = resolve(opts.callback or opts[1]) + + -- Fall back to the parent desc, adding the opts as a string if it exists + if parent_opts.desc and not opts.desc then + if opts.opts then + opts.desc = + string.format("%s %s", parent_opts.desc, vim.inspect(opts.opts):gsub("%s+", " ")) + else + opts.desc = parent_opts.desc + end + end + local mode = opts.mode if type(rhs.callback) == "string" then local action_opts, action_mode