nvf/modules/wrapper/rc/config.nix
diniamo f9789432f9
treewide: make the entire generated config lua based (#333)
* modules: switch to gerg's neovim-wrapper

* modules: use initViml instead of writing the file

* treewide: make the entire generated config lua based

* docs: remove mentions of configRC

* plugins/treesitter: remove vim.cmd hack

* treewide: move resolveDag to lib

* modules/wrapper(rc): fix typo

* treewide: migrate to pluginRC for correct DAG order

The "new" DAG order is as follows:
- (luaConfigPre)
- globalsScript
- basic
- theme
- pluginConfigs
- extraPluginConfigs
- mappings
- (luaConfigPost)

* plugins/theme: fix theme DAG place

* plugins/theme: fix fixed theme DAG place

* modules/wrapper(rc): add removed option module for configRC

* docs: add dag-entries chapter, add release note entry

* fix: formatting CI

* languages/nix: add missing `local`

* docs: fix page link

* docs: add mention of breaking changes at the start of the release notes

* plugins/neo-tree: convert to pluginRC

* modules/wrapper(rc): add back entryAnywhere

* modules/wrapper(rc): expose pluginRC

* apply raf patch

---------

Co-authored-by: NotAShelf <raf@notashelf.dev>
2024-07-20 08:30:48 +00:00

167 lines
5.3 KiB
Nix

{
config,
lib,
...
}: let
inherit (builtins) map mapAttrs filter;
inherit (lib.options) mkOption;
inherit (lib.attrsets) filterAttrs getAttrs attrValues attrNames;
inherit (lib.strings) concatLines concatMapStringsSep;
inherit (lib.misc) mapAttrsFlatten;
inherit (lib.trivial) showWarnings;
inherit (lib.types) str nullOr;
inherit (lib.generators) mkLuaInline;
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
inherit (lib.nvim.lua) toLuaObject;
inherit (lib.nvim.config) mkBool;
cfg = config.vim;
# Most of the keybindings code is highly inspired by pta2002/nixvim.
# Thank you!
mapConfigOptions = {
silent =
mkBool false
"Whether this mapping should be silent. Equivalent to adding <silent> to a map.";
nowait =
mkBool false
"Whether to wait for extra input on ambiguous mappings. Equivalent to adding <nowait> to a map.";
script =
mkBool false
"Equivalent to adding <script> to a map.";
expr =
mkBool false
"Means that the action is actually an expression. Equivalent to adding <expr> to a map.";
unique =
mkBool false
"Whether to fail if the map is already defined. Equivalent to adding <unique> to a map.";
noremap =
mkBool true
"Whether to use the 'noremap' variant of the command, ignoring any custom mappings on the defined action. It is highly advised to keep this on, which is the default.";
desc = mkOption {
type = nullOr str;
default = null;
description = "A description of this keybind, to be shown in which-key, if you have it enabled.";
};
};
genMaps = mode: maps: let
/*
Take a user-defined action (string or attrs) and return the following attribute set:
{
action = (string) the actual action to map to this key
config = (attrs) the configuration options for this mapping (noremap, silent...)
}
*/
normalizeAction = action: {
# Extract the values of the config options that have been explicitly set by the user
config =
filterAttrs (_: v: v != null)
(getAttrs (attrNames mapConfigOptions) action);
action =
if action.lua
then mkLuaInline action.action
else action.action;
};
in
attrValues (mapAttrs
(key: action: let
normalizedAction = normalizeAction action;
in {
inherit (normalizedAction) action config;
inherit key;
inherit mode;
})
maps);
in {
config = let
filterNonNull = attrs: filterAttrs (_: value: value != null) attrs;
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);
};
toLuaBindings = mode: maps:
map (value: ''
vim.keymap.set(${toLuaObject mode}, ${toLuaObject value.key}, ${toLuaObject value.action}, ${toLuaObject value.config})
'') (genMaps mode maps);
# I'm not sure if every one of these will work.
allmap = toLuaBindings "" config.vim.maps.normalVisualOp;
nmap = toLuaBindings "n" config.vim.maps.normal;
vmap = toLuaBindings "v" config.vim.maps.visual;
xmap = toLuaBindings "x" config.vim.maps.visualOnly;
smap = toLuaBindings "s" config.vim.maps.select;
imap = toLuaBindings "i" config.vim.maps.insert;
cmap = toLuaBindings "c" config.vim.maps.command;
tmap = toLuaBindings "t" config.vim.maps.terminal;
lmap = toLuaBindings "l" config.vim.maps.lang;
omap = toLuaBindings "o" config.vim.maps.operator;
icmap = toLuaBindings "ic" config.vim.maps.insertCommand;
maps = [
nmap
imap
vmap
xmap
smap
cmap
omap
tmap
lmap
icmap
allmap
];
mappings = concatLines (map concatLines maps);
in {
vim = {
luaConfigRC = {
globalsScript = entryAnywhere (concatLines globalsScript);
# basic, theme
pluginConfigs = entryAfter ["theme"] pluginConfigs;
extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
mappings = entryAfter ["extraPluginConfigs"] mappings;
};
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;
};
};
}