Skip to content

Commit 1a5c8fa

Browse files
committed
lib/helpers: refactor mkPlugin helpers
1 parent b822078 commit 1a5c8fa

File tree

7 files changed

+315
-250
lines changed

7 files changed

+315
-250
lines changed

lib/helpers.nix

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,23 +10,23 @@ let
1010
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
1111
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
1212
inherit (import ./to-lua.nix { inherit lib; }) toLuaObject;
13-
in
14-
{
15-
maintainers = import ./maintainers.nix;
16-
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
17-
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
18-
neovim-plugin = import ./neovim-plugin.nix {
13+
nixvimPlugins = import ./plugin {
1914
inherit
2015
lib
2116
nixvimOptions
2217
nixvimUtils
2318
toLuaObject
2419
;
2520
};
26-
vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
21+
in
22+
{
23+
maintainers = import ./maintainers.nix;
24+
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
25+
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
2726
inherit nixvimTypes;
2827
inherit toLuaObject;
2928
}
3029
// nixvimUtils
3130
// nixvimOptions
3231
// nixvimBuilders
32+
// nixvimPlugins

lib/neovim-plugin.nix

Lines changed: 0 additions & 128 deletions
This file was deleted.

lib/plugin/default.nix

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
lib,
3+
nixvimOptions,
4+
nixvimUtils,
5+
toLuaObject,
6+
...
7+
}:
8+
rec {
9+
mkPlugin = import ./mk-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
10+
11+
neovim-plugin = import ./neovim-plugin.nix {
12+
inherit
13+
lib
14+
nixvimOptions
15+
nixvimUtils
16+
toLuaObject
17+
mkPlugin
18+
;
19+
};
20+
21+
vim-plugin = import ./vim-plugin.nix {
22+
inherit
23+
lib
24+
nixvimOptions
25+
nixvimUtils
26+
mkPlugin
27+
;
28+
};
29+
}

lib/plugin/mk-plugin.nix

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
{
2+
lib,
3+
nixvimOptions,
4+
nixvimUtils,
5+
}:
6+
with lib;
7+
config:
8+
{
9+
name,
10+
maintainers,
11+
url ? defaultPackage.meta.homepage,
12+
imports ? [ ],
13+
description ? null,
14+
# deprecations
15+
optionsRenamedToSettings ? [ ],
16+
# colorscheme
17+
isColorscheme ? false,
18+
colorscheme ? name,
19+
# options
20+
originalName ? name,
21+
defaultPackage ? null,
22+
extraOptions ? { },
23+
# config
24+
extraConfig ? cfg: { },
25+
extraPlugins ? [ ],
26+
extraPackages ? [ ],
27+
}:
28+
let
29+
namespace = if isColorscheme then "colorschemes" else "plugins";
30+
31+
optionsRenamedToSettingsWarnings =
32+
let
33+
basePluginPath = [
34+
namespace
35+
name
36+
];
37+
settingsPath = basePluginPath ++ [ "settings" ];
38+
in
39+
map (
40+
option:
41+
let
42+
optionPath = if isString option then [ option ] else option; # option is already a path (i.e. a list)
43+
44+
optionPathSnakeCase = map nixvimUtils.toSnakeCase optionPath;
45+
in
46+
mkRenamedOptionModule (
47+
[
48+
namespace
49+
name
50+
]
51+
++ optionPath
52+
) (settingsPath ++ optionPathSnakeCase)
53+
) optionsRenamedToSettings;
54+
in
55+
{
56+
meta = {
57+
inherit maintainers;
58+
nixvimInfo = {
59+
inherit description url;
60+
path = [
61+
namespace
62+
name
63+
];
64+
};
65+
};
66+
67+
imports = optionsRenamedToSettingsWarnings ++ imports;
68+
69+
options.${namespace}.${name} =
70+
{
71+
enable = mkEnableOption originalName;
72+
}
73+
// (optionalAttrs (defaultPackage != null) {
74+
package = nixvimOptions.mkPluginPackageOption originalName defaultPackage;
75+
})
76+
// extraOptions;
77+
78+
config =
79+
let
80+
cfg = config.${namespace}.${name};
81+
in
82+
mkIf cfg.enable (mkMerge [
83+
{
84+
extraPlugins = extraPlugins ++ optional (defaultPackage != null) cfg.package;
85+
inherit extraPackages;
86+
}
87+
(extraConfig cfg)
88+
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
89+
]);
90+
}

lib/plugin/neovim-plugin.nix

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
{
2+
lib,
3+
nixvimOptions,
4+
toLuaObject,
5+
nixvimUtils,
6+
mkPlugin,
7+
}:
8+
with lib;
9+
rec {
10+
mkSettingsOption =
11+
{
12+
pluginName ? null,
13+
options ? { },
14+
description ?
15+
if pluginName != null then
16+
"Options provided to the `require('${pluginName}').setup` function."
17+
else
18+
throw "mkSettingsOption: Please provide either a `pluginName` or `description`.",
19+
example ? null,
20+
}:
21+
nixvimOptions.mkSettingsOption { inherit options description example; };
22+
23+
# TODO: DEPRECATED: use the `settings` option instead
24+
extraOptionsOptions = {
25+
extraOptions = mkOption {
26+
default = { };
27+
type = with types; attrsOf anything;
28+
description = ''
29+
These attributes will be added to the table parameter for the setup function.
30+
Typically, it can override NixVim's default settings.
31+
'';
32+
};
33+
};
34+
35+
mkNeovimPlugin =
36+
config:
37+
{
38+
name,
39+
defaultPackage, # make this parameter mandatory for neovim plugins
40+
# deprecations
41+
deprecateExtraOptions ? false,
42+
# colorscheme
43+
isColorscheme ? false,
44+
# options
45+
settingsOptions ? { },
46+
settingsExample ? null,
47+
# config
48+
luaName ? name,
49+
callSetup ? true,
50+
...
51+
}@args:
52+
mkPlugin config (
53+
(removeAttrs args [
54+
"callSetup"
55+
"deprecateExtraOptions"
56+
"extraConfig"
57+
"extraOptions"
58+
"imports"
59+
"luaName"
60+
"settingsExample"
61+
"settingsOptions"
62+
])
63+
// {
64+
imports =
65+
let
66+
basePluginPath = [
67+
(if isColorscheme then "colorschemes" else "plugins")
68+
name
69+
];
70+
settingsPath = basePluginPath ++ [ "settings" ];
71+
in
72+
(args.imports or [ ])
73+
++ (optional deprecateExtraOptions (
74+
mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath
75+
));
76+
77+
extraOptions = {
78+
settings = mkSettingsOption {
79+
pluginName = name;
80+
options = settingsOptions;
81+
example = settingsExample;
82+
};
83+
} // (args.extraOptions or { });
84+
85+
extraConfig =
86+
let
87+
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
88+
in
89+
cfg:
90+
mkMerge (
91+
[
92+
{
93+
${extraConfigNamespace} = optionalString callSetup ''
94+
require('${luaName}').setup(${toLuaObject cfg.settings})
95+
'';
96+
}
97+
]
98+
++ (optional (args ? extraConfig) (args.extraConfig cfg))
99+
);
100+
}
101+
);
102+
}

0 commit comments

Comments
 (0)