Skip to content

Commit

Permalink
feat: expose default before hook (#20)
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb authored Feb 8, 2025
1 parent dc6f6c0 commit 7eaf5ec
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 30 deletions.
7 changes: 6 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,6 @@ to your `nvim` config, with a lua script for each plugin.

Or


```sh
── nvim
├── lua
Expand Down Expand Up @@ -278,6 +277,12 @@ it will not load configs for any `opt` plugins.
`rocks-lazy` will use the `rocks-config` API to load them in the
`lz.n.PluginSpec.before` hooks.

> [!TIP]
>
> If you use Lua to configure lazy-loading, you can invoke the default
> [`before` hook](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#plugin-spec)
> by calling `require("rocks-lazy").default_before_hook(plugin)`.
## :book: License

`rocks-lazy.nvim` is licensed under [GPLv3](./LICENSE).
Expand Down
12 changes: 12 additions & 0 deletions lua/rocks-lazy/init.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---@mod rocks-lazy lazy-loading module for rocks.nvim
local rocks_lazy = {}

---The default `before` hook.
---If rocks-config.nvim is installed, this invokes
---the |rocks-config.configure| function.
---@param plugin lz.n.Plugin
rocks_lazy.default_before_hook = function(plugin)
require("rocks-lazy.internal").config_hook(plugin)
end

return rocks_lazy
57 changes: 28 additions & 29 deletions lua/rocks-lazy/internal.lua
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
---@mod rocks-lazy.internal lazy-loading module for rocks.nvim
---@mod rocks-lazy.internal lazy-loading module for rocks.nvim (internal)

local rocks_lazy = {}

rocks_lazy.config_hook = function(_) end

function rocks_lazy.load()
local api = require("rocks.api")
local log = require("rocks.log")
Expand All @@ -11,34 +13,31 @@ function rocks_lazy.load()

local has_rocks_config, rocks_config = pcall(require, "rocks-config")

local config_hook = has_rocks_config
and type(rocks_config.configure) == "function"
---@param plugin lz.n.Plugin
and function(plugin)
local rock_spec = user_rocks[plugin.name]
if rock_spec then
pcall(vim.cmd.packadd, { plugin.name, bang = true })
rocks_config.configure(
rock_spec,
---@param items? rock_name[]
function(items)
vim
.iter(items or {})
---@param item rock_name
:each(function(item)
pcall(vim.cmd.packadd, { item, bang = true })
end)
end
)
else
log.warn(
("rocks-lazy: skipping rocks-config hook because %s not found in user rocks."):format(
plugin.name
)
)
end
if has_rocks_config and type(rocks_config.configure) == "function" then
---@param plugin lz.n.Plugin
rocks_lazy.config_hook = function(plugin)
local rock_spec = user_rocks[plugin.name]
if rock_spec then
pcall(vim.cmd.packadd, { plugin.name, bang = true })
rocks_config.configure(
rock_spec,
---@param items? rock_name[]
function(items)
vim
.iter(items or {})
---@param item rock_name
:each(function(item)
pcall(vim.cmd.packadd, { item, bang = true })
end)
end
)
else
log.warn(
("rocks-lazy: skipping rocks-config hook because %s not found in user rocks."):format(plugin.name)
)
end
or function(_) end
end
end

--- HACK: For some reason, if a RockSpec contains a list
--- (e.g. colorscheme = [ .. ]) then vim.deepcopy errors
Expand Down Expand Up @@ -92,7 +91,7 @@ function rocks_lazy.load()
---@type lz.n.PluginSpec
return {
rock.name,
before = config_hook,
before = rocks_lazy.config_hook,
lazy = rock.opt,
event = clone_toml_list(rock.event),
cmd = clone_toml_list(rock.cmd),
Expand Down

0 comments on commit 7eaf5ec

Please sign in to comment.