From b538e896d26e4cf5092660d0c9944f75291c8366 Mon Sep 17 00:00:00 2001 From: asmodeus Date: Wed, 14 Jun 2023 11:58:10 +0300 Subject: [PATCH] Add new will-action events to complement the existing one for rename node. These are mostly going to be useful for implementing lsp file operation actions. --- doc/nvim-tree-lua.txt | 10 ++++++++++ lua/nvim-tree/actions/fs/create-file.lua | 1 + lua/nvim-tree/actions/fs/remove-file.lua | 1 + lua/nvim-tree/actions/fs/trash.lua | 1 + lua/nvim-tree/events.lua | 12 ++++++++++++ 5 files changed, 25 insertions(+) diff --git a/doc/nvim-tree-lua.txt b/doc/nvim-tree-lua.txt index f16683e6578..422fedebcee 100644 --- a/doc/nvim-tree-lua.txt +++ b/doc/nvim-tree-lua.txt @@ -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. diff --git a/lua/nvim-tree/actions/fs/create-file.lua b/lua/nvim-tree/actions/fs/create-file.lua index 74f4ff37565..a510d30119f 100644 --- a/lua/nvim-tree/actions/fs/create-file.lua +++ b/lua/nvim-tree/actions/fs/create-file.lua @@ -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) diff --git a/lua/nvim-tree/actions/fs/remove-file.lua b/lua/nvim-tree/actions/fs/remove-file.lua index a28c916610d..a8fddf22e45 100644 --- a/lua/nvim-tree/actions/fs/remove-file.lua +++ b/lua/nvim-tree/actions/fs/remove-file.lua @@ -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) diff --git a/lua/nvim-tree/actions/fs/trash.lua b/lua/nvim-tree/actions/fs/trash.lua index 137468ce8e7..954165ea2ad 100644 --- a/lua/nvim-tree/actions/fs/trash.lua +++ b/lua/nvim-tree/actions/fs/trash.lua @@ -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") diff --git a/lua/nvim-tree/events.lua b/lua/nvim-tree/events.lua index 4e73841d3bc..786467c03f9 100644 --- a/lua/nvim-tree/events.lua +++ b/lua/nvim-tree/events.lua @@ -10,7 +10,9 @@ M.Event = { NodeRenamed = "NodeRenamed", TreeOpen = "TreeOpen", TreeClose = "TreeClose", + WillCreateFile = "WillCreateFile", FileCreated = "FileCreated", + WillRemoveFile = "WillRemoveFile", FileRemoved = "FileRemoved", FolderCreated = "FolderCreated", 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 }) 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 })