Skip to content

feat: add BEFORE_FILE_MOVE BEFORE_FILE_RENAME events #625

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 1 commit into from
Dec 16, 2022
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
2 changes: 2 additions & 0 deletions lua/neo-tree/events/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@ local M = {
AFTER_RENDER = "after_render",
FILE_ADDED = "file_added",
FILE_DELETED = "file_deleted",
BEFORE_FILE_MOVE = "before_file_move",
FILE_MOVED = "file_moved",
FILE_OPEN_REQUESTED = "file_open_requested",
FILE_OPENED = "file_opened",
BEFORE_FILE_RENAME = "before_file_rename",
FILE_RENAMED = "file_renamed",
FS_EVENT = "fs_event",
GIT_EVENT = "git_event",
Expand Down
73 changes: 48 additions & 25 deletions lua/neo-tree/sources/filesystem/lib/fs_actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -157,25 +157,36 @@ M.move_node = function(source, destination, callback, using_root_directory)
)
local _, name = utils.split_path(source)
get_unused_name(destination or source, using_root_directory, function(dest)
create_all_parents(dest)
loop.fs_rename(source, dest, function(err)
if err then
log.error("Could not move the files from", source, "to", dest, ":", err)
return
end
vim.schedule(function()
rename_buffer(source, dest)
end)
vim.schedule(function()
events.fire_event(events.FILE_MOVED, {
source = source,
destination = dest,
})
if callback then
callback(source, dest)
local function move_file()
create_all_parents(dest)
loop.fs_rename(source, dest, function(err)
if err then
log.error("Could not move the files from", source, "to", dest, ":", err)
return
end
vim.schedule(function()
rename_buffer(source, dest)
end)
vim.schedule(function()
events.fire_event(events.FILE_MOVED, {
source = source,
destination = dest,
})
if callback then
callback(source, dest)
end
end)
end)
end)
end
local event_result = events.fire_event(events.BEFORE_FILE_MOVE, {
source = source,
destination = dest,
callback = move_file,
}) or {}
if event_result.handled then
return
end
move_file()
end, 'Move "' .. name .. '" to:')
end

Expand Down Expand Up @@ -530,14 +541,26 @@ M.rename_node = function(path, callback)
log.info("Renamed " .. new_name .. " successfully")
end)

loop.fs_rename(path, destination, function(err)
if err then
log.warn("Could not rename the files")
return
else
complete()
end
end)
local function fs_rename()
loop.fs_rename(path, destination, function(err)
if err then
log.warn("Could not rename the files")
return
else
complete()
end
end)
end

local event_result = events.fire_event(events.BEFORE_FILE_RENAME, {
source = path,
destination = destination,
callback = fs_rename,
}) or {}
if event_result.handled then
return
end
fs_rename()
end)
end

Expand Down