modules/output: refactor config generation

The motivation for this change was to avoid generating empty
config sections like

    vim.cmd([[

    ]])

To make a config generation cleaner several helper functions introduced:

* `hasContent` have been moved to helpers
* `concatNonEmptyLines` joins strings (which has content) separated with
  newlines
* `wrapVimscriptForLua` wraps a lua string for using in Vimscript, but
  only if the string has content, otherwise empty string is returned
* `wrapLuaForVimscript` wraps Vimscript for using in lua, but only if
  the string has content, otherwise empty string is returned

Added tests:

* testing that all possible config sections are present in the final
  generated config
* testing that the config files generated by empty `files` definitions
  don't have any content in it
This commit is contained in:
Stanislav Asunkin 2024-07-21 11:55:52 +03:00 committed by Gaétan Lepage
parent 6dc0bda459
commit 299d0406bb
4 changed files with 149 additions and 37 deletions

View file

@ -1,4 +1,9 @@
{ lib, config, ... }:
{
lib,
config,
helpers,
...
}:
with lib;
let
pluginWithConfigType = types.submodule {
@ -97,29 +102,27 @@ in
};
};
config =
let
contentLua = ''
${config.extraConfigLuaPre}
vim.cmd([[
${config.extraConfigVim}
]])
${config.extraConfigLua}
${config.extraConfigLuaPost}
'';
contentVim = ''
lua << EOF
${config.extraConfigLuaPre}
EOF
${config.extraConfigVim}
lua << EOF
${config.extraConfigLua}
${config.extraConfigLuaPost}
EOF
'';
in
{
content = if config.type == "lua" then contentLua else contentVim;
};
config = {
content =
if config.type == "lua" then
# Lua
helpers.concatNonEmptyLines [
config.extraConfigLuaPre
(helpers.wrapVimscriptForLua config.extraConfigVim)
config.extraConfigLua
config.extraConfigLuaPost
]
else
# Vimscript
helpers.concatNonEmptyLines [
(helpers.wrapLuaForVimscript config.extraConfigLuaPre)
config.extraConfigVim
(helpers.wrapLuaForVimscript (
helpers.concatNonEmptyLines [
config.extraConfigLua
config.extraConfigLuaPost
]
))
];
};
}