1
Fork 0

Some neovim changes I forgot about

This commit is contained in:
prescientmoon 2024-09-11 15:59:50 +02:00
parent 2eb3151562
commit 7c7e067c1a
Signed by: prescientmoon
SSH key fingerprint: SHA256:WFp/cO76nbarETAoQcQXuV+0h7XJsEsOCI0UsyPIy6U
5 changed files with 213 additions and 195 deletions

View file

@ -2,69 +2,89 @@
let
k = korora;
# {{{ Helpers
# {{{ Helpers
helpers = rec {
removeEmptyLines = str:
removeEmptyLines =
str:
lib.pipe str [
(lib.splitString "\n")
(lib.lists.filter
(line: lib.any
(c: c != " ")
(lib.stringToCharacters line)))
(lib.lists.filter (line: lib.any (c: c != " ") (lib.stringToCharacters line)))
(lib.concatStringsSep "\n")
];
hasType = type: value:
let err = type.verify value; in
hasType =
type: value:
let
err = type.verify value;
in
lib.assertMsg (err == null) err;
lua = value: assert hasType k.string value;
{
inherit value;
lua =
value:
assert hasType k.string value;
lib.fix (self: {
inherit value;
__luaEncoderTag = "lua";
__functor = _: arg: lua "(${value})(${encode arg})";
};
__functor =
_: arg:
if builtins.typeOf arg == "path" then
if arg == /. then
self
else
let
object = self (builtins.dirOf arg);
attr = builtins.toString (builtins.baseNameOf arg);
in
lua "${encode object}.${attr}"
else
lua "${value}(${encode arg})";
});
# {{{ Verification helpers
# {{{ Verification helpers
toPretty = lib.generators.toPretty { indent = " "; };
withError = cond: err: if cond then null else err;
hasProp = obj: p: obj ? ${p};
propExists = p: obj:
helpers.withError
(helpers.hasProp obj p)
"Expected property ${p}";
propXor = a: b: obj:
helpers.withError
((hasProp obj a) != (hasProp obj b))
"Expected only one of the properties ${a} and ${b} in object ${toPretty obj}";
propOnlyOne = props: obj:
helpers.withError
(1 == lib.count (hasProp obj) props)
"Expected exactly one property from the list ${toPretty props} in object ${toPretty obj}";
propImplies = a: b: obj:
helpers.withError
((hasProp obj a) -> (hasProp obj b))
"Property ${a} should imply property ${b} in object ${toPretty obj}";
mkVerify = checks: obj:
propExists = p: obj: helpers.withError (helpers.hasProp obj p) "Expected property ${p}";
propXor =
a: b: obj:
helpers.withError (
(hasProp obj a) != (hasProp obj b)
) "Expected only one of the properties ${a} and ${b} in object ${toPretty obj}";
propOnlyOne =
props: obj:
helpers.withError (
1 == lib.count (hasProp obj) props
) "Expected exactly one property from the list ${toPretty props} in object ${toPretty obj}";
propImplies =
a: b: obj:
helpers.withError (
(hasProp obj a) -> (hasProp obj b)
) "Property ${a} should imply property ${b} in object ${toPretty obj}";
mkVerify =
checks: obj:
assert lib.all lib.isFunction checks;
let
results = lib.lists.map (c: c obj) checks;
errors = lib.lists.filter (v: v != null) results;
in
assert lib.all lib.isString errors;
if errors == [ ]
then null
if errors == [ ] then
null
else
let prettyErrors =
lib.lists.map (s: "\n- ${s}") errors;
let
prettyErrors = lib.lists.map (s: "\n- ${s}") errors;
in
"Multiple errors occured: ${lib.concatStrings prettyErrors}";
# }}}
# {{{ Custom types
intersection = l: r:
k.typedef'
"${l.name} ${r.name}"
(helpers.mkVerify [ l r ]);
intersection =
l: r:
k.typedef' "${l.name} ${r.name}" (
helpers.mkVerify [
l
r
]
);
# dependentAttrsOf =
# name: mkType:
@ -85,36 +105,54 @@ let
# v));
# }}}
# {{{ Encoding helpers
mkRawLuaObject = chunks:
''
{
${lib.concatStringsSep "," (lib.filter (s: s != "") chunks)}
}
'';
mkRawLuaObject = chunks: ''
{
${lib.concatStringsSep "," (lib.filter (s: s != "") chunks)}
}
'';
encodeString = given:
encodeString =
given:
if lib.hasInfix "\n" given then
''[[${(toString given)}]]'' # This is not perfect but oh well
else
''"${lib.escape ["\"" "\\"] (toString given)}"'';
''"${
lib.escape [
"\""
"\\"
] (toString given)
}"'';
mkAttrName = s:
mkAttrName =
s:
let
# A "good enough" approach to figuring out the correct encoding for attribute names
allowedStartingChars = lib.stringToCharacters "abcdefghijklmnopqrstuvwxyz_";
allowedChars = allowedStartingChars ++ lib.stringToCharacters "0123456789";
keywords = [ "if" "then" "else" "do" "for" "local" "" ];
keywords = [
"if"
"then"
"else"
"do"
"for"
"local"
""
];
in
if builtins.match "([a-zA-Z_][a-zA-Z_0-9]*)" s != [ s ] || lib.elem s keywords then
"[${helpers.encodeString s}]"
else s;
else
s;
# }}}
};
# }}}
# {{{ Encoding
isLuaLiteral = given: lib.isAttrs given && given.__luaEncoderTag or null == "lua";
encodeWithDepth = depth: given:
let recurse = encodeWithDepth depth; in
encodeWithDepth =
depth: given:
let
recurse = encodeWithDepth depth;
in
if lib.isString given || lib.isDerivation given || lib.isPath given then
helpers.encodeString given
else if lib.isInt given || lib.isFloat given then
@ -140,40 +178,43 @@ let
else if isLuaLiteral given then
given.value
else if lib.isAttrs given then
helpers.mkRawLuaObject
(lib.mapAttrsToList
(name: value:
let result = recurse value;
in
lib.optionalString (result != "nil")
"${helpers.mkAttrName name} = ${result}"
)
given)
helpers.mkRawLuaObject (
lib.mapAttrsToList (
name: value:
let
result = recurse value;
in
lib.optionalString (result != "nil") "${helpers.mkAttrName name} = ${result}"
) given
)
else if lib.isFunction given then
let
argNames = [ "context" "inner_context" "local_context" ];
argNames = [
"context"
"inner_context"
"local_context"
];
secretArg =
if depth >= builtins.length argNames
then "arg_${depth}"
else builtins.elemAt argNames depth;
if depth >= builtins.length argNames then "arg_${depth}" else builtins.elemAt argNames depth;
body = given secretArg;
encoded = encodeWithDepth (depth + 1) body;
encodedBody =
if isLuaLiteral body
then encoded
else "return ${encoded}";
encodedBody = if isLuaLiteral body then encoded else "return ${encoded}";
in
if lib.hasInfix secretArg encoded
then ''
function(${secretArg})
${encodedBody}
end''
else ''
function()
${encodedBody}
end''
else builtins.throw "Cannot encode value ${helpers.toPretty given}";
if lib.hasInfix secretArg encoded then
''
function(${secretArg})
${encodedBody}
end''
else
''
function()
${encodedBody}
end''
else
builtins.throw "Cannot encode value ${helpers.toPretty given}";
# }}}
encode = encodeWithDepth 0;
in
{ inherit encode helpers; }
{
inherit encode helpers;
}

View file

@ -39,11 +39,12 @@ let
else
"Expected function, but got ${h.toPretty f} instead"
);
lazy = types.functionCheckedWith "";
luaEagerOrLazy =
type:
k.union [
type
(types.functionCheckedWith "" type)
(types.lazy type)
];
luaLiteral = types.luaEagerOrLazy types.strictLuaLiteral;
# }}}
@ -128,10 +129,7 @@ let
struct "tempest key"
{
mapping = k.string;
action = k.union [
types.luaLiteral
k.string
];
action = types.luaValue;
mode = k.string;
desc = k.string;
expr = k.bool;
@ -177,7 +175,7 @@ let
keys = types.luaEagerOrLazy (types.oneOrMany types.tempestKey);
autocmds = types.luaEagerOrLazy (types.oneOrMany types.tempestAutocmd);
mkContext = types.luaValue;
cond = types.oneOrMany types.luaLiteral;
cond = types.oneOrMany types.luaValue;
} [ ];
tempestConfig = lazyType "lazy tempest config" (_: types.strictTempestConfig);
@ -219,8 +217,18 @@ let
__luaEncoderTag = "foldedList";
};
thunk = code: _: lua code;
require = module: lua "require(${module})";
tempest =
return = code: lua "return ${encode code}";
vim = lua "vim";
print = lua "print";
require = lua "require";
tempest = lua "D.tempest";
do = actions: lua (lib.concatStringsSep "\n" (lib.forEach actions encode));
args = values: lua (lib.concatStringsSep ", " (lib.forEach values encode));
none = lua "";
tempestConfigure =
given: context:
lua ''
D.tempest.configure(