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.
This commit is contained in:
Stanislav Asunkin 2024-07-21 20:59:16 +03:00 committed by Gaétan Lepage
parent 299d0406bb
commit 9317537848
7 changed files with 80 additions and 73 deletions

View file

@ -110,11 +110,12 @@ with lib;
{ {
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins; extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages; inherit extraPackages;
}
${extraConfigNamespace} = optionalString callSetup '' (optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)}) require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
''; '';
} })
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; }) (optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg) (extraConfig cfg)
]); ]);

View file

@ -42,7 +42,7 @@ with lib;
let let
inherit (config) autoGroups autoCmd; inherit (config) autoGroups autoCmd;
in in
mkIf (autoGroups != { } || autoCmd != { }) { mkIf (autoGroups != { } || autoCmd != [ ]) {
# Introduced early October 2023. # Introduced early October 2023.
# TODO remove in early December 2023. # TODO remove in early December 2023.
assertions = [ assertions = [

View file

@ -19,7 +19,7 @@ with lib;
}; };
config = { config = {
extraConfigLuaPre = optionalString (config.diagnostics != { }) '' extraConfigLuaPre = mkIf (config.diagnostics != { }) ''
vim.diagnostic.config(${helpers.toLuaObject config.diagnostics}) vim.diagnostic.config(${helpers.toLuaObject config.diagnostics})
''; '';
}; };

View file

@ -35,50 +35,52 @@ with lib;
}; };
}; };
config = { config = mkMerge [
extraConfigLuaPre = {
(optionalString (config.highlight != { }) extraConfigLuaPre =
# lua mkIf (config.highlight != { })
'' # lua
-- Highlight groups {{ ''
do -- Highlight groups {{
local highlights = ${helpers.toLuaObject config.highlight} do
local highlights = ${helpers.toLuaObject config.highlight}
for k,v in pairs(highlights) do for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v) vim.api.nvim_set_hl(0, k, v)
end
end end
end
-- }}
''
)
+ (optionalString (config.match != { })
# lua
''
-- Match groups {{
do
local match = ${helpers.toLuaObject config.match}
for k,v in pairs(match) do
vim.fn.matchadd(k, v)
end
end
-- }} -- }}
'' '';
); extraConfigLuaPost =
mkIf (config.highlightOverride != { })
# lua
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlightOverride}
extraConfigLuaPost = for k,v in pairs(highlights) do
optionalString (config.highlightOverride != { }) vim.api.nvim_set_hl(0, k, v)
# lua end
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlightOverride}
for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v)
end end
end -- }}
-- }} '';
''; }
}; {
extraConfigLuaPre =
mkIf (config.match != { })
# lua
''
-- Match groups {{
do
local match = ${helpers.toLuaObject config.match}
for k,v in pairs(match) do
vim.fn.matchadd(k, v)
end
end
-- }}
'';
}
];
} }

View file

@ -90,7 +90,7 @@ with lib;
${concatStringsSep "\n" luaDefs} ${concatStringsSep "\n" luaDefs}
''; '';
extraConfigLua = optionalString (config.keymaps != [ ]) '' extraConfigLua = mkIf (config.keymaps != [ ]) ''
-- Set up keybinds {{{ -- Set up keybinds {{{
do do
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)} local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)}

View file

@ -55,31 +55,35 @@ in
}; };
config = { config = {
extraConfigLuaPre = concatLines ( extraConfigLuaPre =
mapAttrsToList ( let
optionName: content = helpers.concatNonEmptyLines (
{ mapAttrsToList (
prettyName, optionName:
luaVariableName, {
luaApi, prettyName,
... luaVariableName,
}: luaApi,
let ...
varName = "nixvim_${luaVariableName}"; }:
optionDefinitions = config.${optionName}; let
in varName = "nixvim_${luaVariableName}";
optionalString (optionDefinitions != { }) '' optionDefinitions = config.${optionName};
-- Set up ${prettyName} {{{ in
do optionalString (optionDefinitions != { }) ''
local ${varName} = ${helpers.toLuaObject optionDefinitions} -- Set up ${prettyName} {{{
do
local ${varName} = ${helpers.toLuaObject optionDefinitions}
for k,v in pairs(${varName}) do for k,v in pairs(${varName}) do
vim.${luaApi}[k] = v vim.${luaApi}[k] = v
end end
end end
-- }}} -- }}}
'' ''
) optionsAttrs ) optionsAttrs
); );
in
mkIf (content != "") content;
}; };
} }

View file

@ -140,7 +140,7 @@ with lib;
''; '';
}; };
extraConfigLuaPre = lib.optionalString config.wrapRc '' extraConfigLuaPre = lib.mkIf config.wrapRc ''
-- Ignore the user lua configuration -- Ignore the user lua configuration
vim.opt.runtimepath:remove(vim.fn.stdpath('config')) -- ~/.config/nvim vim.opt.runtimepath:remove(vim.fn.stdpath('config')) -- ~/.config/nvim
vim.opt.runtimepath:remove(vim.fn.stdpath('config') .. "/after") -- ~/.config/nvim/after vim.opt.runtimepath:remove(vim.fn.stdpath('config') .. "/after") -- ~/.config/nvim/after