Skip to content

Commit

Permalink
feat: do not close preview when switching dirs (#277)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
lucaseras and stevearc authored Jan 22, 2024
1 parent f0315c1 commit bf753c3
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 6 deletions.
37 changes: 31 additions & 6 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -643,6 +665,9 @@ M.select = function(opts, callback)
M.close()
end)
end

update_preview_window()

finish()
end)
end
Expand Down
22 changes: 22 additions & 0 deletions lua/oil/util.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit bf753c3

Please sign in to comment.