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 <alex@courtis.org>
This commit is contained in:
Svetlozar Iliev 2023-06-19 03:00:55 +03:00 committed by GitHub
parent 85ece277bc
commit c3c6544ee0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 25 additions and 0 deletions

View File

@ -2186,10 +2186,20 @@ e.g. handler for node renamed: >
handler parameters: ~ handler parameters: ~
{fname} `{string}` Absolute path to the created file {fname} `{string}` Absolute path to the created file
- Event.WillCreateFile
handler parameters: ~
{fname} `{string}` Absolute path to the file to be
created
- Event.FileRemoved - Event.FileRemoved
handler parameters: ~ handler parameters: ~
{fname} `{string}` Absolute path to the removed file. {fname} `{string}` Absolute path to the removed file.
- Event.WillRemoveFile
handler parameters: ~
{fname} `{string}` Absolute path to the file to be
removed
- Event.FolderCreated - Event.FolderCreated
handler parameters: ~ handler parameters: ~
{folder_name} `{string}` Absolute path to the created folder. {folder_name} `{string}` Absolute path to the created folder.

View File

@ -9,6 +9,7 @@ local find_file = require("nvim-tree.actions.finders.find-file").fn
local M = {} local M = {}
local function create_and_notify(file) local function create_and_notify(file)
events._dispatch_will_create_file(file)
local ok, fd = pcall(vim.loop.fs_open, file, "w", 420) local ok, fd = pcall(vim.loop.fs_open, file, "w", 420)
if not ok then if not ok then
notify.error("Couldn't create file " .. file) notify.error("Couldn't create file " .. file)

View File

@ -81,6 +81,7 @@ function M.remove(node)
end end
events._dispatch_folder_removed(node.absolute_path) events._dispatch_folder_removed(node.absolute_path)
else else
events._dispatch_will_remove_file(node.absolute_path)
local success = vim.loop.fs_unlink(node.absolute_path) local success = vim.loop.fs_unlink(node.absolute_path)
if not success then if not success then
return notify.error("Could not remove " .. node.name) return notify.error("Could not remove " .. node.name)

View File

@ -75,6 +75,7 @@ function M.fn(node)
end end
end) end)
else else
events._dispatch_will_remove_file(node.absolute_path)
trash_path(function(_, rc) trash_path(function(_, rc)
if rc ~= 0 then if rc ~= 0 then
notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash") notify.warn("trash failed: " .. err_msg .. "; please see :help nvim-tree.trash")

View File

@ -10,7 +10,9 @@ M.Event = {
NodeRenamed = "NodeRenamed", NodeRenamed = "NodeRenamed",
TreeOpen = "TreeOpen", TreeOpen = "TreeOpen",
TreeClose = "TreeClose", TreeClose = "TreeClose",
WillCreateFile = "WillCreateFile",
FileCreated = "FileCreated", FileCreated = "FileCreated",
WillRemoveFile = "WillRemoveFile",
FileRemoved = "FileRemoved", FileRemoved = "FileRemoved",
FolderCreated = "FolderCreated", FolderCreated = "FolderCreated",
FolderRemoved = "FolderRemoved", FolderRemoved = "FolderRemoved",
@ -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 }) dispatch(M.Event.NodeRenamed, { old_name = old_name, new_name = new_name })
end end
--@private
function M._dispatch_will_remove_file(fname)
dispatch(M.Event.WillRemoveFile, { fname = fname })
end
--@private --@private
function M._dispatch_file_removed(fname) function M._dispatch_file_removed(fname)
dispatch(M.Event.FileRemoved, { fname = fname }) dispatch(M.Event.FileRemoved, { fname = fname })
end end
--@private
function M._dispatch_will_create_file(fname)
dispatch(M.Event.WillCreateFile, { fname = fname })
end
--@private --@private
function M._dispatch_file_created(fname) function M._dispatch_file_created(fname)
dispatch(M.Event.FileCreated, { fname = fname }) dispatch(M.Event.FileCreated, { fname = fname })