Skip to content

Commit

Permalink
feat: handle aborting hooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natecraddock committed Feb 10, 2022
1 parent c0d5aae commit 047773e
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 8 deletions.
13 changes: 10 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
# Changelog

# master

# 0.2

* **feat**: handle aborting Lua function hooks

If a Lua function hook returns false, any other registered hooks in the list
will be skipped. Furthermore, if the hook is an open_pre hook and returns
false, the directory will not be changed and any registered post-open hooks
will not be run. This is also to allow more flexibility in hooks.

* **feat**: pass workspace name and path to Lua function hooks

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

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

# v0.1 Initial Release
# 0.1 Initial Release
5 changes: 5 additions & 0 deletions doc/workspaces.txt
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,11 @@ commands and Lua functions. See |workspaces-examples| for reference. If the
hook is a Lua function, it will be passed the workspace name and workspace
path as parameters.

If a Lua function hook returns false, any other registered hooks in the list
will be skipped. Furthermore, if the hook is an open_pre hook and returns
false, the directory will not be changed and any registered post-open hooks
will not be run.

add ~
run hooks after adding a workspace

Expand Down
17 changes: 12 additions & 5 deletions lua/workspaces/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,14 @@ end

local run_hook = function(hook, name, path)
if type(hook) == "function" then
hook(name, path)
if hook(name, path) == false then return false end
elseif type(hook) == "string" then
vim.cmd(hook)
else
vim.notify(string.format("workspaces.nvim: invalid hook '%s'", hook), levels.ERROR)
end

return true
end

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

if type(hooks) == "table" then
for _, hook in ipairs(hooks) do
run_hook(hook, name, path)
if run_hook(hook, name, path) == false then return false end
end
else
run_hook(hooks, name, path)
if run_hook(hooks, name, path) == false then return false end
end

return true
end

local M = {}
Expand Down Expand Up @@ -233,9 +237,12 @@ M.open = function(name)
return
end

-- change directory
run_hooks(config.hooks.open_pre, workspace.name, workspace.path)
if run_hooks(config.hooks.open_pre, workspace.name, workspace.path) == false then
-- if any hooks aborted, then do not change directory
return
end

-- change directory
if config.global_cd then
vim.api.nvim_set_current_dir(workspace.path)
else
Expand Down

0 comments on commit 047773e

Please sign in to comment.