neovim-flake/modules/wrapper/rc/config.nix

84 lines
2.6 KiB
Nix
Raw Normal View History

{
config,
lib,
...
}: let
2024-08-05 12:45:48 +02:00
inherit (builtins) map mapAttrs filter attrsToList;
inherit (lib.attrsets) filterAttrs;
inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.misc) mapAttrsFlatten;
inherit (lib.trivial) showWarnings;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject;
cfg = config.vim;
in {
config = let
2024-08-05 12:45:48 +02:00
filterNonNull = filterAttrs (_: value: value != null);
globalsScript =
mapAttrsFlatten (name: value: "vim.g.${name} = ${toLuaObject value}")
(filterNonNull cfg.globals);
extraPluginConfigs = resolveDag {
name = "extra plugin configs";
dag = mapAttrs (_: value: entryAfter value.after value.setup) cfg.extraPlugins;
mapResult = result: concatLines (map mkLuarcSection result);
};
pluginConfigs = resolveDag {
name = "plugin configs";
dag = cfg.pluginRC;
mapResult = result: concatLines (map mkLuarcSection result);
};
2024-08-05 12:45:48 +02:00
getAction = keymap:
if keymap.lua
then mkLuaInline keymap.action
else keymap.action;
getOpts = keymap: {
inherit (keymap) silent nowait script expr unique noremap;
};
2024-08-05 12:45:48 +02:00
toLuaKeymap = {
name,
value,
}: "vim.keymap.set(${toLuaObject value.mode}, ${toLuaObject name}, ${toLuaObject (getAction value)}, ${toLuaObject (getOpts value)})";
2024-08-05 12:45:48 +02:00
keymaps = concatLines (map toLuaKeymap (attrsToList (filterNonNull config.maps)));
in {
vim = {
luaConfigRC = {
globalsScript = entryAnywhere (concatLines globalsScript);
# basic, theme
pluginConfigs = entryAfter ["theme"] pluginConfigs;
extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
2024-08-05 12:45:48 +02:00
mappings = entryAfter ["extraPluginConfigs"] keymaps;
};
builtLuaConfigRC = let
# Catch assertions and warnings
# and throw for each failed assertion. If no assertions are found, show warnings.
failedAssertions = map (x: x.message) (filter (x: !x.assertion) config.assertions);
baseSystemAssertWarn =
if failedAssertions != []
then throw "\nFailed assertions:\n${concatMapStringsSep "\n" (x: "- ${x}") failedAssertions}"
else showWarnings config.warnings;
luaConfig = resolveDag {
name = "lua config script";
dag = cfg.luaConfigRC;
mapResult = result:
concatLines [
cfg.luaConfigPre
(concatMapStringsSep "\n" mkLuarcSection result)
cfg.luaConfigPost
];
};
in
baseSystemAssertWarn luaConfig;
};
};
}