nixeovim/modules/vim/augroup.nix

130 lines
3.3 KiB
Nix

{ config, lib, ... }:
let
inherit (lib.nix2lua) call;
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 = call "vim.api.nvim_create_autocmd" [
config.event
{
inherit (config) pattern buffer desc callback command once nested;
group = call "vim.api.nvim_create_augroup" [ config.group { } ];
}
];
};
};
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
);
};
}