Skip to content

Commit 047773e

Browse files
committed
feat: handle aborting hooks
If a Lua function hook returns false at any point, any other registered hooks will be skipped. Furthermore, if that hook was an open_pre hook, the directory will not change, and the post-open hooks will not run. Any other return values from function hooks are ignored.
1 parent c0d5aae commit 047773e

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

CHANGELOG.md

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,14 @@
1-
# Changelog
2-
31
# master
42

3+
# 0.2
4+
5+
* **feat**: handle aborting Lua function hooks
6+
7+
If a Lua function hook returns false, any other registered hooks in the list
8+
will be skipped. Furthermore, if the hook is an open_pre hook and returns
9+
false, the directory will not be changed and any registered post-open hooks
10+
will not be run. This is also to allow more flexibility in hooks.
11+
512
* **feat**: pass workspace name and path to Lua function hooks
613

714
If a hook is a Lua function, the workspace name and path will be passed as
@@ -35,4 +42,4 @@
3542

3643
* **feat**: add `open_pre` hook support.
3744

38-
# v0.1 Initial Release
45+
# 0.1 Initial Release

doc/workspaces.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,11 @@ commands and Lua functions. See |workspaces-examples| for reference. If the
9999
hook is a Lua function, it will be passed the workspace name and workspace
100100
path as parameters.
101101

102+
If a Lua function hook returns false, any other registered hooks in the list
103+
will be skipped. Furthermore, if the hook is an open_pre hook and returns
104+
false, the directory will not be changed and any registered post-open hooks
105+
will not be run.
106+
102107
add ~
103108
run hooks after adding a workspace
104109

lua/workspaces/init.lua

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,14 @@ end
7676

7777
local run_hook = function(hook, name, path)
7878
if type(hook) == "function" then
79-
hook(name, path)
79+
if hook(name, path) == false then return false end
8080
elseif type(hook) == "string" then
8181
vim.cmd(hook)
8282
else
8383
vim.notify(string.format("workspaces.nvim: invalid hook '%s'", hook), levels.ERROR)
8484
end
85+
86+
return true
8587
end
8688

8789
-- given a list of hooks, execute each in the order given
@@ -90,11 +92,13 @@ local run_hooks = function(hooks, name, path)
9092

9193
if type(hooks) == "table" then
9294
for _, hook in ipairs(hooks) do
93-
run_hook(hook, name, path)
95+
if run_hook(hook, name, path) == false then return false end
9496
end
9597
else
96-
run_hook(hooks, name, path)
98+
if run_hook(hooks, name, path) == false then return false end
9799
end
100+
101+
return true
98102
end
99103

100104
local M = {}
@@ -233,9 +237,12 @@ M.open = function(name)
233237
return
234238
end
235239

236-
-- change directory
237-
run_hooks(config.hooks.open_pre, workspace.name, workspace.path)
240+
if run_hooks(config.hooks.open_pre, workspace.name, workspace.path) == false then
241+
-- if any hooks aborted, then do not change directory
242+
return
243+
end
238244

245+
-- change directory
239246
if config.global_cd then
240247
vim.api.nvim_set_current_dir(workspace.path)
241248
else

0 commit comments

Comments
 (0)