feat: more actions for interacting with preview window (#41)

This commit is contained in:
Steven Arcangeli 2023-01-21 15:06:05 -08:00
parent 4e853eabcb
commit b3c4ff340b
6 changed files with 166 additions and 19 deletions

View file

@ -29,13 +29,65 @@ M.select_split = {
end,
}
---@return nil|integer
local function get_preview_win()
for _, winid in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
if vim.api.nvim_win_is_valid(winid) and vim.api.nvim_win_get_option(winid, "previewwindow") then
return winid
end
end
end
M.preview = {
desc = "Open the entry under the cursor in a preview window",
desc = "Open the entry under the cursor in a preview window, or close the preview window if already open",
callback = function()
local entry = oil.get_cursor_entry()
if not entry then
vim.notify("Could not find entry under cursor", vim.log.levels.ERROR)
return
end
local winid = get_preview_win()
if winid then
local cur_id = vim.w[winid].oil_entry_id
if entry.id == cur_id then
vim.api.nvim_win_close(winid, true)
return
end
end
oil.select({ preview = true })
end,
}
M.preview_scroll_down = {
desc = "Scroll down in the preview window",
callback = function()
local winid = get_preview_win()
if winid then
vim.api.nvim_win_call(winid, function()
vim.cmd.normal({
args = { vim.api.nvim_replace_termcodes("<C-d>", true, true, true) },
bang = true,
})
end)
end
end,
}
M.preview_scroll_up = {
desc = "Scroll up in the preview window",
callback = function()
local winid = get_preview_win()
if winid then
vim.api.nvim_win_call(winid, function()
vim.cmd.normal({
args = { vim.api.nvim_replace_termcodes("<C-u>", true, true, true) },
bang = true,
})
end)
end
end,
}
M.parent = {
desc = "Navigate to the parent path",
callback = oil.open,
@ -71,7 +123,7 @@ M.tcd = {
}
M.open_cwd = {
desc = "Open oil in Neovim's cwd",
desc = "Open oil in Neovim's current working directory",
callback = function()
oil.open(vim.fn.getcwd())
end,
@ -95,7 +147,7 @@ M.open_terminal = {
}
M.refresh = {
desc = "Refresh directory",
desc = "Refresh current directory list",
callback = function()
if vim.bo.modified then
local ok, choice = pcall(vim.fn.confirm, "Discard changes?", "No\nYes")
@ -134,4 +186,19 @@ M.open_cmdline = {
end,
}
---List actions for documentation generation
---@private
M._get_actions = function()
local ret = {}
for name, action in pairs(M) do
if type(action) == "table" and action.desc then
table.insert(ret, {
name = name,
desc = action.desc,
})
end
end
return ret
end
return M

View file

@ -26,9 +26,10 @@ local default_config = {
skip_confirm_for_simple_edits = false,
-- 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 })
-- Additionally, if it is a string that matches "action.<name>",
-- it will use the mapping at require("oil.action").<name>
-- 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
-- See :help oil-actions for a list of all available actions
keymaps = {
["g?"] = "actions.show_help",
["<CR>"] = "actions.select",

View file

@ -407,6 +407,7 @@ M.select = function(opts)
})
if opts.preview then
vim.api.nvim_win_set_option(0, "previewwindow", true)
vim.api.nvim_win_set_var(0, "oil_entry_id", entry.id)
vim.api.nvim_set_current_win(prev_win)
end
-- Set opts.split so that for every entry after the first, we do a split