Skip to content

Commit cff5a10

Browse files
authored
feat - add open and close tree events (#1151)
1 parent ff6ef58 commit cff5a10

File tree

3 files changed

+44
-0
lines changed

3 files changed

+44
-0
lines changed

doc/nvim-tree-lua.txt

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,22 @@ on_folder_created({handler})
988988
on_folder_removed({handler})
989989
Registers a handler for when a folder is removed.
990990

991+
Parameters: ~
992+
{handler} (function) Handler function, with the
993+
signature `function(payload)`.
994+
995+
*nvim-tree.events.on_tree_open()*
996+
on_tree_open({handler})
997+
Registers a handler for when NvimTree is opened.
998+
999+
Parameters: ~
1000+
{handler} (function) Handler function, with the
1001+
signature `function(payload)`.
1002+
1003+
*nvim-tree.events.on_tree_close()*
1004+
on_tree_close({handler})
1005+
Registers a handler for when NvimTree is closed.
1006+
9911007
Parameters: ~
9921008
{handler} (function) Handler function, with the
9931009
signature `function(payload)`.

lua/nvim-tree/events.lua

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ local global_handlers = {}
55
local Event = {
66
Ready = "Ready",
77
NodeRenamed = "NodeRenamed",
8+
TreeOpen = "TreeOpen",
9+
TreeClose = "TreeClose",
810
FileCreated = "FileCreated",
911
FileRemoved = "FileRemoved",
1012
FolderCreated = "FolderCreated",
@@ -60,6 +62,16 @@ function M._dispatch_folder_removed(folder_name)
6062
dispatch(Event.FolderRemoved, { folder_name = folder_name })
6163
end
6264

65+
--@private
66+
function M._dispatch_on_tree_open()
67+
dispatch(Event.TreeOpen, nil)
68+
end
69+
70+
--@private
71+
function M._dispatch_on_tree_close()
72+
dispatch(Event.TreeClose, nil)
73+
end
74+
6375
--Registers a handler for the Ready event.
6476
--@param handler (function) Handler with the signature `function()`
6577
function M.on_nvim_tree_ready(handler)
@@ -102,4 +114,16 @@ function M.on_folder_removed(handler)
102114
register_handler(Event.FolderRemoved, handler)
103115
end
104116

117+
--Registers a handler for the TreeOpen event.
118+
--@param handler (function) Handler with the signature function(payload)
119+
function M.on_tree_open(handler)
120+
register_handler(Event.TreeOpen, handler)
121+
end
122+
123+
--Registers a handler for the TreeClose event.
124+
--@param handler (function) Handler with the signature function(payload)
125+
function M.on_tree_close(handler)
126+
register_handler(Event.TreeClose, handler)
127+
end
128+
105129
return M

lua/nvim-tree/view.lua

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ local a = vim.api
22

33
local M = {}
44

5+
local events = require "nvim-tree.events"
6+
57
M.View = {
68
tabpages = {},
79
hide_root_folder = false,
@@ -173,6 +175,7 @@ function M.close()
173175
if tree_win == current_win and LAST_FOCUSED_WIN then
174176
a.nvim_set_current_win(LAST_FOCUSED_WIN)
175177
end
178+
events._dispatch_on_tree_close()
176179
return
177180
end
178181
end
@@ -193,6 +196,7 @@ function M.open(options)
193196
if not opts.focus_tree then
194197
vim.cmd "wincmd p"
195198
end
199+
events._dispatch_on_tree_open()
196200
end
197201

198202
function M.resize(size)

0 commit comments

Comments
 (0)