Skip to content

Commit

Permalink
lib/helpers: refactor mkPlugin helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage committed Jun 30, 2024
1 parent 1391a64 commit e136967
Show file tree
Hide file tree
Showing 7 changed files with 318 additions and 244 deletions.
19 changes: 10 additions & 9 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -10,25 +10,26 @@ let
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
in
rec {
maintainers = import ./maintainers.nix;
lua = import ./to-lua.nix { inherit lib; };
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
neovim-plugin = import ./neovim-plugin.nix {
toLuaObject = lua.toLua;
nixvimPlugins = import ./plugins {
inherit
lib
nixvimOptions
nixvimUtils
toLuaObject
;
};
vim-plugin = import ./vim-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
inherit nixvimTypes;
toLuaObject = lua.toLua;
in
rec {
maintainers = import ./maintainers.nix;
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
plugins = import ./plugin { };
inherit nixvimTypes lua toLuaObject;
}
// nixvimUtils
// nixvimOptions
// nixvimBuilders
// nixvimDeprecation
// nixvimPlugins
121 changes: 0 additions & 121 deletions lib/neovim-plugin.nix

This file was deleted.

36 changes: 36 additions & 0 deletions lib/plugins/default.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
lib,
nixvimOptions,
nixvimUtils,
toLuaObject,
...
}:
let
mkPlugin = import ./mk-plugin.nix { inherit lib nixvimOptions nixvimUtils; };
in
rec {
plugins = {
inherit mkPlugin;
inherit (neovim-plugin) mkNeovimPlugin;
inherit (vim-plugin) mkVimPlugin;
};

neovim-plugin = import ./neovim-plugin.nix {
inherit
lib
nixvimOptions
nixvimUtils
toLuaObject
mkPlugin
;
};

vim-plugin = import ./vim-plugin.nix {
inherit
lib
nixvimOptions
nixvimUtils
mkPlugin
;
};
}
90 changes: 90 additions & 0 deletions lib/plugins/mk-plugin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
lib,
nixvimOptions,
nixvimUtils,
}:
with lib;
config:
{
name,
maintainers,
url ? defaultPackage.meta.homepage,
imports ? [ ],
description ? null,
# deprecations
optionsRenamedToSettings ? [ ],
# colorscheme
isColorscheme ? false,
colorscheme ? name,
# options
originalName ? name,
defaultPackage ? null,
extraOptions ? { },
# config
extraConfig ? cfg: { },
extraPlugins ? [ ],
extraPackages ? [ ],
}:
let
namespace = if isColorscheme then "colorschemes" else "plugins";

optionsRenamedToSettingsWarnings =
let
basePluginPath = [
namespace
name
];
settingsPath = basePluginPath ++ [ "settings" ];
in
map (
option:
let
optionPath = if isString option then [ option ] else option; # option is already a path (i.e. a list)

optionPathSnakeCase = map nixvimUtils.toSnakeCase optionPath;
in
mkRenamedOptionModule (
[
namespace
name
]
++ optionPath
) (settingsPath ++ optionPathSnakeCase)
) optionsRenamedToSettings;
in
{
meta = {
inherit maintainers;
nixvimInfo = {
inherit description url;
path = [
namespace
name
];
};
};

imports = optionsRenamedToSettingsWarnings ++ imports;

options.${namespace}.${name} =
{
enable = mkEnableOption originalName;
}
// (optionalAttrs (defaultPackage != null) {
package = nixvimOptions.mkPluginPackageOption originalName defaultPackage;
})
// extraOptions;

config =
let
cfg = config.${namespace}.${name};
in
mkIf cfg.enable (mkMerge [
{
extraPlugins = extraPlugins ++ optional (defaultPackage != null) cfg.package;
inherit extraPackages;
}
(extraConfig cfg)
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
]);
}
96 changes: 96 additions & 0 deletions lib/plugins/neovim-plugin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
{
lib,
nixvimOptions,
toLuaObject,
nixvimUtils,
mkPlugin,
}:
with lib;
{
# TODO: DEPRECATED: use the `settings` option instead
extraOptionsOptions = {
extraOptions = mkOption {
default = { };
type = with types; attrsOf anything;
description = ''
These attributes will be added to the table parameter for the setup function.
Typically, it can override NixVim's default settings.
'';
};
};

mkNeovimPlugin =
config:
{
name,
defaultPackage, # make this parameter mandatory for neovim plugins
# deprecations
deprecateExtraOptions ? false,
# colorscheme
isColorscheme ? false,
# options
settingsOptions ? { },
settingsExample ? null,
settingsDescription ? "Options provided to the `require('${luaName}')${setup}` function.",
hasSettings ? true,
extraOptions ? { },
# config
setup ? ".setup",
luaName ? name,
callSetup ? true,
...
}@args:
mkPlugin config (
(removeAttrs args [
"callSetup"
"deprecateExtraOptions"
"extraConfig"
"extraOptions"
"hasSettings"
"imports"
"luaName"
"settingsExample"
"settingsOptions"
])
// {
imports =
let
basePluginPath = [
(if isColorscheme then "colorschemes" else "plugins")
name
];
settingsPath = basePluginPath ++ [ "settings" ];
in
(args.imports or [ ])
++ (optional deprecateExtraOptions (
mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath
));

extraOptions =
(optionalAttrs hasSettings {
settings = nixvimOptions.mkSettingsOption {
description = settingsDescription;
options = settingsOptions;
example = settingsExample;
};
})
// extraOptions;

extraConfig =
let
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
in
cfg:
mkMerge (
[
{
${extraConfigNamespace} = optionalString callSetup ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';
}
]
++ (optional (args ? extraConfig) (args.extraConfig cfg))
);
}
);
}
Loading

0 comments on commit e136967

Please sign in to comment.