nix-community.nixvim/modules/opts.nix
Stanislav Asunkin 9317537848 modules: avoid setting empty strings to extraConfig* options
Problem:  Some modules are setting empty strings to extraConfig* options
          with the intention to not generate any config. But empty
          strings are also values, so they are still concatenated in the
          final value of extraConfig* options. This results in a
          multiple empty strings in extraConfigs.

Solution: Avoid using optionalString when setting values to extraConfig*
          options. Use mkIf instead.

          This commit also fixes mkIf condition in autocmd module.

          `mkNeovimPlugin` is a special case. To avoid evaluating
          caller's arguments mkMerge/optionalAttrs pattern is used
          instead.
2024-07-22 23:18:53 +02:00

89 lines
2.1 KiB
Nix

{
lib,
helpers,
config,
...
}:
with lib;
let
optionsAttrs = {
opts = {
prettyName = "options";
luaVariableName = "options";
luaApi = "opt";
description = "The configuration options, e.g. line numbers (`vim.opt.*`)";
};
globalOpts = {
prettyName = "global options";
luaVariableName = "global_options";
luaApi = "opt_global";
description = "The configuration global options (`vim.opt_global.*`)";
};
localOpts = {
prettyName = "local options";
luaVariableName = "local_options";
luaApi = "opt_local";
description = "The configuration local options (`vim.opt_local.*`)";
};
globals = {
prettyName = "globals";
luaVariableName = "globals";
luaApi = "g";
description = "Global variables (`vim.g.*`)";
};
};
in
{
options = mapAttrs (
_:
{ description, ... }:
mkOption {
type = with types; attrsOf anything;
default = { };
inherit description;
}
) optionsAttrs;
# Added 2024-03-29 (do not remove)
imports = mapAttrsToList (old: new: mkRenamedOptionModule [ old ] [ new ]) {
options = "opts";
globalOptions = "globalOpts";
localOptions = "localOpts";
};
config = {
extraConfigLuaPre =
let
content = helpers.concatNonEmptyLines (
mapAttrsToList (
optionName:
{
prettyName,
luaVariableName,
luaApi,
...
}:
let
varName = "nixvim_${luaVariableName}";
optionDefinitions = config.${optionName};
in
optionalString (optionDefinitions != { }) ''
-- Set up ${prettyName} {{{
do
local ${varName} = ${helpers.toLuaObject optionDefinitions}
for k,v in pairs(${varName}) do
vim.${luaApi}[k] = v
end
end
-- }}}
''
) optionsAttrs
);
in
mkIf (content != "") content;
};
}