2023-12-25 14:14:33 +01:00
|
|
|
attrs@{ lib, korora, ... }:
|
2023-12-24 19:19:12 +01:00
|
|
|
let
|
|
|
|
e = import ./korora-lua.nix attrs;
|
2023-12-25 14:14:33 +01:00
|
|
|
k = korora;
|
2023-12-24 19:19:12 +01:00
|
|
|
h = e.helpers;
|
2024-08-31 18:38:31 +02:00
|
|
|
struct =
|
|
|
|
name: props: verify:
|
|
|
|
(k.struct name props).override {
|
|
|
|
total = false;
|
|
|
|
unknown = false;
|
|
|
|
verify = h.mkVerify verify;
|
|
|
|
};
|
2023-12-25 14:14:33 +01:00
|
|
|
|
|
|
|
lazyType = name: mkType: k.typedef' name (v: (mkType true).verify v);
|
|
|
|
|
|
|
|
types = {
|
|
|
|
# {{{ Helper types
|
2024-08-31 18:38:31 +02:00
|
|
|
oneOrMany =
|
|
|
|
type:
|
|
|
|
k.union [
|
|
|
|
type
|
|
|
|
(k.listOf type)
|
|
|
|
];
|
2023-12-25 14:14:33 +01:00
|
|
|
luaValue = k.any;
|
2024-08-31 18:38:31 +02:00
|
|
|
strictLuaLiteral =
|
|
|
|
(k.struct "lua literal" {
|
|
|
|
value = k.string;
|
|
|
|
__luaEncoderTag = k.enum "lua literal tag" [ "lua" ];
|
|
|
|
}).override
|
|
|
|
{ unknown = true; };
|
2023-12-25 14:14:33 +01:00
|
|
|
derivation = k.typedef "derivation" lib.isDerivation;
|
|
|
|
path = k.typedef "path" lib.isPath;
|
2024-08-31 18:38:31 +02:00
|
|
|
functionCheckedWith =
|
|
|
|
arg: type:
|
|
|
|
k.typedef' "${h.toPretty arg} -> ${type.name}" (
|
|
|
|
f:
|
|
|
|
if lib.isFunction f then
|
|
|
|
type.verify (f arg)
|
|
|
|
else
|
|
|
|
"Expected function, but got ${h.toPretty f} instead"
|
|
|
|
);
|
|
|
|
luaEagerOrLazy =
|
|
|
|
type:
|
|
|
|
k.union [
|
|
|
|
type
|
|
|
|
(types.functionCheckedWith "" type)
|
|
|
|
];
|
2023-12-25 14:14:33 +01:00
|
|
|
luaLiteral = types.luaEagerOrLazy types.strictLuaLiteral;
|
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Lazy key
|
|
|
|
lazyKey = struct "lazy key" {
|
|
|
|
mapping = k.string;
|
|
|
|
action = k.union [
|
|
|
|
types.luaLiteral
|
|
|
|
k.string
|
|
|
|
];
|
|
|
|
mode = k.string;
|
|
|
|
desc = k.string;
|
|
|
|
expr = k.bool;
|
|
|
|
ft = types.oneOrMany k.string;
|
|
|
|
} [ (h.propExists "mapping") ];
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Lazy module
|
|
|
|
lazyModule = lazyType "actually lazy lazy module" (
|
|
|
|
_:
|
|
|
|
struct "lazy module"
|
|
|
|
{
|
|
|
|
package = k.string;
|
|
|
|
dir = k.union [
|
|
|
|
k.string
|
|
|
|
types.derivation
|
|
|
|
types.path
|
|
|
|
];
|
|
|
|
version = k.string;
|
|
|
|
tag = k.string;
|
|
|
|
name = k.string;
|
|
|
|
main = k.string;
|
|
|
|
lazy = k.bool;
|
|
|
|
dependencies = struct "lazy dependencies" {
|
|
|
|
lua = k.listOf (
|
|
|
|
k.union [
|
|
|
|
k.string
|
|
|
|
types.lazyModule
|
|
|
|
]
|
|
|
|
);
|
2023-12-25 14:14:33 +01:00
|
|
|
nix = k.listOf types.derivation;
|
2024-08-31 18:38:31 +02:00
|
|
|
} [ ];
|
|
|
|
cond = types.oneOrMany types.luaLiteral;
|
|
|
|
init = types.luaEagerOrLazy (
|
|
|
|
k.union [
|
|
|
|
types.strictLuaLiteral
|
|
|
|
types.strictTempestConfig
|
|
|
|
]
|
|
|
|
);
|
|
|
|
config = k.union [
|
|
|
|
k.bool
|
|
|
|
(types.luaEagerOrLazy (
|
|
|
|
k.union [
|
|
|
|
types.strictLuaLiteral
|
|
|
|
types.strictTempestConfig
|
|
|
|
]
|
|
|
|
))
|
|
|
|
];
|
|
|
|
event = types.oneOrMany k.string;
|
|
|
|
cmd = types.oneOrMany k.string;
|
|
|
|
ft = types.oneOrMany k.string;
|
|
|
|
keys = types.oneOrMany (
|
|
|
|
k.union [
|
|
|
|
k.string
|
|
|
|
types.lazyKey
|
|
|
|
]
|
|
|
|
);
|
|
|
|
passthrough = types.luaValue;
|
|
|
|
opts = types.luaValue;
|
|
|
|
}
|
|
|
|
[
|
|
|
|
(h.propOnlyOne [
|
|
|
|
"dir"
|
|
|
|
"package"
|
|
|
|
])
|
|
|
|
(h.propImplies "tag" "package")
|
|
|
|
(h.propImplies "version" "package")
|
|
|
|
]
|
|
|
|
);
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Tempest key
|
|
|
|
tempestKey =
|
|
|
|
struct "tempest key"
|
|
|
|
{
|
|
|
|
mapping = k.string;
|
|
|
|
action = k.union [
|
|
|
|
types.luaLiteral
|
|
|
|
k.string
|
|
|
|
];
|
|
|
|
mode = k.string;
|
|
|
|
desc = k.string;
|
|
|
|
expr = k.bool;
|
|
|
|
silent = k.bool;
|
|
|
|
ft = types.oneOrMany k.string;
|
|
|
|
buffer = k.union [
|
|
|
|
k.bool
|
|
|
|
k.number
|
|
|
|
types.luaLiteral
|
|
|
|
];
|
|
|
|
}
|
|
|
|
[
|
|
|
|
(h.propExists "mapping")
|
|
|
|
(h.propExists "action")
|
|
|
|
];
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Tempest autocmd
|
|
|
|
tempestAutocmd =
|
|
|
|
struct "tempest autocommand"
|
|
|
|
{
|
|
|
|
event = types.oneOrMany k.string;
|
|
|
|
pattern = types.oneOrMany k.string;
|
|
|
|
group = k.string;
|
|
|
|
action = k.union [
|
|
|
|
types.tempestConfig
|
|
|
|
types.luaLiteral
|
|
|
|
];
|
|
|
|
}
|
|
|
|
[
|
|
|
|
(h.propExists "event")
|
|
|
|
(h.propExists "group")
|
|
|
|
(h.propExists "action")
|
|
|
|
];
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
|
|
|
# {{{ Tempest config
|
2024-08-31 18:38:31 +02:00
|
|
|
strictTempestConfig = struct "tempest config" {
|
|
|
|
vim = types.luaValue;
|
|
|
|
callback = k.union [
|
|
|
|
types.luaLiteral
|
|
|
|
types.tempestConfig
|
|
|
|
];
|
|
|
|
setup = k.attrsOf types.luaValue;
|
|
|
|
keys = types.luaEagerOrLazy (types.oneOrMany types.tempestKey);
|
|
|
|
autocmds = types.luaEagerOrLazy (types.oneOrMany types.tempestAutocmd);
|
|
|
|
mkContext = types.luaValue;
|
|
|
|
cond = types.oneOrMany types.luaLiteral;
|
|
|
|
} [ ];
|
|
|
|
|
|
|
|
tempestConfig = lazyType "lazy tempest config" (_: types.strictTempestConfig);
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Neovim env
|
|
|
|
neovimEnv = k.enum "neovim env" [
|
|
|
|
"neovide"
|
|
|
|
"firenvim"
|
|
|
|
"vscode"
|
|
|
|
];
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Neovim config
|
|
|
|
neovimConfig = struct "neovim configuration" {
|
|
|
|
pre = k.attrsOf types.tempestConfig;
|
|
|
|
lazy = k.attrsOf types.lazyModule;
|
|
|
|
post = k.attrsOf types.tempestConfig;
|
|
|
|
} [ ];
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
2023-12-24 19:19:12 +01:00
|
|
|
};
|
2023-12-25 14:14:33 +01:00
|
|
|
|
2024-08-31 18:38:31 +02:00
|
|
|
mkLib =
|
|
|
|
{ tempestModule }:
|
2024-07-28 20:01:45 +02:00
|
|
|
assert h.hasType k.string tempestModule;
|
2023-12-25 14:14:33 +01:00
|
|
|
rec {
|
|
|
|
inherit (e) encode;
|
2024-07-28 20:01:45 +02:00
|
|
|
inherit (h) lua;
|
|
|
|
|
2024-08-31 18:38:31 +02:00
|
|
|
# {{{ Common generation helpers
|
|
|
|
importFrom =
|
|
|
|
path: tag:
|
2023-12-25 14:14:33 +01:00
|
|
|
assert lib.isPath path;
|
2024-07-28 20:01:45 +02:00
|
|
|
assert h.hasType k.string tag;
|
2023-12-25 14:14:33 +01:00
|
|
|
lua "dofile(${encode (toString path)}).${tag}";
|
2024-08-31 18:38:31 +02:00
|
|
|
foldedList =
|
|
|
|
value:
|
|
|
|
assert h.hasType k.attrs value;
|
|
|
|
{
|
|
|
|
inherit value;
|
|
|
|
__luaEncoderTag = "foldedList";
|
|
|
|
};
|
2023-12-25 14:14:33 +01:00
|
|
|
thunk = code: _: lua code;
|
2024-08-31 18:38:31 +02:00
|
|
|
require = module: lua "require(${module})";
|
|
|
|
tempest =
|
|
|
|
given: context:
|
|
|
|
lua ''
|
|
|
|
D.tempest.configure(
|
2024-07-26 20:18:26 +02:00
|
|
|
${encode given},
|
2024-08-31 18:38:31 +02:00
|
|
|
${context}
|
2024-07-26 20:18:26 +02:00
|
|
|
)
|
2024-08-31 18:38:31 +02:00
|
|
|
'';
|
|
|
|
tempestBufnr =
|
|
|
|
given:
|
|
|
|
lua ''
|
|
|
|
function(_, bufnr)
|
|
|
|
return D.tempest.configure(
|
|
|
|
${encode given},
|
|
|
|
{ bufnr = bufnr}
|
|
|
|
)
|
|
|
|
end
|
|
|
|
'';
|
|
|
|
keymap = mode: mapping: action: desc: {
|
|
|
|
inherit
|
|
|
|
mode
|
|
|
|
mapping
|
|
|
|
action
|
|
|
|
desc
|
|
|
|
;
|
|
|
|
};
|
|
|
|
nmap = mapping: action: desc: { inherit mapping action desc; };
|
|
|
|
unmap = mapping: {
|
|
|
|
inherit mapping;
|
|
|
|
action = "<nop>";
|
|
|
|
};
|
|
|
|
blacklist =
|
|
|
|
given:
|
2024-07-28 20:01:45 +02:00
|
|
|
assert h.hasType (types.oneOrMany types.neovimEnv) given;
|
2024-08-31 18:38:31 +02:00
|
|
|
# lua
|
|
|
|
lua ''
|
2023-12-25 14:14:33 +01:00
|
|
|
D.tempest.blacklist(${encode given})
|
|
|
|
'';
|
2024-08-31 18:38:31 +02:00
|
|
|
whitelist =
|
|
|
|
given:
|
2024-07-28 20:01:45 +02:00
|
|
|
assert h.hasType (types.oneOrMany types.neovimEnv) given;
|
2024-08-31 18:38:31 +02:00
|
|
|
# lua
|
|
|
|
lua ''
|
2023-12-25 14:14:33 +01:00
|
|
|
D.tempest.whitelist(${encode given})
|
|
|
|
'';
|
2024-04-16 23:25:20 +02:00
|
|
|
# :p => expands path
|
|
|
|
# :h => returns the head of the path
|
|
|
|
notmp = lua ''vim.fn.expand("%:p:h") ~= "/tmp"'';
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
|
|
|
# {{{ Main config generation entrypoint
|
2024-08-31 18:38:31 +02:00
|
|
|
generateConfig =
|
|
|
|
rawConfig:
|
2024-07-28 20:01:45 +02:00
|
|
|
assert h.hasType types.neovimConfig rawConfig;
|
2023-12-25 14:14:33 +01:00
|
|
|
let
|
2024-08-31 18:38:31 +02:00
|
|
|
config = {
|
|
|
|
lazy = { };
|
|
|
|
pre = { };
|
|
|
|
post = { };
|
|
|
|
} // rawConfig;
|
|
|
|
collectNixDeps =
|
|
|
|
lazyModule:
|
2023-12-25 14:14:33 +01:00
|
|
|
if lazyModule ? dependencies then
|
|
|
|
let
|
|
|
|
nix = lazyModule.dependencies.nix or [ ];
|
|
|
|
other = lib.pipe (lazyModule.dependencies.lua or [ ]) [
|
|
|
|
(lib.lists.map collectNixDeps)
|
|
|
|
lib.lists.flatten
|
|
|
|
];
|
|
|
|
in
|
|
|
|
nix ++ other
|
|
|
|
else
|
|
|
|
[ ];
|
|
|
|
dependencies = lib.pipe config.lazy [
|
|
|
|
(lib.mapAttrsToList (_: collectNixDeps))
|
|
|
|
lib.lists.flatten
|
|
|
|
];
|
2024-08-31 18:38:31 +02:00
|
|
|
processedLazyModules = lib.mapAttrs (
|
|
|
|
name: module:
|
|
|
|
{ inherit name; } // module // { dependencies = (module.dependencies or { }).lua or null; }
|
|
|
|
) config.lazy;
|
2023-12-25 14:14:33 +01:00
|
|
|
|
|
|
|
luaConfig = ''
|
|
|
|
local M = {}
|
|
|
|
local D = {
|
|
|
|
tempest = require(${encode tempestModule}),
|
|
|
|
}
|
|
|
|
|
|
|
|
-- {{{ Pre-plugin config
|
|
|
|
M.pre = ${encode (foldedList config.pre)}
|
|
|
|
-- }}}
|
2024-08-31 18:38:31 +02:00
|
|
|
-- {{{ Lazy modules
|
2023-12-25 14:14:33 +01:00
|
|
|
M.lazy = ${encode (foldedList processedLazyModules)}
|
|
|
|
D.tempest.prepareLazySpec(M.lazy)
|
|
|
|
-- }}}
|
|
|
|
-- {{{ Post-plugin config
|
|
|
|
M.post = ${encode (foldedList config.post)}
|
|
|
|
-- }}}
|
|
|
|
|
|
|
|
return M
|
|
|
|
'';
|
|
|
|
in
|
2024-08-31 18:38:31 +02:00
|
|
|
{
|
|
|
|
inherit dependencies;
|
|
|
|
lua = luaConfig;
|
|
|
|
};
|
2023-12-25 14:14:33 +01:00
|
|
|
# }}}
|
|
|
|
};
|
2023-12-24 19:19:12 +01:00
|
|
|
in
|
2023-12-25 14:14:33 +01:00
|
|
|
mkLib
|