nixeovim/modules/vim/augroup.nix

130 lines
3.2 KiB
Nix
Raw Permalink Normal View History

2024-05-03 17:50:57 +03:00
{ config, lib, ... }:
let
2024-05-09 02:28:24 +03:00
inherit (lib.nix2lua) call;
2024-05-03 17:50:57 +03:00
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 {
2024-05-17 15:27:04 +03:00
type = with types; nullOr extCommas;
2024-05-03 17:50:57 +03:00
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;
2024-05-09 02:28:24 +03:00
genConfig = call "vim.api.nvim_create_autocmd" [
2024-05-03 17:50:57 +03:00
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
);
};
}