add event API (#265)

This commit is contained in:
William Boman 2021-04-06 21:13:21 +02:00 committed by GitHub
parent b48274ced0
commit bbb8d6070f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 182 additions and 1 deletions

9
.editorconfig Normal file
View File

@ -0,0 +1,9 @@
root = true
[*]
insert_final_newline = true
end_of_line = lf
[*.lua]
indent_style = space
indent_size = 2

View File

@ -1,6 +1,6 @@
*nvim-tree.lua* A file explorer written in lua
Minimum version of neovim: 0.4.0
Minimum version of neovim: 0.5.0
Author: Yazdani Kiyan <yazdani.kiyan@protonmail.com>
@ -404,3 +404,75 @@ NvimTreeFileNew
NvimTreeFileDeleted
vim:tw=78:ts=8:noet:ft=help:norl:
==============================================================================
EVENTS *nvim-tree-events*
|nvim_tree_events| *nvim_tree_events*
nvim-tree will dispatch events whenever an action is made. These events can be
subscribed to through handler functions. This allows for even further
customization of nvim-tree.
A handler for an event is just a function which receives one argument -
the payload of the event. The payload is different for each event type. Refer
to |nvim_tree_registering_handlers| for more information.
<
|nvim_tree_registering_handlers| *nvim_tree_registering_handlers*
Handlers are registered by calling the `on_*` functions available in the
`require('nvim-tree.events')` module. See |nvim-tree.events|.
For example, registering a handler for when a node is renamed is done like this: >
local events = require('nvim-tree.events')
events.on_node_renamed(function(data)
print("Node renamed from " .. data.old_name .. " to " .. data.new_name)
end)
==============================================================================
Lua module: nvim-tree.events *nvim-tree.events*
*nvim-tree.events.on_node_renamed()*
on_node_renamed({handler})
Registers a handler for when a node is renamed.
• Note: A node can either be a file or a directory.
Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.
*nvim-tree.events.on_file_created()*
on_file_created({handler})
Registers a handler for when a file is created.
Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.
*nvim-tree.events.on_file_removed()*
on_file_removed({handler})
Registers a handler for when a file is removed.
Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.
*nvim-tree.events.on_folder_created()*
on_folder_created({handler})
Registers a handler for when a folder is created.
Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.
*nvim-tree.events.on_folder_removed()*
on_folder_removed({handler})
Registers a handler for when a folder is removed.
Parameters: ~
{handler} (function) Handler function, with the
signature `function(payload)`.

94
lua/nvim-tree/events.lua Normal file
View File

@ -0,0 +1,94 @@
local M = {}
local global_handlers = {}
local Event = {
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_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 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

View File

@ -4,6 +4,7 @@ local open_mode = luv.constants.O_CREAT + luv.constants.O_WRONLY + luv.constants
local utils = require'nvim-tree.utils'
local lib = require'nvim-tree.lib'
local events = require'nvim-tree.events'
local M = {}
local clipboard = {
move = {},
@ -34,6 +35,7 @@ local function create_file(file)
-- this is why we need to chmod to default file permissions
luv.fs_chmod(file, 420)
luv.fs_close(fd)
events._dispatch_file_created(file)
api.nvim_out_write('File '..file..' was properly created\n')
refresh_tree()
end
@ -88,6 +90,7 @@ function M.create(node)
return
end
if idx == num_entries then
events._dispatch_folder_created(add_into..relpath)
api.nvim_out_write('Folder '..add_into..relpath..' was properly created\n')
refresh_tree()
end
@ -242,12 +245,14 @@ function M.remove(node)
if not success then
return api.nvim_err_writeln('Could not remove '..node.name)
end
events._dispatch_folder_removed(node.absolute_path)
api.nvim_out_write(node.name..' has been removed\n')
else
local success = luv.fs_unlink(node.absolute_path)
if not success then
return api.nvim_err_writeln('Could not remove '..node.name)
end
events._dispatch_file_removed(node.absolute_path)
api.nvim_out_write(node.name..' has been removed\n')
clear_buffer(node.absolute_path)
end
@ -280,6 +285,7 @@ function M.rename(with_sub)
end
end
end
events._dispatch_node_renamed(abs_path, new_name)
refresh_tree()
end
end