forked from nvim-tree/nvim-tree.lua
-
Notifications
You must be signed in to change notification settings - Fork 1
/
events.lua
106 lines (87 loc) · 3.22 KB
/
events.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
local M = {}
local global_handlers = {}
local Event = {
Ready = 'Ready',
NodeRenamed = 'NodeRenamed',
FileCreated = 'FileCreated',
FileRemoved = 'FileRemoved',
FolderCreated = 'FolderCreated',
FolderRemoved = 'FolderRemoved',
}
local function get_handlers(event_name)
return global_handlers[event_name] or {}
end
local function register_handler(event_name, handler)
local handlers = get_handlers(event_name)
table.insert(handlers, handler)
global_handlers[event_name] = handlers
end
local function dispatch(event_name, payload)
for _, handler in pairs(get_handlers(event_name)) do
local success, error = pcall(handler, payload)
if not success then
vim.api.nvim_err_writeln('Handler for event ' .. event_name .. ' errored. ' .. vim.inspect(error))
end
end
end
--@private
function M._dispatch_ready()
dispatch(Event.Ready)
end
--@private
function M._dispatch_node_renamed(old_name, new_name)
dispatch(Event.NodeRenamed, {old_name=old_name, new_name=new_name})
end
--@private
function M._dispatch_file_removed(fname)
dispatch(Event.FileRemoved, {fname=fname})
end
--@private
function M._dispatch_file_created(fname)
dispatch(Event.FileCreated, {fname=fname})
end
--@private
function M._dispatch_folder_created(folder_name)
dispatch(Event.FolderCreated, {folder_name=folder_name})
end
--@private
function M._dispatch_folder_removed(folder_name)
dispatch(Event.FolderRemoved, {folder_name=folder_name})
end
--Registers a handler for the Ready event.
--@param handler (function) Handler with the signature `function()`
function M.on_nvim_tree_ready(handler)
register_handler(Event.Ready, handler)
end
--Registers a handler for the NodeRenamed event.
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
-- - old_name (string) Absolute path to the old node location.
-- - new_name (string) Absolute path to the new node location.
function M.on_node_renamed(handler)
register_handler(Event.NodeRenamed, handler)
end
--Registers a handler for the FileCreated event.
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
-- - fname (string) Absolute path to the created file.
function M.on_file_created(handler)
register_handler(Event.FileCreated, handler)
end
--Registers a handler for the FileRemoved event.
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
-- - fname (string) Absolute path to the removed file.
function M.on_file_removed(handler)
register_handler(Event.FileRemoved, handler)
end
--Registers a handler for the FolderCreated event.
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
-- - folder_name (string) Absolute path to the created folder.
function M.on_folder_created(handler)
register_handler(Event.FolderCreated, handler)
end
--Registers a handler for the FolderRemoved event.
--@param handler (function) Handler with the signature function(payload), where payload is a table containing:
-- - folder_name (string) Absolute path to the removed folder.
function M.on_folder_removed(handler)
register_handler(Event.FolderRemoved, handler)
end
return M