Skip to content

Commit

Permalink
lib: cleanup with lib
Browse files Browse the repository at this point in the history
  • Loading branch information
khaneliman committed Sep 4, 2024
1 parent c76e507 commit 35788bb
Show file tree
Hide file tree
Showing 9 changed files with 179 additions and 160 deletions.
6 changes: 4 additions & 2 deletions lib/autocmd-helpers.nix
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
{ lib, helpers }:
with lib;
let
inherit (lib) types;
in
rec {
autoGroupOption = types.submodule {
options = {
clear = mkOption {
clear = lib.mkOption {
type = types.bool;
description = "Clear existing commands if the group already exists.";
default = true;
Expand Down
45 changes: 23 additions & 22 deletions lib/deprecation.nix
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
{ 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
else if lib.isOption opt then
getOptionRecursive (opt.type.getSubOptions prefix) prefix optionPath
else
let
name = head optionPath;
opt' = getAttr name opt;
name = lib.head optionPath;
opt' = lib.getAttr name opt;
prefix' = prefix ++ [ name ];
optionPath' = drop 1 optionPath;
optionPath' = lib.drop 1 optionPath;
in
getOptionRecursive opt' prefix' optionPath';

Expand All @@ -24,24 +23,26 @@ rec {
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;
});
options = lib.setAttrByPath optionPath (
lib.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.
lib.optional opt.isDefined ''
The option definition `${lib.showOption optionPath}' in ${lib.showFiles opt.files} is deprecated.
${replacementInstructions}
'';
};
Expand All @@ -51,11 +52,11 @@ rec {
map (
option':
let
option = toList option';
option = lib.toList option';
oldPath = oldPrefix ++ option;
newPath = newPrefix ++ map nixvim.toSnakeCase option;
newPath = newPrefix ++ map lib.nixvim.toSnakeCase option;
in
mkRenamedOptionModule oldPath newPath
lib.mkRenamedOptionModule oldPath newPath
);

# A clone of types.coercedTo, but it prints a warning when oldType is used.
Expand Down
31 changes: 16 additions & 15 deletions lib/keymap-helpers.nix
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{ lib, helpers }:
with lib;
let
inherit (lib) optionalAttrs isAttrs types;
in
rec {
# These are the configuration options that change the behavior of each mapping.
mapConfigOptions = {
Expand All @@ -17,7 +19,7 @@ rec {

remap = helpers.defaultNullOpts.mkBool false "Make the mapping recursive. Inverses `noremap`.";

desc = helpers.mkNullOrOption types.str "A textual description of this keybind, to be shown in which-key, if you have it.";
desc = helpers.mkNullOrOption lib.types.str "A textual description of this keybind, to be shown in which-key, if you have it.";

buffer = helpers.defaultNullOpts.mkBool false "Make the mapping buffer-local. Equivalent to adding `<buffer>` to a map.";
};
Expand Down Expand Up @@ -52,9 +54,9 @@ rec {
};

modeEnum =
types.enum
lib.types.enum
# ["" "n" "v" ...]
(map ({ short, ... }: short) (attrValues modes));
(map ({ short, ... }: short) (lib.attrValues modes));

mapOptionSubmodule = mkMapOptionSubmodule { };

Expand All @@ -66,8 +68,8 @@ rec {

mkModeOption =
default:
mkOption {
type = with types; either modeEnum (listOf modeEnum);
lib.mkOption {
type = with lib.types; either modeEnum (listOf modeEnum);
description = ''
One or several modes.
Use the short-names (`"n"`, `"v"`, ...).
Expand Down Expand Up @@ -96,17 +98,16 @@ rec {
extraOptions ? { },
extraModules ? [ ],
}:
with types;
submodule (
types.submodule (
{ config, options, ... }:
{
imports = extraModules;

options =
(optionalAttrs (isAttrs key || key) {
key = mkOption (
(lib.optionalAttrs (isAttrs key || key) {
key = lib.mkOption (
{
type = str;
type = types.str;
description = "The key to map.";
example = "<C-m>";
}
Expand All @@ -115,9 +116,9 @@ rec {
);
})
// (optionalAttrs (isAttrs action || action) {
action = mkOption (
action = lib.mkOption (
{
type = maybeRaw str;
type = types.maybeRaw types.str;
description = "The action to execute.";
apply = v: if options.lua.isDefined or false && config.lua then helpers.mkRaw v else v;
}
Expand All @@ -126,9 +127,9 @@ rec {
);
})
// optionalAttrs (isAttrs lua || lua) {
lua = mkOption (
lua = lib.mkOption (
{
type = bool;
type = types.bool;
description = ''
If true, `action` is considered to be lua code.
Thus, it will not be wrapped in `""`.
Expand Down
47 changes: 26 additions & 21 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
{ lib, helpers }:
with lib;
{
# TODO: DEPRECATED: use the `settings` option instead
extraOptionsOptions = {
extraOptions = mkOption {
extraOptions = lib.mkOption {
default = { };
type = with types; attrsOf anything;
type = with lib.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.
Expand Down Expand Up @@ -78,7 +77,7 @@ with lib;

options.${namespace}.${name} =
{
enable = mkEnableOption originalName;
enable = lib.mkEnableOption originalName;
package =
if lib.isOption package then
package
Expand All @@ -94,7 +93,7 @@ with lib;
];
};
}
// optionalAttrs hasSettings {
// lib.optionalAttrs hasSettings {
settings = helpers.mkSettingsOption {
description = settingsDescription;
options = settingsOptions;
Expand All @@ -103,19 +102,25 @@ with lib;
}
// extraOptions;

config = mkIf cfg.enable (mkMerge [
{
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;
}
(optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)})
'';
})
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
]);
config = lib.mkIf cfg.enable (
lib.mkMerge [
{
extraPlugins = (lib.optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;
}
(lib.optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${
lib.optionalString (cfg ? settings) (helpers.toLuaObject cfg.settings)
})
'';
})
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme;
})
(extraConfig cfg)
]
);
};
in
{
Expand All @@ -129,9 +134,9 @@ with lib;
in
imports
++ [ module ]
++ (optional deprecateExtraOptions (
mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath
++ (lib.optional deprecateExtraOptions (
lib.mkRenamedOptionModule (basePluginPath ++ [ "extraOptions" ]) settingsPath
))
++ (nixvim.mkSettingsRenamedOptionModules basePluginPath settingsPath optionsRenamedToSettings);
++ (lib.nixvim.mkSettingsRenamedOptionModules basePluginPath settingsPath optionsRenamedToSettings);
};
}
Loading

0 comments on commit 35788bb

Please sign in to comment.