Skip to content

Commit c5a5a3d

Browse files
authored
feat: add BEFORE_FILE_MOVE BEFORE_FILE_RENAME events (#625)
closes #623
1 parent 52a9efd commit c5a5a3d

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

lua/neo-tree/events/init.lua

+2
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ local M = {
99
AFTER_RENDER = "after_render",
1010
FILE_ADDED = "file_added",
1111
FILE_DELETED = "file_deleted",
12+
BEFORE_FILE_MOVE = "before_file_move",
1213
FILE_MOVED = "file_moved",
1314
FILE_OPEN_REQUESTED = "file_open_requested",
1415
FILE_OPENED = "file_opened",
16+
BEFORE_FILE_RENAME = "before_file_rename",
1517
FILE_RENAMED = "file_renamed",
1618
FS_EVENT = "fs_event",
1719
GIT_EVENT = "git_event",

lua/neo-tree/sources/filesystem/lib/fs_actions.lua

+48-25
Original file line numberDiff line numberDiff line change
@@ -157,25 +157,36 @@ M.move_node = function(source, destination, callback, using_root_directory)
157157
)
158158
local _, name = utils.split_path(source)
159159
get_unused_name(destination or source, using_root_directory, function(dest)
160-
create_all_parents(dest)
161-
loop.fs_rename(source, dest, function(err)
162-
if err then
163-
log.error("Could not move the files from", source, "to", dest, ":", err)
164-
return
165-
end
166-
vim.schedule(function()
167-
rename_buffer(source, dest)
168-
end)
169-
vim.schedule(function()
170-
events.fire_event(events.FILE_MOVED, {
171-
source = source,
172-
destination = dest,
173-
})
174-
if callback then
175-
callback(source, dest)
160+
local function move_file()
161+
create_all_parents(dest)
162+
loop.fs_rename(source, dest, function(err)
163+
if err then
164+
log.error("Could not move the files from", source, "to", dest, ":", err)
165+
return
176166
end
167+
vim.schedule(function()
168+
rename_buffer(source, dest)
169+
end)
170+
vim.schedule(function()
171+
events.fire_event(events.FILE_MOVED, {
172+
source = source,
173+
destination = dest,
174+
})
175+
if callback then
176+
callback(source, dest)
177+
end
178+
end)
177179
end)
178-
end)
180+
end
181+
local event_result = events.fire_event(events.BEFORE_FILE_MOVE, {
182+
source = source,
183+
destination = dest,
184+
callback = move_file,
185+
}) or {}
186+
if event_result.handled then
187+
return
188+
end
189+
move_file()
179190
end, 'Move "' .. name .. '" to:')
180191
end
181192

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

533-
loop.fs_rename(path, destination, function(err)
534-
if err then
535-
log.warn("Could not rename the files")
536-
return
537-
else
538-
complete()
539-
end
540-
end)
544+
local function fs_rename()
545+
loop.fs_rename(path, destination, function(err)
546+
if err then
547+
log.warn("Could not rename the files")
548+
return
549+
else
550+
complete()
551+
end
552+
end)
553+
end
554+
555+
local event_result = events.fire_event(events.BEFORE_FILE_RENAME, {
556+
source = path,
557+
destination = destination,
558+
callback = fs_rename,
559+
}) or {}
560+
if event_result.handled then
561+
return
562+
end
563+
fs_rename()
541564
end)
542565
end
543566

0 commit comments

Comments
 (0)