Skip to content

Commit

Permalink
feat(files): add autocommand events immediately before performing fil…
Browse files Browse the repository at this point in the history
…e actions
  • Loading branch information
mehalter committed Feb 19, 2025
1 parent a84b7e5 commit 0dbf651
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
10 changes: 10 additions & 0 deletions doc/mini-files.txt
Original file line number Diff line number Diff line change
Expand Up @@ -354,14 +354,24 @@ with the following information:

File action events ~

- `MiniFilesActionCreatePre` - before entry is created.

- `MiniFilesActionCreate` - after entry is successfully created.

- `MiniFilesActionDeletePre` - before entry is deleted.

- `MiniFilesActionDelete` - after entry is successfully deleted.

- `MiniFilesActionRenamePre` - before entry is renamed.

- `MiniFilesActionRename` - after entry is successfully renamed.

- `MiniFilesActionCopyPre` - before entry is copied.

- `MiniFilesActionCopy` - after entry is successfully copied.

- `MiniFilesActionMovePre` - before entry is moved.

- `MiniFilesActionMove` - after entry is successfully moved.

Callback for each file action event will receive `data` field
Expand Down
21 changes: 17 additions & 4 deletions lua/mini/files.lua
Original file line number Diff line number Diff line change
Expand Up @@ -350,14 +350,24 @@
---
--- File action events ~
---
--- - `MiniFilesActionCreatePre` - before entry is created.
---
--- - `MiniFilesActionCreate` - after entry is successfully created.
---
--- - `MiniFilesActionDeletePre` - before entry is deleted.
---
--- - `MiniFilesActionDelete` - after entry is successfully deleted.
---
--- - `MiniFilesActionRenamePre` - before entry is renamed.
---
--- - `MiniFilesActionRename` - after entry is successfully renamed.
---
--- - `MiniFilesActionCopyPre` - before entry is copied.
---
--- - `MiniFilesActionCopy` - after entry is successfully copied.
---
--- - `MiniFilesActionMovePre` - before entry is moved.
---
--- - `MiniFilesActionMove` - after entry is successfully moved.
---
--- Callback for each file action event will receive `data` field
Expand Down Expand Up @@ -2676,13 +2686,16 @@ end
H.fs_actions_apply = function(fs_actions)
for i = 1, #fs_actions do
local diff, action = fs_actions[i], fs_actions[i].action
local to = action == 'create' and diff.to:gsub('/$', '') or diff.to
local data = { action = action, from = diff.from, to = to }
local action_titlecase = action:sub(1, 1):upper() .. action:sub(2)
local event = 'MiniFilesAction' .. action_titlecase
-- Trigger pre event before action
H.trigger_event(event .. 'Pre', data)
local ok, success = pcall(H.fs_do[action], diff.from, diff.to)
if ok and success then
-- Trigger event
local to = action == 'create' and diff.to:gsub('/$', '') or diff.to
local data = { action = action, from = diff.from, to = to }
local action_titlecase = action:sub(1, 1):upper() .. action:sub(2)
H.trigger_event('MiniFilesAction' .. action_titlecase, data)
H.trigger_event(event, data)

-- Modify later actions to account for file movement
local has_moved = to ~= nil and not (action == 'copy' or action == 'create')
Expand Down

0 comments on commit 0dbf651

Please sign in to comment.