Skip to content

Commit

Permalink
feat: pass workspace name and path to hooks
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
natecraddock committed Feb 10, 2022
1 parent dfdb319 commit c0d5aae
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 16 deletions.
18 changes: 12 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,37 @@

# master

* **feat** add `sort` config option
* **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
arguments to the function. This allows hooks to respond differently depending
on the name and path of the workspace if desired.

* **feat**: add `sort` config option

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

* **feat** add `global_cd` config option
* **feat**: add `global_cd` config option

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

* **deprecated** `:Workspaces [add|remove|open|list]` commands
* **deprecated**: `:Workspaces [add|remove|open|list]` commands

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

* **feat** improved command completion
* **feat**: improved command completion

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

* **feat** add `workspaces.name()` api function to query the current workpace
* **feat**: add `workspaces.name()` api function to query the current workspace
name.

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

# v0.1 Initial Release
4 changes: 3 additions & 1 deletion doc/workspaces.txt
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ There are 4 types of hooks that can be registered in three categories: add,
remove, and open.

Each hook may be either a single command or Lua function, or a list of
commands and Lua functions. See |workspaces-examples| for reference.
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.

add ~
run hooks after adding a workspace
Expand Down
19 changes: 10 additions & 9 deletions lua/workspaces/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,9 @@ local direq = function(a, b)
return a == b
end

local run_hook = function(hook)
local run_hook = function(hook, name, path)
if type(hook) == "function" then
hook()
hook(name, path)
elseif type(hook) == "string" then
vim.cmd(hook)
else
Expand All @@ -85,15 +85,15 @@ local run_hook = function(hook)
end

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

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

Expand Down Expand Up @@ -148,7 +148,7 @@ M.add = function(path, name)
})

store_workspaces(workspaces)
run_hooks(config.hooks.add)
run_hooks(config.hooks.add, name, path)
end

-- TODO: make this the default api in v1.0
Expand Down Expand Up @@ -195,7 +195,8 @@ M.remove = function(name)
local workspaces = load_workspaces()
table.remove(workspaces, i)
store_workspaces(workspaces)
run_hooks(config.hooks.remove)

run_hooks(config.hooks.remove, workspace.name, workspace.path)
end

---returns the list of all workspaces
Expand Down Expand Up @@ -233,7 +234,7 @@ M.open = function(name)
end

-- change directory
run_hooks(config.hooks.open_pre)
run_hooks(config.hooks.open_pre, workspace.name, workspace.path)

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

current_workspace = workspace.name
run_hooks(config.hooks.open)
run_hooks(config.hooks.open, workspace.name, workspace.path)
end

---returns the name of the current workspace
Expand Down

0 comments on commit c0d5aae

Please sign in to comment.