nix-community.nixvim/lib/vim-plugin.nix

110 lines
3.3 KiB
Nix
Raw Normal View History

2024-07-28 22:30:11 +01:00
{ lib, helpers }:
2024-05-05 19:39:35 +02:00
with lib;
{
mkVimPlugin =
{
name,
url ? if defaultPackage != null then defaultPackage.meta.homepage else null,
2024-05-05 19:39:35 +02:00
maintainers,
imports ? [ ],
description ? null,
# deprecations
deprecateExtraConfig ? false,
optionsRenamedToSettings ? [ ],
# colorscheme
isColorscheme ? false,
colorscheme ? name,
# options
originalName ? name,
defaultPackage ? null,
settingsOptions ? { },
settingsExample ? null,
globalPrefix ? "",
extraOptions ? { },
# config
extraConfig ? cfg: { },
extraPlugins ? [ ],
extraPackages ? [ ],
}:
let
namespace = if isColorscheme then "colorschemes" else "plugins";
2024-05-05 19:39:35 +02:00
# does this evaluate package?
packageOption =
if defaultPackage == null then
{ }
else
2024-07-28 22:30:11 +01:00
{ package = helpers.mkPluginPackageOption name defaultPackage; };
2024-05-05 19:39:35 +02:00
createSettingsOption = (isString globalPrefix) && (globalPrefix != "");
2024-05-05 19:39:35 +02:00
settingsOption = optionalAttrs createSettingsOption {
2024-07-28 22:30:11 +01:00
settings = helpers.mkSettingsOption {
options = settingsOptions;
example = settingsExample;
description = ''
The configuration options for **${name}** without the `${globalPrefix}` prefix.
For example, the following settings are equivialent to these `:setglobal` commands:
- `foo_bar = 1` -> `:setglobal ${globalPrefix}foo_bar=1`
- `hello = "world"` -> `:setglobal ${globalPrefix}hello="world"`
- `some_toggle = true` -> `:setglobal ${globalPrefix}some_toggle`
- `other_toggle = false` -> `:setglobal no${globalPrefix}other_toggle`
'';
};
};
modules = [
(
{ config, ... }:
let
cfg = config.${namespace}.${name};
in
{
config = mkIf cfg.enable (mkMerge [
{
inherit extraPackages;
globals = mapAttrs' (n: nameValuePair (globalPrefix + n)) (cfg.settings or { });
# does this evaluate package? it would not be desired to evaluate package if we use another package.
extraPlugins = extraPlugins ++ optional (defaultPackage != null) cfg.package;
}
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
]);
}
)
];
2024-05-05 19:39:35 +02:00
in
{
meta = {
inherit maintainers;
nixvimInfo = {
inherit description url;
path = [
namespace
name
];
2024-05-05 19:39:35 +02:00
};
};
2024-05-05 19:39:35 +02:00
options.${namespace}.${name} = {
enable = mkEnableOption originalName;
2024-05-05 19:39:35 +02:00
} // settingsOption // packageOption // extraOptions;
2024-05-05 19:39:35 +02:00
imports =
let
basePluginPath = [
namespace
name
];
settingsPath = basePluginPath ++ [ "settings" ];
in
imports
++ (optional (deprecateExtraConfig && createSettingsOption) (
mkRenamedOptionModule (basePluginPath ++ [ "extraConfig" ]) settingsPath
))
++ (nixvim.mkSettingsRenamedOptionModules basePluginPath settingsPath optionsRenamedToSettings)
++ modules;
2024-05-05 19:39:35 +02:00
};
}