Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mkNeovimPlugin: refactor lua code generation logic #2623

Merged
merged 1 commit into from
Dec 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ A template plugin can be found in (plugins/TEMPLATE.nix)[https://github.com/nix-
| **extraOptions** | Module options for the plugin, to be added _outside_ of the `settings` option. These should be Nixvim-specific options. | No | `{}` |
| **extraPackages** | Extra packages to include. | No | `[]` |
| **extraPlugins** | Extra plugins to include. | No | `[]` |
| **hasConfigAttrs** | Indicating whether the plugin has configuration attributes. | No | `true` |
| **hasLuaConfig** | Indicating whether the plugin generates lua configuration code (and thus should have a `luaConfig` option). | No | `true` |
| **hasSettings** | Indicating whether the plugin has settings. A `settings` option will be created if true. | No | `true` |
| **imports** | Additional modules to import. | No | `[]` |
| **isColorscheme** | Indicating whether the plugin is a colorscheme. | No | `false` |
Expand Down
27 changes: 16 additions & 11 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
colorscheme ? name,
# luaConfig
configLocation ? if isColorscheme then "extraConfigLuaPre" else "extraConfigLua",
# For some plugins it may not make sense to have a configuration attribute, as they are
# configured through some other mean, like global variables
hasConfigAttrs ? true,
# Some plugin are not supposed to generate lua configuration code.
# For example, they might just be configured through some other mean, like global variables
hasLuaConfig ? true,
# options
originalName ? name,
# Can be a string, a list of strings, or a module option:
Expand Down Expand Up @@ -121,7 +121,7 @@
example = settingsExample;
};
}
// lib.optionalAttrs hasConfigAttrs {
// lib.optionalAttrs hasLuaConfig {
luaConfig = lib.mkOption {
type = lib.types.pluginLuaConfig;
default = { };
Expand All @@ -132,8 +132,8 @@

config =
assert lib.assertMsg (
callSetup -> configLocation != null
) "When a plugin has no config attrs and has a setup function it must have a config location";
callSetup -> hasLuaConfig
) "This plugin is supposed to call the `setup()` function but has `hasLuaConfig` set to false";
lib.mkIf cfg.enable (
lib.mkMerge (
[
Expand All @@ -143,21 +143,26 @@
(cfg.packageDecorator cfg.package)
];
}

(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme;
})

# Apply any additional configuration added to `extraConfig`
(lib.optionalAttrs (args ? extraConfig) (
lib.nixvim.modules.applyExtraConfig {
inherit extraConfig cfg opts;
}
))
]
++ (lib.optionals (!hasConfigAttrs) [
(lib.optionalAttrs callSetup (setLuaConfig setupCode))
])
++ (lib.optionals hasConfigAttrs [
# Lua configuration code generation
++ (lib.optionals hasLuaConfig [

# Add the plugin setup code `require('foo').setup(...)` to the lua configuration
(lib.optionalAttrs callSetup { ${namespace}.${name}.luaConfig.content = setupCode; })
(lib.optionalAttrs (configLocation != null) (setLuaConfig cfg.luaConfig.content))

# Write the lua configuration `luaConfig.content` to the config file
(setLuaConfig cfg.luaConfig.content)
Comment on lines +164 to +165
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Q: why is it ok to remove the null check? (configLocation != null)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it should never be null.
null is a forbidden value. Or at least, it is fine as long as hasLuaConfig is false.

])
)
);
Expand Down
3 changes: 1 addition & 2 deletions plugins/by-name/rustaceanvim/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ helpers.neovim-plugin.mkNeovimPlugin {
};

callSetup = false;
hasConfigAttrs = false;
configLocation = null;
hasLuaConfig = false;
extraConfig =
cfg:
mkMerge [
Expand Down
Loading