Skip to content

Commit 29239d5

Browse files
committed
feat: emit OilReadPost user event after each buffer render
Problem: third-party plugins like oil-git-status.nvim had no way to know when an oil buffer was re-rendered after a filesystem change, causing their decorations to be cleared with no signal to refresh. Solution: emit a User OilReadPost autocmd after every successful render_buffer_async call. Also document all oil user events (OilEnter, OilReadPost, OilMutationComplete) in oil.txt since none were previously documented. Cherry-picked from: stevearc#723
1 parent 2228f80 commit 29239d5

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

doc/oil.txt

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,6 +777,45 @@ OilTrash *hl-OilTras
777777
OilTrashSourcePath *hl-OilTrashSourcePath*
778778
Virtual text that shows the original path of file in the trash
779779

780+
--------------------------------------------------------------------------------
781+
EVENTS *oil-events*
782+
783+
Oil emits the following User autocmd events:
784+
785+
OilEnter *OilEnter*
786+
Fired once when an oil buffer finishes its initial render and is ready for
787+
interaction. The autocmd `data` table contains `{ buf = <bufnr> }`.
788+
789+
Example: >lua
790+
vim.api.nvim_create_autocmd("User", {
791+
pattern = "OilEnter",
792+
callback = function(args)
793+
-- args.data.buf is the oil buffer number
794+
end,
795+
})
796+
<
797+
798+
OilReadPost *OilReadPost*
799+
Fired after every successful render of an oil buffer, including re-renders
800+
triggered by filesystem changes or manual refresh. This fires in addition
801+
to OilEnter on the initial render.
802+
803+
Useful for third-party plugins that need to update decorations (e.g., git
804+
status columns) whenever the buffer contents change.
805+
806+
Example: >lua
807+
vim.api.nvim_create_autocmd("User", {
808+
pattern = "OilReadPost",
809+
callback = function(args)
810+
-- args.data.buf is the oil buffer number
811+
end,
812+
})
813+
<
814+
815+
OilMutationComplete *OilMutationComplete*
816+
Fired after a mutation (file create, delete, rename, move, copy) finishes
817+
executing.
818+
780819
--------------------------------------------------------------------------------
781820
TRASH *oil-trash*
782821

lua/oil/view.lua

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,19 @@ local pending_renders = {}
867867
---@param opts nil|table
868868
--- refetch nil|boolean Defaults to true
869869
---@param callback nil|fun(err: nil|string)
870-
M.render_buffer_async = function(bufnr, opts, callback)
870+
M.render_buffer_async = function(bufnr, opts, caller_callback)
871+
local function callback(err)
872+
if not err then
873+
vim.api.nvim_exec_autocmds(
874+
"User",
875+
{ pattern = "OilReadPost", modeline = false, data = { buf = bufnr } }
876+
)
877+
end
878+
if caller_callback then
879+
caller_callback(err)
880+
end
881+
end
882+
871883
opts = vim.tbl_deep_extend("keep", opts or {}, {
872884
refetch = true,
873885
})

0 commit comments

Comments
 (0)