nixeovim/modules/vim/augroup.nix

128 lines
3.3 KiB
Nix
Raw Normal View History

2024-05-03 17:50:57 +03:00
{ config, lib, ... }:
let
augroupsCfg = config.vim.augroup;
autocmd = { name, config, ... }: {
options = with lib; {
group = mkOption {
type = types.str;
description = ''
autocommand group name.
'';
};
event = mkOption {
type = with types; either str (listOf str);
description = ''
Event(s) that will trigger the handler (`callback` or `command`).
'';
};
pattern = mkOption {
type = with types; nullOr (either str (listOf str));
default = null;
description = ''
pattern(s) to match literally.
`:help autocmd-pattern
'';
};
buffer = mkOption {
type = with types; nullOr attrs;
default = null;
description = ''
buffer number for buffer-local autocommands.
Cannot be used with `pattern`.
`:help autocmd-buflocal`
'';
};
desc = mkOption {
type = with types; nullOr str;
default = null;
description = ''
description (for documentation and troubleshooting).
'';
};
callback = mkOption {
type = with types; nullOr attrs;
default = null;
description = ''
Lua function (or Vimscript function name) called when the event(s) is triggered.
'';
};
command = mkOption {
type = with types; nullOr str;
default = null;
description = ''
Vim command to execute on event. Cannot be used with `callback`.
'';
};
once = mkOption {
type = types.bool;
default = false;
description = ''
Run the autocommand only once.
`:help autocmd-once`
'';
};
nested = mkOption {
type = types.bool;
default = false;
description = ''
Run nested autocommands.
`:help autocmd-nested`
'';
};
genConfig = mkOption {
type = types.attrs;
internal = true;
};
};
config = {
group = lib.mkDefault name;
genConfig = with lib.nix2lua; call "vim.api.nvim_create_autocmd" [
config.event
{
inherit (config) pattern buffer desc callback command once nested;
2024-05-05 18:17:31 +03:00
group = call "vim.api.nvim_create_augroup" [ config.group { } ];
2024-05-03 17:50:57 +03:00
}
];
};
};
in
{
options.vim.augroup = with lib; mkOption {
type = with types; attrsOf (submodule autocmd);
default = { };
};
config = lib.mkIf (augroupsCfg != { }) {
assertions = lib.flatten (lib.mapAttrsToList
(k: { pattern, buffer, command, callback, ... }: [
{
assertion = (buffer != null -> pattern == null)
|| (pattern != null -> buffer == null);
message = "vim.augroup.${k}: `buffer` cannot be used with `pattern`";
}
{
assertion = callback != null || command != null;
message = "vim.augroup.${k}: either `command` or `callback` is required";
}
{
assertion = (callback != null -> command == null)
|| (command != null -> callback == null);
message = "vim.augroup.${k}: `command` cannot be used with `callback`";
}
])
augroupsCfg
);
};
}