nixvim
nixvim copied to clipboard
Feature: parsed lua function and code
Description
I would like to have my lua code for my configs parsed by nixvim and report errors if I made typos or other mistakes.
The idea is that we often use rawTypes to write custom logic for our configs, but sometimes you might write mistakes, and there isn't an lsp that can help me here since it is a nix string. This would then lead to me finding out in runtime that I made an error in my config and then having to fix it and rebuild. This can be a somewhat annoying process to deal with and would be great to catch in build time.
By doing this we can also check other things, such as if an options should be a function or a single node in the abstract synax tree.
Possible solution / proposal
I think we should keep the rawType just the way it is and still let users be able to write raw code if they want to.
helpers.mkRawSafe function
This function should do the same as mkRaw, but also parse the string and check for errors at build time. If there are any errors the build will fail.
Behind the scenes we it would create an attrset like { __raw = "..."; __rawOptions = { parse = true; }; }. I guess this can be checked by toLuaObject and raise/throw an error if the parse was not possible.
helpers.luaFunctionType type and mkLuaFunction function
the luaFunctionType type would accept rawType too to be compliant with the previous statement, but would look something like this
{
__raw = "...";
__rawOptions = {
parse = true
parseSingleValue = true;
parseValueTypes = [ "function" ];
};
}
The luaFunctionType would then only accept "function" in the parseValueTypes.
luaSingleValueType type and mkLuaValue function
This should be very similar to mkLuaFunction and luaFunctionType, but has parseValueTypes = [ "all" ]
Closing thoughts
This is of course just a rough draft, I am not sure the naming is the best, but I think it gives the gist. I would like to hear the opinions of other contributors/maintainers.