Skip to content

Commit a52b607

Browse files
committed
mkNeovimPlugin: refactor lua code generation logic
1 parent 4b5364a commit a52b607

File tree

3 files changed

+18
-14
lines changed

3 files changed

+18
-14
lines changed

CONTRIBUTING.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ A template plugin can be found in (plugins/TEMPLATE.nix)[https://github.com/nix-
6969
| **extraOptions** | Module options for the plugin, to be added _outside_ of the `settings` option. These should be Nixvim-specific options. | No | `{}` |
7070
| **extraPackages** | Extra packages to include. | No | `[]` |
7171
| **extraPlugins** | Extra plugins to include. | No | `[]` |
72-
| **hasConfigAttrs** | Indicating whether the plugin has configuration attributes. | No | `true` |
72+
| **hasLuaConfig** | Indicating whether the plugin generates lua configuration code (and thus should have a `luaConfig` option). | No | `true` |
7373
| **hasSettings** | Indicating whether the plugin has settings. A `settings` option will be created if true. | No | `true` |
7474
| **imports** | Additional modules to import. | No | `[]` |
7575
| **isColorscheme** | Indicating whether the plugin is a colorscheme. | No | `false` |

lib/neovim-plugin.nix

+16-11
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
colorscheme ? name,
2828
# luaConfig
2929
configLocation ? if isColorscheme then "extraConfigLuaPre" else "extraConfigLua",
30-
# For some plugins it may not make sense to have a configuration attribute, as they are
31-
# configured through some other mean, like global variables
32-
hasConfigAttrs ? true,
30+
# Some plugin are not supposed to generate lua configuration code.
31+
# For example, they might just be configured through some other mean, like global variables
32+
hasLuaConfig ? true,
3333
# options
3434
originalName ? name,
3535
# Can be a string, a list of strings, or a module option:
@@ -121,7 +121,7 @@
121121
example = settingsExample;
122122
};
123123
}
124-
// lib.optionalAttrs hasConfigAttrs {
124+
// lib.optionalAttrs hasLuaConfig {
125125
luaConfig = lib.mkOption {
126126
type = lib.types.pluginLuaConfig;
127127
default = { };
@@ -132,8 +132,8 @@
132132

133133
config =
134134
assert lib.assertMsg (
135-
callSetup -> configLocation != null
136-
) "When a plugin has no config attrs and has a setup function it must have a config location";
135+
callSetup -> hasLuaConfig
136+
) "This plugin is supposed to call the `setup()` function but has `hasLuaConfig` set to false";
137137
lib.mkIf cfg.enable (
138138
lib.mkMerge (
139139
[
@@ -143,21 +143,26 @@
143143
(cfg.packageDecorator cfg.package)
144144
];
145145
}
146+
146147
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
147148
colorscheme = lib.mkDefault colorscheme;
148149
})
150+
151+
# Apply any additional configuration added `extraConfig`
149152
(lib.optionalAttrs (args ? extraConfig) (
150153
lib.nixvim.modules.applyExtraConfig {
151154
inherit extraConfig cfg opts;
152155
}
153156
))
154157
]
155-
++ (lib.optionals (!hasConfigAttrs) [
156-
(lib.optionalAttrs callSetup (setLuaConfig setupCode))
157-
])
158-
++ (lib.optionals hasConfigAttrs [
158+
# Lua configuration code generation
159+
++ (lib.optionals hasLuaConfig [
160+
161+
# Add the plugin setup code `require('foo').setup(...)` to the lua configuration
159162
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
160-
(lib.optionalAttrs (configLocation != null) (setLuaConfig cfg.luaConfig.content))
163+
164+
# Write the lua configuration `luaConfig.content` to the config file
165+
(setLuaConfig cfg.luaConfig.content)
161166
])
162167
)
163168
);

plugins/by-name/rustaceanvim/default.nix

+1-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
4848
};
4949

5050
callSetup = false;
51-
hasConfigAttrs = false;
52-
configLocation = null;
51+
hasLuaConfig = false;
5352
extraConfig =
5453
cfg:
5554
mkMerge [

0 commit comments

Comments
 (0)