Skip to content

feat(event): add WillCreateFile, WillRemoveFile #2273

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 2 commits into from
Jun 19, 2023
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: 10 additions & 0 deletions doc/nvim-tree-lua.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2186,10 +2186,20 @@ e.g. handler for node renamed: >
handler parameters: ~
{fname} `{string}` Absolute path to the created file

- Event.WillCreateFile
handler parameters: ~
{fname} `{string}` Absolute path to the file to be
created

- Event.FileRemoved
handler parameters: ~
{fname} `{string}` Absolute path to the removed file.

- Event.WillRemoveFile
handler parameters: ~
{fname} `{string}` Absolute path to the file to be
removed

- Event.FolderCreated
handler parameters: ~
{folder_name} `{string}` Absolute path to the created folder.
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/fs/create-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
local M = {}

local function create_and_notify(file)
events._dispatch_will_create_file(file)
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
if not ok then
notify.error("Couldn't create file " .. file)
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/fs/remove-file.lua
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ function M.remove(node)
end
events._dispatch_folder_removed(node.absolute_path)
else
events._dispatch_will_remove_file(node.absolute_path)
local success = vim.loop.fs_unlink(node.absolute_path)
if not success then
return notify.error("Could not remove " .. node.name)
Expand Down
1 change: 1 addition & 0 deletions lua/nvim-tree/actions/fs/trash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ function M.fn(node)
end
end)
else
events._dispatch_will_remove_file(node.absolute_path)
trash_path(function(_, rc)
if rc ~= 0 then
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")
Expand Down
12 changes: 12 additions & 0 deletions lua/nvim-tree/events.lua
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ M.Event = {
NodeRenamed = "NodeRenamed",
TreeOpen = "TreeOpen",
TreeClose = "TreeClose",
WillCreateFile = "WillCreateFile",
FileCreated = "FileCreated",
WillRemoveFile = "WillRemoveFile",
FileRemoved = "FileRemoved",
FolderCreated = "FolderCreated",
FolderRemoved = "FolderRemoved",
Expand Down Expand Up @@ -52,11 +54,21 @@ function M._dispatch_node_renamed(old_name, new_name)
dispatch(M.Event.NodeRenamed, { old_name = old_name, new_name = new_name })
end

--@private
function M._dispatch_will_remove_file(fname)
dispatch(M.Event.WillRemoveFile, { fname = fname })
end

--@private
function M._dispatch_file_removed(fname)
dispatch(M.Event.FileRemoved, { fname = fname })
end

--@private
function M._dispatch_will_create_file(fname)
dispatch(M.Event.WillCreateFile, { fname = fname })
end

--@private
function M._dispatch_file_created(fname)
dispatch(M.Event.FileCreated, { fname = fname })
Expand Down