Skip to content

feat: add TreePreOpen event #3105

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

Merged
merged 4 commits into from
Apr 20, 2025
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
14 changes: 13 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2769,13 +2769,25 @@ e.g. handler for node renamed: >lua
|nvim_tree_events_kind|

- Event.Ready
When NvimTree has been initialized
When NvimTree has been initialized.
• Note: Handler takes no parameter.

- Event.TreePreOpen
Invoked before the window and buffer for NvimTree are created
or opened. Before |Event.TreeOpen| event.
• Note: Handler takes no parameter.

- Event.TreeOpen
Invoked after the NvimTree is opened.
• Note: Handler takes no parameter.

- Event.TreePreClose
Invoked before the window and buffer for NvimTree are closed.
Before |Event.TreeClose| event.
• Note: Handler takes no parameter.

- Event.TreeClose
Invoked after the NvimTree is closed.
• Note: Handler takes no parameter.

- Event.Resize - When NvimTree is resized.
Expand Down
12 changes: 12 additions & 0 deletions lua/nvim-tree/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ M.Event = {
Ready = "Ready",
WillRenameNode = "WillRenameNode",
NodeRenamed = "NodeRenamed",
TreePreOpen = "TreePreOpen",
TreeOpen = "TreeOpen",
TreePreClose = "TreePreClose",
TreeClose = "TreeClose",
WillCreateFile = "WillCreateFile",
FileCreated = "FileCreated",
Expand Down Expand Up @@ -91,11 +93,21 @@ function M._dispatch_folder_removed(folder_name)
dispatch(M.Event.FolderRemoved, { folder_name = folder_name })
end

--@private
function M._dispatch_on_tree_pre_open()
dispatch(M.Event.TreePreOpen, nil)
end

--@private
function M._dispatch_on_tree_open()
dispatch(M.Event.TreeOpen, nil)
end

--@private
function M._dispatch_on_tree_pre_close()
dispatch(M.Event.TreePreClose, nil)
end

--@private
function M._dispatch_on_tree_close()
dispatch(M.Event.TreeClose, nil)
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/lib.lua
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ function M.open(opts)

local explorer = core.get_explorer()

events._dispatch_on_tree_pre_open()
if should_hijack_current_buf() then
view.close_this_tab_only()
view.open_in_win()
Expand Down
2 changes: 2 additions & 0 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ local function close(tabpage)
return
end
save_tab_state(tabpage)
events._dispatch_on_tree_pre_close()
switch_buf_if_last_buf()
local tree_win = M.get_winnr(tabpage)
local current_win = vim.api.nvim_get_current_win()
Expand Down Expand Up @@ -288,6 +289,7 @@ function M.open(options)

local profile = log.profile_start("view open")

events._dispatch_on_tree_pre_open()
create_buffer()
open_window()
M.resize()
Expand Down
Loading