From c6e0be37f11f0847cc8f1716d5667668cbbcc792 Mon Sep 17 00:00:00 2001 From: Marc Jakobi Date: Mon, 17 Jun 2024 19:54:10 +0200 Subject: [PATCH] feat: support both toml and lua configuration --- README.md | 54 ++++++++++++++++++++++++++++++++-- lua/rocks-lazy/meta.lua | 3 ++ nix/overlay.nix | 10 +++---- plugin/rocks-lazy.lua | 12 ++++++++ rocks-lazy.nvim-scm-1.rockspec | 2 +- 5 files changed, 73 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 59dd3e0..ac1de62 100644 --- a/README.md +++ b/README.md @@ -63,11 +63,11 @@ and you are good to go! ## :books: Usage +### Via `rocks.toml` + With this module installed, you can add the following fields to a `[plugins]` entry in your `rocks.toml`: -### Fields - #### `event` Lazy-load on an event (`:h autocmd-events`). @@ -206,6 +206,56 @@ colorscheme = [ > keys = [ { lhs = "t", rhs = "Telescope" } ] > ``` +### Lua configuration + +If you prefer using Lua for configuration, +you can use the `vim.g.rocks_nvim.lz_spec` field, which can be + +- A [`lz.n.PluginSpec`](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#plugin-spec). +- A Lua module name (`string`) that contains your plugin spec. + See [the `lz.n` documentation](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#structuring-your-plugins). + +> [!IMPORTANT] +> +> If you use a module name to import your plugin specs +> and you also use `rocks-config.nvim`, +> the `lz_spec` module name **must not clash** with the `rocks-config` `plugins_dir`. + +Examples: + +```lua +vim.g.rocks_nvim = { + -- ... + lz_spec = { + { + "crates.nvim", + -- lazy-load when opening a toml file + ft = "toml", + }, + { + "sweetie.nvim", + -- lazy-load when setting the `sweetie` colorscheme + colorscheme = "sweetie", + }, + }, +} +``` + +Or + +```lua +vim.g.rocks_nvim = { + -- ... + lz_spec = "lazy_specs", -- Spec modules in nvim/lua/lazy_specs/.lua +} +``` + +> [!TIP] +> +> You can use both `rocks.toml` entries and a Lua config to configure +> your plugin specs. +> `rocks-lazy.nvim` will merge the resulting specs. + ## :book: License `rocks-lazy.nvim` is licensed under [GPLv3](./LICENSE). diff --git a/lua/rocks-lazy/meta.lua b/lua/rocks-lazy/meta.lua index 023a177..076694c 100644 --- a/lua/rocks-lazy/meta.lua +++ b/lua/rocks-lazy/meta.lua @@ -30,3 +30,6 @@ error("Cannot require a meta module") ---@field nowait? boolean ---@field ft? string|string[] ---@field mode? string|string[] + +---@type lz.n.Spec +vim.g.rocks_nvim.lz_spec = vim.g.rocks_nvim.lz_spec diff --git a/nix/overlay.nix b/nix/overlay.nix index 29e3cf5..3001d6a 100644 --- a/nix/overlay.nix +++ b/nix/overlay.nix @@ -13,16 +13,16 @@ }: buildLuarocksPackage { pname = "lz.n"; - version = "1.2.3-1"; + version = "1.2.4-1"; knownRockspec = (fetchurl { - url = "mirror://luarocks/lz.n-1.2.3-1.rockspec"; - sha256 = "1h89pkj8j82wfzqvia54q70y1zpdwkc4j8kifl3xpmyyp4kgibkw"; + url = "mirror://luarocks/lz.n-1.2.4-1.rockspec"; + sha256 = "sha256-DR0wr7wczl0P1PEMDSc9w9gU30KstX/rWBjAcx97k7A="; }) .outPath; src = fetchzip { - url = "https://github.com/nvim-neorocks/lz.n/archive/v1.2.3.zip"; - sha256 = "1bms3ynha48mar2zfmyd3vlvxz7v3q1v5jxp1dhxwmyxa1dp2vhc"; + url = "https://github.com/nvim-neorocks/lz.n/archive/v1.2.4.zip"; + sha256 = "sha256-T4gRlf7GoiPhwvQnvomKLU4y21v1zFm3YcKvsdgkSds="; }; disabled = luaOlder "5.1"; }) {}; diff --git a/plugin/rocks-lazy.lua b/plugin/rocks-lazy.lua index 18f0535..a0ba5b9 100644 --- a/plugin/rocks-lazy.lua +++ b/plugin/rocks-lazy.lua @@ -76,4 +76,16 @@ local specs = vim.iter(user_rocks) end) :totable() +local lz_spec = vim.g.rocks_nvim and vim.g.rocks_nvim.lz_spec or {} +if type(lz_spec) == "string" then + table.insert(specs, { import = lz_spec }) +else + local is_single_plugin_spec = type(lz_spec[1]) == "string" + if is_single_plugin_spec then + table.insert(specs, lz_spec) + elseif vim.islist(lz_spec) then + vim.list_extend(specs, lz_spec) + end +end + lz_n.load(specs) diff --git a/rocks-lazy.nvim-scm-1.rockspec b/rocks-lazy.nvim-scm-1.rockspec index 7de2291..3d32449 100644 --- a/rocks-lazy.nvim-scm-1.rockspec +++ b/rocks-lazy.nvim-scm-1.rockspec @@ -6,7 +6,7 @@ version = _MODREV .. _SPECREV dependencies = { "lua >= 5.1", "rocks.nvim >= 1.19.0", - "lz.n >= 1.2.3", + "lz.n >= 1.2.4", } source = {