From 1aaa18a7ff58e2d992e51120a6318f38f649b7c4 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Sat, 8 Feb 2025 22:26:18 +0100 Subject: [PATCH] feat: expose default `before` hook --- README.md | 7 ++++- lua/rocks-lazy/init.lua | 12 ++++++++ lua/rocks-lazy/internal.lua | 57 ++++++++++++++++++------------------- 3 files changed, 46 insertions(+), 30 deletions(-) create mode 100644 lua/rocks-lazy/init.lua diff --git a/README.md b/README.md index 3c392ee..9d56a92 100644 --- a/README.md +++ b/README.md @@ -244,7 +244,6 @@ to your `nvim` config, with a lua script for each plugin. Or - ```sh ── nvim ├── lua @@ -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). diff --git a/lua/rocks-lazy/init.lua b/lua/rocks-lazy/init.lua new file mode 100644 index 0000000..49adbcb --- /dev/null +++ b/lua/rocks-lazy/init.lua @@ -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 diff --git a/lua/rocks-lazy/internal.lua b/lua/rocks-lazy/internal.lua index 017fd6d..fe21629 100644 --- a/lua/rocks-lazy/internal.lua +++ b/lua/rocks-lazy/internal.lua @@ -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") @@ -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 @@ -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),