Skip to content

Commit e136967

Browse files
committed
lib/helpers: refactor mkPlugin helpers
1 parent 1391a64 commit e136967

File tree

7 files changed

+318
-244
lines changed

7 files changed

+318
-244
lines changed

lib/helpers.nix

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,25 +10,26 @@ let
1010
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
1111
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
1212
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
13-
in
14-
rec {
15-
maintainers = import ./maintainers.nix;
1613
lua = import ./to-lua.nix { inherit lib; };
17-
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
18-
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
19-
neovim-plugin = import ./neovim-plugin.nix {
14+
toLuaObject = lua.toLua;
15+
nixvimPlugins = import ./plugins {
2016
inherit
2117
lib
2218
nixvimOptions
2319
nixvimUtils
2420
toLuaObject
2521
;
2622
};
27-
vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
28-
inherit nixvimTypes;
29-
toLuaObject = lua.toLua;
23+
in
24+
rec {
25+
maintainers = import ./maintainers.nix;
26+
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
27+
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
28+
plugins = import ./plugin { };
29+
inherit nixvimTypes lua toLuaObject;
3030
}
3131
// nixvimUtils
3232
// nixvimOptions
3333
// nixvimBuilders
3434
// nixvimDeprecation
35+
// nixvimPlugins

lib/neovim-plugin.nix

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

lib/plugins/default.nix

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

lib/plugins/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/plugins/neovim-plugin.nix

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

0 commit comments

Comments
 (0)