Skip to content

Commit

Permalink
refactor: add helper method to run function after oil buffer loads
Browse files Browse the repository at this point in the history
  • Loading branch information
stevearc committed Jan 10, 2024
1 parent 381b5a4 commit 5220fa5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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 @@ -710,6 +710,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

0 comments on commit 5220fa5

Please sign in to comment.