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 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
10 changes: 9 additions & 1 deletion doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2769,13 +2769,21 @@ 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.TreeClose
Invoked after the NvimTree is closed, but before the window is
closed. Dispatched on |WinClosed| event for NvimTree window.
• Note: Handler takes no parameter.

- Event.Resize - When NvimTree is resized.
Expand Down
6 changes: 6 additions & 0 deletions lua/nvim-tree/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ M.Event = {
Ready = "Ready",
WillRenameNode = "WillRenameNode",
NodeRenamed = "NodeRenamed",
TreePreOpen = "TreePreOpen",
TreeOpen = "TreeOpen",
TreeClose = "TreeClose",
WillCreateFile = "WillCreateFile",
Expand Down Expand Up @@ -91,6 +92,11 @@ 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)
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 @@ -288,6 +288,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 Expand Up @@ -413,6 +414,7 @@ end
---@param opts OpenInWinOpts|nil
function M.open_in_win(opts)
opts = opts or { hijack_current_buf = true, resize = true }
events._dispatch_on_tree_pre_open()
if opts.winid and vim.api.nvim_win_is_valid(opts.winid) then
vim.api.nvim_set_current_win(opts.winid)
end
Expand Down
Loading