Skip to content

Commit c0d5aae

Browse files
committed
feat: pass workspace name and path to hooks
Function hooks now accept two arguments, the name and path of the workspace. This works for add, remove, open_pre, and open hooks. This can be used to modify behavior depending on the workspace.
1 parent dfdb319 commit c0d5aae

3 files changed

Lines changed: 25 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,37 @@
22

33
# master
44

5-
* **feat** add `sort` config option
5+
* **feat**: pass workspace name and path to Lua function hooks
6+
7+
If a hook is a Lua function, the workspace name and path will be passed as
8+
arguments to the function. This allows hooks to respond differently depending
9+
on the name and path of the workspace if desired.
10+
11+
* **feat**: add `sort` config option
612

713
Set to true by default. Sorts the list of workspaces by name after loading
814
from the workspaces file. All lists of workspaces (commands, completions,
915
.get() api) will be sorted.
1016

11-
* **feat** add `global_cd` config option
17+
* **feat**: add `global_cd` config option
1218

1319
This may be used to only change directory in the current window rather than
1420
for the entire neovim process.
1521

16-
* **deprecated** `:Workspaces [add|remove|open|list]` commands
22+
* **deprecated**: `:Workspaces [add|remove|open|list]` commands
1723

1824
use `:Workspaces[Add|Remove|Open|List]`. The old command will work until
1925
version 1.0, but it will warn each time it is called.
2026

21-
* **feat** improved command completion
27+
* **feat**: improved command completion
2228

2329
This introduces `:WorkspacesAdd`, `:WorkspacesRemove`, `:WorkspacesList`, and
2430
`:WorkspacesOpen` commands. See deprecation warning above. The new commands
2531
offer path completion, are more standard, and are easier to maintain.
2632

27-
* **feat** add `workspaces.name()` api function to query the current workpace
33+
* **feat**: add `workspaces.name()` api function to query the current workspace
2834
name.
2935

30-
* **feat** add `open_pre` hook support.
36+
* **feat**: add `open_pre` hook support.
3137

3238
# v0.1 Initial Release

doc/workspaces.txt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ There are 4 types of hooks that can be registered in three categories: add,
9595
remove, and open.
9696

9797
Each hook may be either a single command or Lua function, or a list of
98-
commands and Lua functions. See |workspaces-examples| for reference.
98+
commands and Lua functions. See |workspaces-examples| for reference. If the
99+
hook is a Lua function, it will be passed the workspace name and workspace
100+
path as parameters.
99101

100102
add ~
101103
run hooks after adding a workspace

lua/workspaces/init.lua

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ local direq = function(a, b)
7474
return a == b
7575
end
7676

77-
local run_hook = function(hook)
77+
local run_hook = function(hook, name, path)
7878
if type(hook) == "function" then
79-
hook()
79+
hook(name, path)
8080
elseif type(hook) == "string" then
8181
vim.cmd(hook)
8282
else
@@ -85,15 +85,15 @@ local run_hook = function(hook)
8585
end
8686

8787
-- given a list of hooks, execute each in the order given
88-
local run_hooks = function(hooks)
88+
local run_hooks = function(hooks, name, path)
8989
if not hooks then return end
9090

9191
if type(hooks) == "table" then
9292
for _, hook in ipairs(hooks) do
93-
run_hook(hook)
93+
run_hook(hook, name, path)
9494
end
9595
else
96-
run_hook(hooks)
96+
run_hook(hooks, name, path)
9797
end
9898
end
9999

@@ -148,7 +148,7 @@ M.add = function(path, name)
148148
})
149149

150150
store_workspaces(workspaces)
151-
run_hooks(config.hooks.add)
151+
run_hooks(config.hooks.add, name, path)
152152
end
153153

154154
-- TODO: make this the default api in v1.0
@@ -195,7 +195,8 @@ M.remove = function(name)
195195
local workspaces = load_workspaces()
196196
table.remove(workspaces, i)
197197
store_workspaces(workspaces)
198-
run_hooks(config.hooks.remove)
198+
199+
run_hooks(config.hooks.remove, workspace.name, workspace.path)
199200
end
200201

201202
---returns the list of all workspaces
@@ -233,7 +234,7 @@ M.open = function(name)
233234
end
234235

235236
-- change directory
236-
run_hooks(config.hooks.open_pre)
237+
run_hooks(config.hooks.open_pre, workspace.name, workspace.path)
237238

238239
if config.global_cd then
239240
vim.api.nvim_set_current_dir(workspace.path)
@@ -242,7 +243,7 @@ M.open = function(name)
242243
end
243244

244245
current_workspace = workspace.name
245-
run_hooks(config.hooks.open)
246+
run_hooks(config.hooks.open, workspace.name, workspace.path)
246247
end
247248

248249
---returns the name of the current workspace

0 commit comments

Comments
 (0)