From c0d5aae430fdc3c021b414d0bdc0fe35b9b0d94f Mon Sep 17 00:00:00 2001 From: Nathan Craddock Date: Thu, 10 Feb 2022 07:02:37 -0700 Subject: [PATCH] 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. --- CHANGELOG.md | 18 ++++++++++++------ doc/workspaces.txt | 4 +++- lua/workspaces/init.lua | 19 ++++++++++--------- 3 files changed, 25 insertions(+), 16 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d08e5dd..091770c 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/doc/workspaces.txt b/doc/workspaces.txt index bb045cf..db25d2b 100644 --- a/doc/workspaces.txt +++ b/doc/workspaces.txt @@ -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 diff --git a/lua/workspaces/init.lua b/lua/workspaces/init.lua index bbbe2f6..7233f87 100644 --- a/lua/workspaces/init.lua +++ b/lua/workspaces/init.lua @@ -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 @@ -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 @@ -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 @@ -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 @@ -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) @@ -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