modules: add fn module to define functions

This commit is contained in:
Dmitriy Pleshevskiy 2024-04-30 16:54:48 +03:00
parent 6086cea1d4
commit 4cc9de194f
Signed by: pleshevskiy
GPG Key ID: 17041163DA10A9A2
4 changed files with 62 additions and 2 deletions

View File

@ -43,7 +43,7 @@ let
'';
};
buffer = mkOption {
type = with types; nullOr str;
type = with types; nullOr attrs;
default = null;
description = ''
The mapping will be effective in the current buffer only.

View File

@ -9,6 +9,7 @@
./modules/filetype.nix
./modules/input.nix
./modules/plugin.nix
./modules/fn.nix
./modules/plugins/theme/catppuccin.nix
./modules/plugins/style/neoformat.nix

59
modules/fn.nix Normal file
View File

@ -0,0 +1,59 @@
{ lib, ... }:
let
fnOpts = { name, config, ... }: {
options = with lib; {
args = mkOption {
type = with types; listOf str;
default = [ ];
};
content = mkOption {
type = with types; nullOr attrs;
default = null;
};
settings = mkOption {
type = types.functionTo (lib.types.submoduleWith {
modules = [
./vim/keymap.nix
# ./vim/opts.nix
];
specialArgs = { inherit lib; };
});
default = { };
};
lambda = mkOption {
type = types.attrs;
readOnly = true;
internal = true;
};
};
config = {
lambda = with lib.nix2lua;
let
fnArgs = builtins.listToAttrs (map (v: { name = v; value = raw v; }) config.args);
innerCfg = config.settings fnArgs;
in
lambda config.args (lib.flatten [
config.content
(lib.flip map innerCfg.vim.keymap.set ({ mode, lhs, rhs, ... } @ vars:
let
buf = lib.optionalString (vars.buffer != null) vars.buffer.raw;
m = if builtins.isList mode then lib.head mode else mode;
in
if innerCfg.vim.keymap._validate."${buf}"."${m}"."${lhs}" == rhs then vars.genConfig
else abort "This case should never happen."
))
]);
};
};
in
{
options.fn = with lib; mkOption {
type = with types; attrsOf (submodule fnOpts);
default = { };
};
}

View File

@ -30,7 +30,7 @@ let cfg = config.vim.keymap; in
else m
)));
buf = lib.optionalString (buffer != null) buffer;
buf = lib.optionalString (buffer != null) buffer.raw;
in
lib.foldl lib.recursiveUpdate { }
(lib.flip map unwrappedModes (m: {