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:
Lucas Eras Paiva 2024-01-21 22:32:02 -06:00 committed by GitHub
parent f0315c101f
commit bf753c3e3f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 53 additions and 6 deletions

View file

@ -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

View file

@ -774,4 +774,26 @@ M.get_visual_range = function()
return { start_lnum = start_lnum, end_lnum = end_lnum }
end
---@param bufnr integer
---@param callback fun()
M.run_after_load = function(bufnr, callback)
if bufnr == 0 then
bufnr = vim.api.nvim_get_current_buf()
end
if vim.b[bufnr].oil_ready then
callback()
else
local autocmd_id
autocmd_id = vim.api.nvim_create_autocmd("User", {
pattern = "OilEnter",
callback = function(args)
if args.data.buf == bufnr then
callback()
vim.api.nvim_del_autocmd(autocmd_id)
end
end,
})
end
end
return M