Skip to content

Commit

Permalink
chore: drop support for vim.g configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
mrcjkb committed Jun 17, 2024
1 parent 30ad453 commit 6e13853
Show file tree
Hide file tree
Showing 9 changed files with 253 additions and 128 deletions.
12 changes: 12 additions & 0 deletions .busted
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
return {
_all = {
coverage = false,
lpath = "lua/?.lua;lua/?/init.lua",
},
default = {
verbose = true
},
tests = {
verbose = true
},
}
4 changes: 4 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -69,3 +69,7 @@ insert_final_newline = unset
trim_trailing_whitespace = unset
indent_style = unset
indent_size = unset

[spec/*.lua]
indent_size = unset
indent_style = unset
74 changes: 40 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,11 @@ Where `rocks.lazy.KeysSpec` is a table with the following fields:

> [!NOTE]
>
> If unspecified, the default `mode` is `n`.
> - If unspecified, the default `mode` is `n`.
> - The `lhs` and `rhs` fields differ
> from [the `lz.n.PluginSpec`](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#plugin-spec)[^2].
[^2]: This is because toml tables are stricter than Lua tables.

Examples:

Expand Down Expand Up @@ -216,47 +220,49 @@ colorscheme = [
### 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`.
you can add a `import` option to your `rocks.toml`:
Examples:
```toml
[rocks_lazy]
import = "lazy_specs/"
```
```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",
},
},
}
This is a subdirectory (relative to `nvim/lua`)
to search for plugin specs.
In this example, you can add a `lua/lazy_specs/` directory
to your `nvim` config, with a lua script for each plugin.

```sh
── nvim
├── lua
│ └── lazy_specs # Your plugin specs go here.
│ └── init.lua # Optional top-level module returning a list of specs
│ └── neorg.lua # Single spec
│ └── sweetie.lua
├── init.lua
```

Or

```lua
vim.g.rocks_nvim = {
-- ...
lz_spec = "lazy_specs", -- Spec modules in nvim/lua/lazy_specs/<spec>.lua
}

```sh
── nvim
├── lua
│ └── lazy_specs.lua # Optional top-level module returning a list of specs
├── init.lua
```

- See [the `lz.n` documentation](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#structuring-your-plugins).
- The Lua plugins specs must be configured according to
the [`lz.n.PluginSpec`](https://github.com/nvim-neorocks/lz.n?tab=readme-ov-file#plugin-spec).

> [!IMPORTANT]
>
> If you use a module to import your plugin specs
> and you also use `rocks-config.nvim`,
> the `rocks-lazy` `import` module name
> **must not clash** with the `rocks-config` `plugins_dir`.
> [!TIP]
>
> You can use both `rocks.toml` entries and a Lua config to configure
Expand Down
18 changes: 12 additions & 6 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,10 @@
type-check-nightly = pre-commit-hooks.lib.${system}.run {
src = self;
hooks = {
lua-ls.enable = true;
};
settings = {
lua-ls.config = luarc;
lua-ls = {
enable = true;
settings.configuration = luarc;
};
};
};

Expand All @@ -92,19 +92,20 @@
};
};

devShell = pkgs.mkShell {
devShell = pkgs.nvim-nightly-tests.overrideAttrs (oa: {
name = "rocks-lazy.nvim devShell";
shellHook = ''
${pre-commit-check.shellHook}
ln -fs ${pkgs.luarc-to-json luarc} .luarc.json
'';
buildInputs =
self.checks.${system}.pre-commit-check.enabledPackages
++ oa.buildInputs
++ (with pkgs; [
busted-nightly
lua-language-server
]);
};
});
in {
packages = rec {
default = neovim-with-rocks;
Expand All @@ -122,6 +123,11 @@
pre-commit-check
type-check-nightly
;
inherit
(pkgs)
nvim-stable-tests
nvim-nightly-tests
;
};
};
flake = {
Expand Down
89 changes: 89 additions & 0 deletions lua/rocks-lazy/internal.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
---@mod rocks-lazy.internal lazy-loading module for rocks.nvim

local rocks_lazy = {}

function rocks_lazy.load()
local api = require("rocks.api")
local lz_n = require("lz.n")

local user_rocks = api.get_user_rocks()

local has_rocks_config, rocks_config = pcall(require, "rocks-config")
---@type fun(name: string) | nil
local config_hook = has_rocks_config and type(rocks_config.configure) == "function" and rocks_config.configure
or nil

--- HACK: For some reason, if a RockSpec contains a list
--- (e.g. colorscheme = [ .. ]) then vim.deepcopy errors
--- if we don't fix it. This is possibly a toml_edit.lua bug!
---
---@generic T
---@param value T | T[]
---@return T | T[]
local function clone_toml_list(value)
if vim.islist(value) then
---@cast value string[]
return vim.list_extend({}, value)
end
return value
end

---@param keys string | string[] | rocks.lazy.KeysSpec[] | nil
---@return string | string[] | lz.n.KeysSpec[] | nil
local function to_lz_n_keys_spec(keys)
if not keys or type(keys) == "string" then
return keys
end
return vim.iter(keys)
:map(function(value)
if type(value) ~= "table" then
return value
end
local result = vim.deepcopy(value)
result[1] = value.lhs
result.lhs = nil
result[2] = value.rhs
result.rhs = nil
return result
end)
:totable()
end

---@type lz.n.PluginSpec[]
local specs = vim.iter(user_rocks)
:filter(function(_, rock)
---@cast rock rocks.lazy.RockSpec
return rock.opt == true
or rock.event ~= nil
or rock.cmd ~= nil
or rock.keys ~= nil
or rock.ft ~= nil
or rock.colorscheme ~= nil
end)
:map(function(_, rock)
---@cast rock rocks.lazy.RockSpec
---@type lz.n.PluginSpec
return {
rock.name,
before = config_hook,
lazy = rock.opt,
event = clone_toml_list(rock.event),
cmd = clone_toml_list(rock.cmd),
keys = to_lz_n_keys_spec(clone_toml_list(rock.keys)),
ft = clone_toml_list(rock.ft),
colorscheme = clone_toml_list(rock.colorscheme),
}
end)
:totable()

local rocks_toml = api.get_rocks_toml()
local config = rocks_toml.rocks_lazy or {}
local lz_specs = config.import
if type(lz_specs) == "string" then
table.insert(specs, { import = lz_specs })
end

lz_n.load(specs)
end

return rocks_lazy
3 changes: 0 additions & 3 deletions lua/rocks-lazy/meta.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,3 @@ 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
41 changes: 41 additions & 0 deletions nix/overlay.nix
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,41 @@
+ " "
+ ''--set NVIM_APPNAME "nvimrocks"'';
});

nvim-nightly = final.neovim-nightly;

mkNeorocksTest = {
name,
nvim ? final.neovim-unwrapped,
extraPkgs ? [],
}: let
nvim-wrapped = final.pkgs.wrapNeovim nvim {
configure = {
packages.myVimPackage = {
start = [];
};
};
};
in
final.pkgs.neorocksTest {
inherit name;
pname = "rocks-lazy.nvim";
src = self;
neovim = nvim-wrapped;
luaPackages = ps: [
luajitPackages.lz-n
luajitPackages.rocks-nvim
];

preCheck = ''
export HOME=$(realpath .)
'';

buildPhase = ''
mkdir -p $out
cp -r spec $out
'';
};
in {
inherit
lua5_1
Expand All @@ -129,4 +164,10 @@ in {
};

rocks-lazy-nvim = lua51Packages.rocks-lazy-nvim;

nvim-stable-tests = mkNeorocksTest {name = "neovim-stable-tests";};
nvim-nightly-tests = mkNeorocksTest {
name = "neovim-nightly-tests";
nvim = nvim-nightly;
};
}
Loading

0 comments on commit 6e13853

Please sign in to comment.