feat: do not close preview when switching dirs (#277)
* feat: do not close preview when cd into dir * refactor: add helper method to run function after oil buffer loads * Keep preview window open * Remove some test logic * Use `run_after_load` when moving to parent directory * Remove unnecessary update of current window * refactor: create helper function for updating preview --------- Co-authored-by: Steven Arcangeli <stevearc@stevearc.com>
This commit is contained in:
parent
f0315c101f
commit
bf753c3e3f
2 changed files with 53 additions and 6 deletions
|
|
@ -366,6 +366,23 @@ M.toggle_float = function(dir)
|
|||
end
|
||||
end
|
||||
|
||||
---@param oil_bufnr? integer
|
||||
local function update_preview_window(oil_bufnr)
|
||||
oil_bufnr = oil_bufnr or 0
|
||||
local util = require("oil.util")
|
||||
util.run_after_load(oil_bufnr, function()
|
||||
local cursor_entry = M.get_cursor_entry()
|
||||
if cursor_entry then
|
||||
local preview_win_id = util.get_preview_win()
|
||||
if preview_win_id then
|
||||
if cursor_entry.id ~= vim.w[preview_win_id].oil_entry_id then
|
||||
M.select({ preview = true })
|
||||
end
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
---Open oil browser for a directory
|
||||
---@param dir nil|string When nil, open the parent of the current buffer, or the cwd if current buffer is not a file
|
||||
M.open = function(dir)
|
||||
|
|
@ -384,6 +401,9 @@ M.open = function(dir)
|
|||
if config.buf_options.buflisted ~= nil then
|
||||
vim.api.nvim_buf_set_option(0, "buflisted", config.buf_options.buflisted)
|
||||
end
|
||||
|
||||
-- If preview window exists, update its content
|
||||
update_preview_window()
|
||||
end
|
||||
|
||||
---Restore the buffer that was present when oil was opened
|
||||
|
|
@ -523,11 +543,7 @@ M.select = function(opts, callback)
|
|||
end
|
||||
end
|
||||
|
||||
-- Close the preview window if we're not previewing the selection
|
||||
local preview_win = util.get_preview_win()
|
||||
if not opts.preview and preview_win then
|
||||
vim.api.nvim_win_close(preview_win, true)
|
||||
end
|
||||
local prev_win = vim.api.nvim_get_current_win()
|
||||
|
||||
local scheme, dir = util.parse_url(bufname)
|
||||
|
|
@ -588,15 +604,16 @@ M.select = function(opts, callback)
|
|||
emsg_silent = true,
|
||||
}
|
||||
local filebufnr = vim.fn.bufadd(normalized_url)
|
||||
local entry_is_file = not vim.endswith(normalized_url, "/")
|
||||
|
||||
if opts.preview then
|
||||
-- If we're previewing a file that hasn't been opened yet, make sure it gets deleted after
|
||||
-- we close the window
|
||||
if not vim.endswith(normalized_url, "/") and vim.fn.bufloaded(filebufnr) == 0 then
|
||||
if entry_is_file and vim.fn.bufloaded(filebufnr) == 0 then
|
||||
vim.bo[filebufnr].bufhidden = "wipe"
|
||||
vim.b[filebufnr].oil_preview_buffer = true
|
||||
end
|
||||
elseif not vim.endswith(normalized_url, "/") then
|
||||
elseif entry_is_file then
|
||||
-- The :buffer command doesn't set buflisted=true
|
||||
-- So do that for non-diretory-buffers
|
||||
vim.bo[filebufnr].buflisted = true
|
||||
|
|
@ -621,6 +638,11 @@ M.select = function(opts, callback)
|
|||
args = { filebufnr },
|
||||
mods = mods,
|
||||
})
|
||||
|
||||
if not opts.preview and preview_win and entry_is_file then
|
||||
vim.api.nvim_win_close(preview_win, true)
|
||||
end
|
||||
|
||||
if opts.preview then
|
||||
vim.api.nvim_set_option_value("previewwindow", true, { scope = "local", win = 0 })
|
||||
vim.w.oil_entry_id = entry.id
|
||||
|
|
@ -643,6 +665,9 @@ M.select = function(opts, callback)
|
|||
M.close()
|
||||
end)
|
||||
end
|
||||
|
||||
update_preview_window()
|
||||
|
||||
finish()
|
||||
end)
|
||||
end
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue