Skip to content

Commit

Permalink
lib/neovim-plugin: allow configLocation to be wrapped using mkOrder
Browse files Browse the repository at this point in the history
  • Loading branch information
MattSturgeon committed Dec 14, 2024
1 parent c181014 commit 6a192a8
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ A template plugin can be found in (plugins/TEMPLATE.nix)[https://github.com/nix-
| **url** | The URL of the plugin's repository. | Yes | `package` parameter's `meta.homepage` |
| **callSetup** | Indicating whether to call the setup function. Useful when `setup` function needs customizations. | No | `true` |
| **colorscheme** | The name of the colorscheme. | No | `name` parameter |
| **configLocation** | The location for the Lua configuration. | No | `"extraConfigLuaPre"` if `isColorscheme` then `extraConfigLuaPre`, otherwise `"extraConfigLua"` |
| **configLocation** | The option location where the lua configuration should be installed. Nested option locations can be represented as a list. The location can also be wrapped using `lib.mkBefore`, `lib.mkAfter`, or `lib.mkOrder`. | No | `"extraConfigLuaPre"` if `isColorscheme` then `extraConfigLuaPre`, otherwise `"extraConfigLua"` |
| **deprecateExtraOptions** | Indicating whether to deprecate the `extraOptions` attribute. Mainly used for old plugins. | No | `false` |
| **description** | A brief description of the plugin. Can also be used for non-normative documentation, warnings, tips and tricks. | No | `null` |
| **extraConfig** | Additional configuration for the plugin. Either an attrset, a function accepting `cfg`, or a function accepting `cfg` and `opts`. | No | `{}` |
Expand Down
9 changes: 9 additions & 0 deletions lib/modules.nix
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,15 @@ in
(maybeApply opts)
(lib.mkIf enabled)
];

mkConfigAt =
loc: def:
let
isOrder = loc._type or null == "order";
withOrder = if isOrder then lib.modules.mkOrder loc.priority else lib.id;
loc' = lib.toList (if isOrder then loc.content else loc);
in
lib.setAttrByPath loc' (withOrder def);
}
// lib.mapAttrs (
name: msg:
Expand Down
6 changes: 3 additions & 3 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@
})
'';

setLuaConfig = lib.setAttrByPath (lib.toList configLocation);
luaConfigAtLocation = lib.nixvim.modules.mkConfigAt configLocation cfg.luaConfig.content;
in
{
meta = {
Expand Down Expand Up @@ -162,8 +162,8 @@
# Add the plugin setup code `require('foo').setup(...)` to the lua configuration
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })

# Write the lua configuration `luaConfig.content` to the config file when lazy loading is not enabled
(lib.mkIf (!cfg.lazyLoad.enable) (setLuaConfig cfg.luaConfig.content))
# When NOT lazy loading, write `luaConfig.content` to `configLocation`
(lib.mkIf (!cfg.lazyLoad.enable) luaConfigAtLocation)

# When lazy loading is enabled for this plugin, route its configuration to the enabled provider
(lib.mkIf cfg.lazyLoad.enable {
Expand Down
28 changes: 28 additions & 0 deletions tests/lib-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,34 @@ let
EOFFF'';
};
};

testMkLuaConfig = {
expr = lib.mapAttrs (_: loc: helpers.modules.mkConfigAt loc "Hello!") {
"simple string" = "foo";
"simple list" = [
"foo"
"bar"
];
"mkBefore string" = lib.mkBefore "foo";
"mkBefore list" = lib.mkBefore [
"foo"
"bar"
];
"mkAfter string" = lib.mkAfter "foo";
"mkAfter list" = lib.mkAfter [
"foo"
"bar"
];
};
expected = {
"simple string".foo = "Hello!";
"simple list".foo.bar = "Hello!";
"mkBefore string".foo = lib.mkBefore "Hello!";
"mkBefore list".foo.bar = lib.mkBefore "Hello!";
"mkAfter string".foo = lib.mkAfter "Hello!";
"mkAfter list".foo.bar = lib.mkAfter "Hello!";
};
};
};
in
if results == [ ] then
Expand Down

0 comments on commit 6a192a8

Please sign in to comment.