Skip to content

Feat - Add open and close tree events #1151

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 15 commits into from
Apr 18, 2022
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
16 changes: 16 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -984,6 +984,22 @@ on_folder_created({handler})
on_folder_removed({handler})
Registers a handler for when a folder is removed.

Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.

*nvim-tree.events.on_tree_open()*
on_tree_open({handler})
Registers a handler for when NvimTree is opened.

Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.

*nvim-tree.events.on_tree_close()*
on_tree_close({handler})
Registers a handler for when NvimTree is closed.

Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.
Expand Down
24 changes: 24 additions & 0 deletions lua/nvim-tree/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ local global_handlers = {}
local Event = {
Ready = "Ready",
NodeRenamed = "NodeRenamed",
TreeOpen = "TreeOpen",
TreeClose = "TreeClose",
FileCreated = "FileCreated",
FileRemoved = "FileRemoved",
FolderCreated = "FolderCreated",
Expand Down Expand Up @@ -60,6 +62,16 @@ function M._dispatch_folder_removed(folder_name)
dispatch(Event.FolderRemoved, { folder_name = folder_name })
end

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

--@private
function M._dispatch_on_tree_close()
dispatch(Event.TreeClose, nil)
end

--Registers a handler for the Ready event.
--@param handler (function) Handler with the signature `function()`
function M.on_nvim_tree_ready(handler)
Expand Down Expand Up @@ -102,4 +114,16 @@ function M.on_folder_removed(handler)
register_handler(Event.FolderRemoved, handler)
end

--Registers a handler for the TreeOpen event.
--@param handler (function) Handler with the signature function(payload)
function M.on_tree_open(handler)
register_handler(Event.TreeOpen, handler)
end

--Registers a handler for the TreeClose event.
--@param handler (function) Handler with the signature function(payload)
function M.on_tree_close(handler)
register_handler(Event.TreeClose, handler)
end

return M
4 changes: 4 additions & 0 deletions lua/nvim-tree/view.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ local a = vim.api

local M = {}

local events = require "nvim-tree.events"

M.View = {
tabpages = {},
hide_root_folder = false,
Expand Down Expand Up @@ -173,6 +175,7 @@ function M.close()
if tree_win == current_win and LAST_FOCUSED_WIN then
a.nvim_set_current_win(LAST_FOCUSED_WIN)
end
events._dispatch_on_tree_close()
return
end
end
Expand All @@ -193,6 +196,7 @@ function M.open(options)
if not opts.focus_tree then
vim.cmd "wincmd p"
end
events._dispatch_on_tree_open()
end

function M.resize(size)
Expand Down