Skip to content

Commit

Permalink
dev: extract common functions into lib
Browse files Browse the repository at this point in the history
  • Loading branch information
horriblename committed Dec 11, 2023
1 parent 6a104aa commit 58795d8
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 27 deletions.
35 changes: 30 additions & 5 deletions lib/types/plugins.nix
Original file line number Diff line number Diff line change
Expand Up @@ -127,8 +127,20 @@ with lib; let
};
};
};

# walk the options tree and extract default values
# without this thing I can't set `setupOpts`'s default value, which leads to:
#
# error: The option `vim.telescope.setupOpts' is used but not defined.
#
# TODO: how do I avoid this mess??
extractDefault = mapAttrs (k: v:
assert builtins.typeOf v == "set";
if v ? _type && v._type == "option"
then v.default
else extractDefault v);
in {
inherit extraPluginType;
inherit extraPluginType extractDefault;

pluginsOpt = {
description,
Expand All @@ -145,11 +157,24 @@ in {
lua = code;
};

mkPluginSetupOption = pluginName: submodule:
# opts is a attrset of options, example:
# ```
# mkPluginSetupOption "telescope" {
# file_ignore_patterns = mkOption {
# description = "...";
# type = types.listOf types.str;
# default = [];
# };
# layout_config.horizontal = mkOption {...};
# }
# ```
mkPluginSetupOption = pluginName: opts:
mkOption {
# TODO: example
# FIXME: DON'T MERGE THIS. might not be needed after all.
description = "Option table to pass into the setup function of " + pluginName;
type = types.submodule (submodule // {freeformType = types.attrs;});
default = extractDefault opts;
type = types.submodule {
freeformType = with types; attrsOf anything;
options = opts;
};
};
}
23 changes: 1 addition & 22 deletions modules/utility/telescope/telescope.nix
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,6 @@
inherit type default;
};

# walk the options tree and extract default values
# without this thing I can't set `setupOpts`'s default value, which leads to:
#
# error: The option `vim.telescope.setupOpts' is used but not defined.
#
# TODO: how do I avoid this mess??
extractDefault = builtins.mapAttrs (k: v:
assert builtins.typeOf v == "set";
if v ? _type && v._type == "option"
then v.default
else extractDefault v);

setupOptions = {
defaults = {
vimgrep_arguments = mkOptOfType (with types; listOf str) [
Expand Down Expand Up @@ -66,11 +54,6 @@
winblend = mkOptOfType types.int 0;
};
};

setupOptsSubmodule = types.submodule {
freeformType = with types; attrsOf anything;
options = setupOptions;
};
in {
options.vim.telescope = {
mappings = {
Expand Down Expand Up @@ -101,10 +84,6 @@ in {

enable = mkEnableOption "telescope.nvim: multi-purpose search and picker utility";

setupOpts = mkOption {
description = "Option table to pass into the setup function of Telescope.";
default = extractDefault setupOptions;
type = setupOptsSubmodule;
};
setupOpts = nvim.types.mkPluginSetupOption "Telescope" setupOptions;
};
}

0 comments on commit 58795d8

Please sign in to comment.