init/autocmds: fix autogroups
Some checks are pending
Check for typos in the source tree / check-typos (push) Waiting to run

This commit is contained in:
raf 2025-02-22 20:39:58 +03:00
parent 403072cae3
commit 80fe9244e2
No known key found for this signature in database
GPG key ID: 29D95B64378DB4BF

View file

@ -6,7 +6,7 @@
inherit (lib.options) mkOption mkEnableOption; inherit (lib.options) mkOption mkEnableOption;
inherit (lib.lists) filter; inherit (lib.lists) filter;
inherit (lib.strings) optionalString; inherit (lib.strings) optionalString;
inherit (lib.types) submodule listOf str bool; inherit (lib.types) nullOr submodule listOf str bool;
inherit (lib.nvim.types) luaInline; inherit (lib.nvim.types) luaInline;
inherit (lib.nvim.lua) toLuaObject; inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.dag) entryAfter; inherit (lib.nvim.dag) entryAfter;
@ -21,40 +21,46 @@
}; };
event = mkOption { event = mkOption {
type = listOf str; type = nullOr (listOf str);
default = null;
example = ["BufRead" "BufWritePre"]; example = ["BufRead" "BufWritePre"];
description = "The event(s) that trigger the autocommand."; description = "The event(s) that trigger the autocommand.";
}; };
pattern = mkOption { pattern = mkOption {
type = listOf str; type = nullOr (listOf str);
default = null;
example = ["*.lua" "*.vim"]; example = ["*.lua" "*.vim"];
description = "The file pattern(s) that determine when the autocommand applies)."; description = "The file pattern(s) that determine when the autocommand applies).";
}; };
callback = mkOption { callback = mkOption {
type = luaInline; type = nullOr luaInline;
default = null;
example = '' example = ''
function() function()
print("Saving a Lua file...") print("Saving a Lua file...")
end, end,
''; '';
description = "The file pattern(s) that determine when the autocommand applies)."; description = "The file pattern(s) that determine when the autocommand applies.";
}; };
command = mkOption { command = mkOption {
type = str; type = nullOr str;
default = null;
description = "Vim command string instead of a Lua function."; description = "Vim command string instead of a Lua function.";
}; };
group = mkOption { group = mkOption {
type = str; type = nullOr str;
default = null;
example = "MyAutoCmdGroup"; example = "MyAutoCmdGroup";
description = "An optional autocommand group to manage related autocommands."; description = "An optional autocommand group to manage related autocommands.";
}; };
desc = mkOption { desc = mkOption {
type = str; type = nullOr str;
default = null;
example = "Notify when saving a Lua file"; example = "Notify when saving a Lua file";
description = "A description for the autocommand."; description = "A description for the autocommand.";
}; };
@ -127,36 +133,51 @@ in {
}; };
}; };
config.vim = let config = {
enabledAutocommands = filter (cmd: cmd.enable) cfg.autocmds; vim = let
enabledAutogroups = filter (au: au.enable) cfg.augroups; enabledAutocommands = filter (cmd: cmd.enable) cfg.autocmds;
in { enabledAutogroups = filter (au: au.enable) cfg.augroups;
luaConfigRC = { in {
augroups = entryAfter ["pluginConfigs"] (optionalString (enabledAutogroups != []) '' luaConfigRC = {
local nvf_autogroups = ${toLuaObject cfg.autogroups} augroups = entryAfter ["pluginConfigs"] (optionalString (enabledAutogroups != []) ''
for group_name, options in pairs(nvf_autogroups) do local nvf_autogroups = {}
vim.api.nvim_create_augroup(group_name, options) for _, group in ipairs(${toLuaObject enabledAutogroups}) do
end if group.name then
''); nvf_autogroups[group.name] = { clear = group.clear }
end
end
autocmds = entryAfter ["pluginConfigs"] (optionalString (enabledAutocommands != []) '' for group_name, options in pairs(nvf_autogroups) do
local nvf_autocommands = ${toLuaObject enabledAutocommands} vim.api.nvim_create_augroup(group_name, options)
for _, autocmd in ipairs(nvf_autocommands) do end
vim.api.nvim_create_autocmd( '');
autocmd.event,
{ autocmds = entryAfter ["pluginConfigs"] (optionalString (enabledAutocommands != []) ''
group = autocmd.group, local nvf_autocommands = ${toLuaObject enabledAutocommands}
pattern = autocmd.pattern, for _, autocmd in ipairs(nvf_autocommands) do
buffer = autocmd.buffer, vim.api.nvim_create_autocmd(
desc = autocmd.desc, autocmd.event,
callback = autocmd.callback, {
command = autocmd.command, group = autocmd.group,
once = autocmd.once, pattern = autocmd.pattern,
nested = autocmd.nested buffer = autocmd.buffer,
} desc = autocmd.desc,
) callback = autocmd.callback,
end command = autocmd.command,
''); once = autocmd.once,
nested = autocmd.nested
}
)
end
'');
};
}; };
assertions = [
{
assertion = builtins.all (cmd: !(cmd.command != null && cmd.callback != null)) cfg.autocmds;
message = "An autocommand cannot have both 'command' and 'callback' defined at the same time.";
}
];
}; };
} }