refactor: extract function to lib

This commit is contained in:
Ching Pei Yang 2024-04-20 14:59:46 +02:00
parent e710afd1ac
commit b38886d25d
2 changed files with 61 additions and 18 deletions

View file

@ -1,6 +1,9 @@
{lib}: let {lib}: let
inherit (lib.options) mkOption; inherit (lib.options) mkOption;
inherit (lib.types) bool; inherit (lib.types) bool;
inherit (lib.modules) mkRenamedOptionModule;
inherit (lib.attrsets) mapAttrsToList;
inherit (lib.lists) flatten;
in { in {
mkBool = value: description: mkBool = value: description:
mkOption { mkOption {
@ -8,4 +11,56 @@ in {
default = value; default = value;
inherit description; inherit description;
}; };
/*
Generates a list of mkRenamedOptionModule, from a mapping of the old name to
the new name. Nested options can optionally supply a "_name" to indicate its
new name.
# Example
```nix
batchRenameOptions ["nvimTree"] ["nvimTree" "setupOpts"] {
disableNetrw = "disable_netrw";
nestedOption = {
_name = "nested_option";
somethingElse = "something_else";
};
}
```
The above code is equivalent to this:
```nix
[
(
mkRenamedOptionModule
["nvimTree" "disableNetrw"]
["nvimTree" "setupOpts" "disable_netrw"]
)
(
mkRenamedOptionModule
["nvimTree" "nestedOption" "somethingElse"]
["nvimTree" "setupOpts" "nested_option" "something_else"]
)
]
```
*/
batchRenameOptions = oldBasePath: newBasePath: mappings: let
genSetupOptRenames = oldSubpath: newSubpath: table:
mapAttrsToList (
oldName: newNameOrAttr:
if builtins.isAttrs newNameOrAttr
then
genSetupOptRenames (oldSubpath ++ [oldName]) (newSubpath
++ [newNameOrAttr._name or oldName])
newNameOrAttr
else
mkRenamedOptionModule
(oldBasePath ++ oldSubpath ++ [oldName])
(newBasePath ++ newSubpath ++ [newNameOrAttr])
)
table;
in
flatten (genSetupOptRenames [] [] mappings);
} }

View file

@ -3,13 +3,11 @@
lib, lib,
... ...
}: let }: let
inherit (lib.modules) mkRenamedOptionModule;
inherit (lib.options) mkEnableOption mkOption literalExpression; inherit (lib.options) mkEnableOption mkOption literalExpression;
inherit (lib.generators) mkLuaInline; inherit (lib.generators) mkLuaInline;
inherit (lib.types) nullOr str bool int submodule listOf enum oneOf attrs addCheck; inherit (lib.types) nullOr str bool int submodule listOf enum oneOf attrs addCheck;
inherit (lib.nvim.types) mkPluginSetupOption; inherit (lib.nvim.types) mkPluginSetupOption;
inherit (lib.lists) flatten; inherit (lib.nvim.config) batchRenameOptions;
inherit (lib.attrsets) mapAttrsToList;
migrationTable = { migrationTable = {
disableNetrw = "disable_netrw"; disableNetrw = "disable_netrw";
@ -67,21 +65,11 @@
ui = "ui"; ui = "ui";
}; };
renamedSetupOpts = flatten (genSetupOptRenames [] migrationTable); renamedSetupOpts =
batchRenameOptions
# Note: I cut a few corners so it only works in this specific case ["vim" "filetree" "nvimTree"]
# if the parent of a nested option needs to be renamed, this would not work ["vim" "filetree" "nvimTree" "setupOpts"]
genSetupOptRenames = path: table: migrationTable;
mapAttrsToList (
oldName: newNameOrAttr:
if builtins.isAttrs newNameOrAttr
then genSetupOptRenames (path ++ [oldName]) newNameOrAttr
else
mkRenamedOptionModule
(["vim" "filetree" "nvimTree"] ++ path ++ [oldName])
(["vim" "filetree" "nvimTree" "setupOpts"] ++ path ++ [newNameOrAttr])
)
table;
in { in {
imports = renamedSetupOpts; imports = renamedSetupOpts;
options.vim.filetree.nvimTree = { options.vim.filetree.nvimTree = {