Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: do not close preview when cd into dir #277

Merged
merged 7 commits into from
Jan 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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