2024-04-20 23:10:06 +00:00
|
|
|
{
|
|
|
|
config,
|
|
|
|
lib,
|
|
|
|
...
|
|
|
|
}: let
|
2024-07-20 08:30:48 +00:00
|
|
|
inherit (builtins) map mapAttrs filter;
|
2024-04-21 01:28:56 +00:00
|
|
|
inherit (lib.options) mkOption;
|
2024-08-12 00:08:53 +00:00
|
|
|
inherit (lib.attrsets) mapAttrsToList filterAttrs getAttrs attrValues attrNames;
|
2024-07-20 08:30:48 +00:00
|
|
|
inherit (lib.strings) concatLines concatMapStringsSep;
|
2024-04-20 23:10:06 +00:00
|
|
|
inherit (lib.trivial) showWarnings;
|
2024-04-21 01:28:56 +00:00
|
|
|
inherit (lib.types) str nullOr;
|
2024-04-20 23:10:06 +00:00
|
|
|
inherit (lib.generators) mkLuaInline;
|
2024-07-20 08:30:48 +00:00
|
|
|
inherit (lib.nvim.dag) entryAfter mkLuarcSection resolveDag entryAnywhere;
|
|
|
|
inherit (lib.nvim.lua) toLuaObject;
|
2024-04-20 23:10:06 +00:00
|
|
|
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...)
|
|
|
|
}
|
|
|
|
*/
|
2024-05-18 12:52:40 +00:00
|
|
|
normalizeAction = action: {
|
2024-04-20 23:10:06 +00:00
|
|
|
# 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
|
2024-07-20 08:30:48 +00:00
|
|
|
filterNonNull = attrs: filterAttrs (_: value: value != null) attrs;
|
2024-04-20 23:10:06 +00:00
|
|
|
globalsScript =
|
2024-08-12 00:08:53 +00:00
|
|
|
mapAttrsToList (name: value: "vim.g.${name} = ${toLuaObject value}")
|
2024-04-20 23:10:06 +00:00
|
|
|
(filterNonNull cfg.globals);
|
|
|
|
|
2024-07-20 08:30:48 +00:00
|
|
|
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-04-20 23:10:06 +00:00
|
|
|
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;
|
|
|
|
|
2024-07-20 08:30:48 +00:00
|
|
|
maps = [
|
|
|
|
nmap
|
|
|
|
imap
|
|
|
|
vmap
|
|
|
|
xmap
|
|
|
|
smap
|
|
|
|
cmap
|
|
|
|
omap
|
|
|
|
tmap
|
|
|
|
lmap
|
|
|
|
icmap
|
|
|
|
allmap
|
|
|
|
];
|
|
|
|
mappings = concatLines (map concatLines maps);
|
2024-04-20 23:10:06 +00:00
|
|
|
in {
|
|
|
|
vim = {
|
2024-07-20 08:30:48 +00:00
|
|
|
luaConfigRC = {
|
2024-07-08 21:57:58 +00:00
|
|
|
globalsScript = entryAnywhere (concatLines globalsScript);
|
2024-09-05 18:06:24 +00:00
|
|
|
# basic
|
|
|
|
pluginConfigs = entryAfter ["basic"] pluginConfigs;
|
2024-07-20 08:30:48 +00:00
|
|
|
extraPluginConfigs = entryAfter ["pluginConfigs"] extraPluginConfigs;
|
|
|
|
mappings = entryAfter ["extraPluginConfigs"] mappings;
|
2024-04-20 23:10:06 +00:00
|
|
|
};
|
|
|
|
|
2024-07-20 08:30:48 +00:00
|
|
|
builtLuaConfigRC = let
|
2024-04-20 23:10:06 +00:00
|
|
|
# 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 != []
|
2024-07-08 21:57:58 +00:00
|
|
|
then throw "\nFailed assertions:\n${concatMapStringsSep "\n" (x: "- ${x}") failedAssertions}"
|
2024-04-20 23:10:06 +00:00
|
|
|
else showWarnings config.warnings;
|
|
|
|
|
2024-07-20 08:30:48 +00:00
|
|
|
luaConfig = resolveDag {
|
|
|
|
name = "lua config script";
|
|
|
|
dag = cfg.luaConfigRC;
|
|
|
|
mapResult = result:
|
|
|
|
concatLines [
|
|
|
|
cfg.luaConfigPre
|
|
|
|
(concatMapStringsSep "\n" mkLuarcSection result)
|
|
|
|
cfg.luaConfigPost
|
|
|
|
];
|
2024-04-20 23:10:06 +00:00
|
|
|
};
|
|
|
|
in
|
2024-07-20 08:30:48 +00:00
|
|
|
baseSystemAssertWarn luaConfig;
|
2024-04-20 23:10:06 +00:00
|
|
|
};
|
|
|
|
};
|
|
|
|
}
|