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 3 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
35 changes: 29 additions & 6 deletions lua/oil/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,12 @@ 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
local preview_win_id = util.get_preview_win()
if preview_win_id then
M.select({ preview = true })
end
lucaseras marked this conversation as resolved.
Show resolved Hide resolved
end

---Restore the buffer that was present when oil was opened
Expand Down Expand Up @@ -523,11 +529,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 +590,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 +624,12 @@ 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)
vim.api.nvim_set_current_win(prev_win)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this line necessary? Is it possible that the cursor has changed windows between line 533 and here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point, yeah I think we can remove that line — the only case where the current window changes is if opts.preview

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 +652,20 @@ M.select = function(opts, callback)
M.close()
end)
end

util.run_after_load(0, function()
local oil = require("oil")
local cursor_entry = oil.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)

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
1 change: 1 addition & 0 deletions lua/oil/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -685,6 +685,7 @@ end
--- refetch nil|boolean Defaults to true
---@param callback nil|fun(err: nil|string)
M.render_buffer_async = function(bufnr, opts, callback)
vim.b[bufnr].oil_ready = false
opts = vim.tbl_deep_extend("keep", opts or {}, {
refetch = true,
})
Expand Down