Skip to content

Commit

Permalink
lib/deprecation: init with mkDeprecatedSubOptionModule
Browse files Browse the repository at this point in the history
Similar to `lib.mkRemovedOptionModule` but tweaked to work with
sub-options (e.g. settings options).

Also uses warnings instead of assertions.
  • Loading branch information
MattSturgeon committed Jun 25, 2024
1 parent 66c8592 commit 54d1188
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
49 changes: 49 additions & 0 deletions lib/deprecation.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{ lib, ... }:
with lib;
rec {
# Get a (sub)option by walking the path,
# checking for submodules along the way
getOptionRecursive =
opt: prefix: optionPath:
if optionPath == [ ] then
opt
else if isOption opt then
getOptionRecursive (opt.type.getSubOptions prefix) prefix optionPath
else
let
name = head optionPath;
opt' = getAttr name opt;
prefix' = prefix ++ [ name ];
optionPath' = drop 1 optionPath;
in
getOptionRecursive opt' prefix' optionPath';

# Like mkRemovedOptionModule, but has support for nested sub-options
# and uses warnings instead of assertions.
mkDeprecatedSubOptionModule =
optionPath: replacementInstructions:
{ options, ... }:
{
options = setAttrByPath optionPath (mkOption {
# When (e.g.) `mkAttrs` is used on a submodule, this option will be evaluated.
# Therefore we have to apply _something_ (null) when there's no definition.
apply =
v:
let
# Avoid "option used but not defined" errors
res = builtins.tryEval v;
in
if res.success then res.value else null;
visible = false;
});
config.warnings =
let
opt = getOptionRecursive options [ ] optionPath;
in
optional opt.isDefined ''
The option definition `${showOption optionPath}' in ${showFiles opt.files} is deprecated.
${replacementInstructions}
'';
};

}
2 changes: 2 additions & 0 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ let
nixvimTypes = import ./types.nix { inherit lib nixvimOptions; };
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
inherit (import ./to-lua.nix { inherit lib; }) toLuaObject;
in
{
Expand All @@ -30,3 +31,4 @@ in
// nixvimUtils
// nixvimOptions
// nixvimBuilders
// nixvimDeprecation

0 comments on commit 54d1188

Please sign in to comment.