Skip to content

Commit

Permalink
helpers: move mkPlugin and mkDefaultOpt to helpers.vim-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
GaetanLepage committed Jan 25, 2024
1 parent ddce82d commit 8f90372
Show file tree
Hide file tree
Showing 22 changed files with 450 additions and 440 deletions.
2 changes: 1 addition & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ You will then need to add Nix options for all (or most) of the upstream plugin o
These options should be in `camelCase` (whereas most plugins define their options in `snake_case`), and their names should match exactly (except the case) to the upstream names.
There are a number of helpers to help you correctly implement them:
- `helpers.mkPlugin`: This helper is useful for simple plugins that are configured through (vim) global variables.
- `helpers.vim-plugin.mkPlugin`: This helper is useful for simple plugins that are configured through (vim) global variables.
- `helpers.defaultNullOpts.{mkBool,mkInt,mkStr,...}`: This family of helpers takes a default value and a description, and sets the Nix default to `null`. These are the main functions you should use to define options.
- `helpers.defaultNullOpts.mkNullable`: This takes a type, a default and a description. This is useful for more complex options.
- `helpers.nixvimTypes.rawLua`: A type to represent raw lua code. The values are of the form `{ __raw = "<code>";}`. This should not be used if the option can only be raw lua code, `mkLua`/`mkLuaFn` should be used in this case.
Expand Down
100 changes: 1 addition & 99 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ with lib; rec {
maintainers = import ./maintainers.nix;
keymaps = import ./keymap-helpers.nix {inherit lib;};
autocmd = import ./autocmd-helpers.nix {inherit lib;};
vim-plugin = import ./vim-plugin.nix {inherit lib mkPackageOption;};

# vim dictionaries are, in theory, compatible with JSON
toVimDict = args:
Expand Down Expand Up @@ -301,105 +302,6 @@ with lib; rec {
description = "Plugin to use for ${name}";
};

mkPlugin = {
config,
lib,
...
}: {
name,
description ? null,
package ? null,
extraPlugins ? [],
extraPackages ? [],
options ? {},
globalPrefix ? "",
...
}: let
cfg = config.plugins.${name};

# TODO support nested options!
pluginOptions =
mapAttrs
(
optName: opt:
opt.option
)
options;
globals =
mapAttrs'
(optName: opt: {
name = let
optGlobal =
if opt.global == null
then optName
else opt.global;
in
globalPrefix + optGlobal;
value = cfg.${optName};
})
options;
# does this evaluate package?
packageOption =
if package == null
then {}
else {
package = mkPackageOption name package;
};

extraConfigOption =
if (isString globalPrefix) && (globalPrefix != "")
then {
extraConfig = mkOption {
type = with types; attrsOf anything;
description = ''
The configuration options for ${name} without the '${globalPrefix}' prefix.
Example: To set '${globalPrefix}_foo_bar' to 1, write
```nix
extraConfig = {
foo_bar = true;
};
```
'';
default = {};
};
}
else {};
in {
options.plugins.${name} =
{
enable = mkEnableOption (
if description == null
then name
else description
);
}
// extraConfigOption
// packageOption
// pluginOptions;

config = mkIf cfg.enable {
inherit extraPackages globals;
# does this evaluate package? it would not be desired to evaluate pacakge if we use another package.
extraPlugins = extraPlugins ++ optional (package != null) cfg.package;
};
};

mkDefaultOpt = {
type,
global ? null,
description ? null,
example ? null,
default ? null,
...
}: {
option = mkOption {
type = types.nullOr type;
inherit default description example;
};

inherit global;
};

extraOptionsOptions = {
extraOptions = mkOption {
default = {};
Expand Down
104 changes: 104 additions & 0 deletions lib/vim-plugin.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
{
lib,
mkPackageOption,
}:
with lib; {
mkPlugin = {
config,
lib,
...
}: {
name,
description ? null,
package ? null,
extraPlugins ? [],
extraPackages ? [],
options ? {},
globalPrefix ? "",
...
}: let
cfg = config.plugins.${name};

# TODO support nested options!
pluginOptions =
mapAttrs
(
optName: opt:
opt.option
)
options;
globals =
mapAttrs'
(optName: opt: {
name = let
optGlobal =
if opt.global == null
then optName
else opt.global;
in
globalPrefix + optGlobal;
value = cfg.${optName};
})
options;
# does this evaluate package?
packageOption =
if package == null
then {}
else {
package = mkPackageOption name package;
};

extraConfigOption =
if (isString globalPrefix) && (globalPrefix != "")
then {
extraConfig = mkOption {
type = with types; attrsOf anything;
description = ''
The configuration options for ${name} without the '${globalPrefix}' prefix.
Example: To set '${globalPrefix}_foo_bar' to 1, write
```nix
extraConfig = {
foo_bar = true;
};
```
'';
default = {};
};
}
else {};
in {
options.plugins.${name} =
{
enable = mkEnableOption (
if description == null
then name
else description
);
}
// extraConfigOption
// packageOption
// pluginOptions;

config = mkIf cfg.enable {
inherit extraPackages globals;
# does this evaluate package? it would not be desired to evaluate pacakge if we use another package.
extraPlugins = extraPlugins ++ optional (package != null) cfg.package;
};
};

mkDefaultOpt = {
type,
global ? null,
description ? null,
example ? null,
default ? null,
...
}: {
option = mkOption {
type = types.nullOr type;
inherit default description example;
};

inherit global;
};
}
2 changes: 1 addition & 1 deletion plugins/completion/copilot-vim.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
} @ args:
with lib;
(
with import ../helpers.nix {inherit lib;};
with (import ../helpers.nix {inherit lib;}).vim-plugin;
mkPlugin args {
name = "copilot-vim";
description = "copilot.vim";
Expand Down
2 changes: 1 addition & 1 deletion plugins/completion/nvim-cmp/cmp-helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
}: let
helpers = import ../../helpers.nix {inherit lib;};
in
with helpers;
with helpers.vim-plugin;
with lib; {
mkCmpSourcePlugin = {
name,
Expand Down
2 changes: 1 addition & 1 deletion plugins/git/fugitive.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
} @ attrs: let
helpers = import ../helpers.nix {inherit lib;};
in
with helpers;
with helpers.vim-plugin;
with lib;
mkPlugin attrs {
name = "fugitive";
Expand Down
2 changes: 1 addition & 1 deletion plugins/languages/ledger.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
...
} @ args:
with lib;
with import ../helpers.nix {inherit lib;};
with (import ../helpers.nix {inherit lib;}).vim-plugin;
mkPlugin args {
name = "ledger";
description = "ledger language features";
Expand Down
2 changes: 1 addition & 1 deletion plugins/languages/markdown-preview.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
...
} @ args:
with lib;
with import ../helpers.nix {inherit lib;};
with (import ../helpers.nix {inherit lib;}).vim-plugin;
mkPlugin args {
name = "markdown-preview";
description = "markdown-preview.nvim";
Expand Down
2 changes: 1 addition & 1 deletion plugins/languages/nix.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
} @ attrs: let
helpers = import ../helpers.nix {inherit lib;};
in
with helpers;
with helpers.vim-plugin;
with lib;
mkPlugin attrs {
name = "nix";
Expand Down
2 changes: 1 addition & 1 deletion plugins/languages/tagbar.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
...
} @ args:
with lib;
with import ../helpers.nix {inherit lib;};
with (import ../helpers.nix {inherit lib;}).vim-plugin;
mkPlugin args {
name = "tagbar";
package = pkgs.vimPlugins.tagbar;
Expand Down
Loading

0 comments on commit 8f90372

Please sign in to comment.