feat: keymaps can specify mode (#187)
This commit is contained in:
parent
96a334abeb
commit
977da9ac66
6 changed files with 27 additions and 10 deletions
|
|
@ -159,7 +159,7 @@ require("oil").setup({
|
|||
-- Selecting a new/moved/renamed file or directory will prompt you to save changes first
|
||||
prompt_save_on_select_new_entry = true,
|
||||
-- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true })
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" })
|
||||
-- Additionally, if it is a string that matches "actions.<name>",
|
||||
-- it will use the mapping at require("oil.actions").<name>
|
||||
-- Set to `false` to remove a keymap
|
||||
|
|
|
|||
|
|
@ -50,7 +50,7 @@ OPTIONS *oil-option
|
|||
-- Selecting a new/moved/renamed file or directory will prompt you to save changes first
|
||||
prompt_save_on_select_new_entry = true,
|
||||
-- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true })
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" })
|
||||
-- Additionally, if it is a string that matches "actions.<name>",
|
||||
-- it will use the mapping at require("oil.actions").<name>
|
||||
-- Set to `false` to remove a keymap
|
||||
|
|
|
|||
|
|
@ -35,7 +35,7 @@ local default_config = {
|
|||
-- Selecting a new/moved/renamed file or directory will prompt you to save changes first
|
||||
prompt_save_on_select_new_entry = true,
|
||||
-- Keymaps in oil buffer. Can be any value that `vim.keymap.set` accepts OR a table of keymap
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", nowait = true })
|
||||
-- options with a `callback` (e.g. { callback = function() ... end, desc = "", mode = "n" })
|
||||
-- Additionally, if it is a string that matches "actions.<name>",
|
||||
-- it will use the mapping at require("oil.actions").<name>
|
||||
-- Set to `false` to remove a keymap
|
||||
|
|
|
|||
|
|
@ -780,7 +780,7 @@ local function load_oil_buffer(bufnr)
|
|||
-- (e.g. ssh) because it will set up the filetype keybinds at the *beginning* of the loading
|
||||
-- process.
|
||||
vim.bo[bufnr].filetype = "oil"
|
||||
keymap_util.set_keymaps("", config.keymaps, bufnr)
|
||||
keymap_util.set_keymaps(config.keymaps, bufnr)
|
||||
end
|
||||
loading.set_loading(bufnr, true)
|
||||
local winid = vim.api.nvim_get_current_win()
|
||||
|
|
|
|||
|
|
@ -3,26 +3,43 @@ local layout = require("oil.layout")
|
|||
local util = require("oil.util")
|
||||
local M = {}
|
||||
|
||||
---@param rhs string|table|fun()
|
||||
---@return string|fun() rhs
|
||||
---@return table opts
|
||||
---@return string|nil mode
|
||||
local function resolve(rhs)
|
||||
if type(rhs) == "string" and vim.startswith(rhs, "actions.") then
|
||||
return resolve(actions[vim.split(rhs, ".", { plain = true })[2]])
|
||||
elseif type(rhs) == "table" then
|
||||
local opts = vim.deepcopy(rhs)
|
||||
local callback = opts.callback
|
||||
local mode = opts.mode
|
||||
if type(rhs.callback) == "string" then
|
||||
local action_opts, action_mode
|
||||
callback, action_opts, action_mode = resolve(rhs.callback)
|
||||
opts = vim.tbl_extend("keep", opts, action_opts)
|
||||
mode = mode or action_mode
|
||||
end
|
||||
opts.callback = nil
|
||||
return rhs.callback, opts
|
||||
opts.mode = nil
|
||||
return callback, opts, mode
|
||||
else
|
||||
return rhs, {}
|
||||
end
|
||||
return rhs, {}
|
||||
end
|
||||
|
||||
M.set_keymaps = function(mode, keymaps, bufnr)
|
||||
---@param keymaps table<string, string|table|fun()>
|
||||
---@param bufnr integer
|
||||
M.set_keymaps = function(keymaps, bufnr)
|
||||
for k, v in pairs(keymaps) do
|
||||
local rhs, opts = resolve(v)
|
||||
local rhs, opts, mode = resolve(v)
|
||||
if rhs then
|
||||
vim.keymap.set(mode, k, rhs, vim.tbl_extend("keep", { buffer = bufnr }, opts))
|
||||
vim.keymap.set(mode or "", k, rhs, vim.tbl_extend("keep", { buffer = bufnr }, opts))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param keymaps table<string, string|table|fun()>
|
||||
M.show_help = function(keymaps)
|
||||
local rhs_to_lhs = {}
|
||||
local lhs_to_all_lhs = {}
|
||||
|
|
|
|||
|
|
@ -357,7 +357,7 @@ M.initialize = function(bufnr)
|
|||
)
|
||||
end
|
||||
end)
|
||||
keymap_util.set_keymaps("", config.keymaps, bufnr)
|
||||
keymap_util.set_keymaps(config.keymaps, bufnr)
|
||||
end
|
||||
|
||||
---@param adapter oil.Adapter
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue