Skip to content

Commit c3c6544

Browse files
feat(event): add WillCreateFile, WillRemoveFile (#2273)
node. These are mostly going to be useful for implementing lsp file operation actions. Co-authored-by: Alexander Courtis <[email protected]>
1 parent 85ece27 commit c3c6544

File tree

5 files changed

+25
-0
lines changed

5 files changed

+25
-0
lines changed

doc/nvim-tree-lua.txt

+10
Original file line numberDiff line numberDiff line change
@@ -2186,10 +2186,20 @@ e.g. handler for node renamed: >
21862186
handler parameters: ~
21872187
{fname} `{string}` Absolute path to the created file
21882188

2189+
- Event.WillCreateFile
2190+
handler parameters: ~
2191+
{fname} `{string}` Absolute path to the file to be
2192+
created
2193+
21892194
- Event.FileRemoved
21902195
handler parameters: ~
21912196
{fname} `{string}` Absolute path to the removed file.
21922197

2198+
- Event.WillRemoveFile
2199+
handler parameters: ~
2200+
{fname} `{string}` Absolute path to the file to be
2201+
removed
2202+
21932203
- Event.FolderCreated
21942204
handler parameters: ~
21952205
{folder_name} `{string}` Absolute path to the created folder.

lua/nvim-tree/actions/fs/create-file.lua

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
99
local M = {}
1010

1111
local function create_and_notify(file)
12+
events._dispatch_will_create_file(file)
1213
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
1314
if not ok then
1415
notify.error("Couldn't create file " .. file)

lua/nvim-tree/actions/fs/remove-file.lua

+1
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ function M.remove(node)
8181
end
8282
events._dispatch_folder_removed(node.absolute_path)
8383
else
84+
events._dispatch_will_remove_file(node.absolute_path)
8485
local success = vim.loop.fs_unlink(node.absolute_path)
8586
if not success then
8687
return notify.error("Could not remove " .. node.name)

lua/nvim-tree/actions/fs/trash.lua

+1
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ function M.fn(node)
7575
end
7676
end)
7777
else
78+
events._dispatch_will_remove_file(node.absolute_path)
7879
trash_path(function(_, rc)
7980
if rc ~= 0 then
8081
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")

lua/nvim-tree/events.lua

+12
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ M.Event = {
1010
NodeRenamed = "NodeRenamed",
1111
TreeOpen = "TreeOpen",
1212
TreeClose = "TreeClose",
13+
WillCreateFile = "WillCreateFile",
1314
FileCreated = "FileCreated",
15+
WillRemoveFile = "WillRemoveFile",
1416
FileRemoved = "FileRemoved",
1517
FolderCreated = "FolderCreated",
1618
FolderRemoved = "FolderRemoved",
@@ -52,11 +54,21 @@ function M._dispatch_node_renamed(old_name, new_name)
5254
dispatch(M.Event.NodeRenamed, { old_name = old_name, new_name = new_name })
5355
end
5456

57+
--@private
58+
function M._dispatch_will_remove_file(fname)
59+
dispatch(M.Event.WillRemoveFile, { fname = fname })
60+
end
61+
5562
--@private
5663
function M._dispatch_file_removed(fname)
5764
dispatch(M.Event.FileRemoved, { fname = fname })
5865
end
5966

67+
--@private
68+
function M._dispatch_will_create_file(fname)
69+
dispatch(M.Event.WillCreateFile, { fname = fname })
70+
end
71+
6072
--@private
6173
function M._dispatch_file_created(fname)
6274
dispatch(M.Event.FileCreated, { fname = fname })

0 commit comments

Comments
 (0)