Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

modules/output: produce empty files by default #1909

Merged
merged 2 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -110,11 +110,12 @@ with lib;
{
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;

${extraConfigNamespace} = optionalString callSetup ''
}
(optionalAttrs callSetup {
${extraConfigNamespace} = ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';
}
})
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
]);
Expand Down
31 changes: 30 additions & 1 deletion lib/utils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
_nixvimTests,
}:
with lib;
{
rec {
# Whether a string contains something other than whitespaces
hasContent = str: builtins.match "[[:space:]]*" str == null;

# Concatenate a list of strings, adding a newline at the end of each one,
# but skipping strings containing only whitespace characters
concatNonEmptyLines = lines: concatLines (builtins.filter hasContent lines);

listToUnkeyedAttrs =
list:
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
Expand Down Expand Up @@ -118,4 +125,26 @@ with lib;
${string}
end
'';

# Wrap Vimscript for using in lua,
# but only if the string contains something other than whitespaces
# TODO: account for a possible ']]' in the string
wrapVimscriptForLua =
string:
optionalString (hasContent string) ''
vim.cmd([[
${string}
]])
stasjok marked this conversation as resolved.
Show resolved Hide resolved
'';

# Wrap lua script for using in Vimscript,
# but only if the string contains something other than whitespaces
# TODO: account for a possible 'EOF' if the string
wrapLuaForVimscript =
string:
optionalString (hasContent string) ''
lua << EOF
${string}
EOF
stasjok marked this conversation as resolved.
Show resolved Hide resolved
'';
}
2 changes: 1 addition & 1 deletion modules/autocmd.nix
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ with lib;
let
inherit (config) autoGroups autoCmd;
in
mkIf (autoGroups != { } || autoCmd != { }) {
mkIf (autoGroups != { } || autoCmd != [ ]) {
stasjok marked this conversation as resolved.
Show resolved Hide resolved
# Introduced early October 2023.
# TODO remove in early December 2023.
assertions = [
Expand Down
2 changes: 1 addition & 1 deletion modules/diagnostics.nix
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ with lib;
};

config = {
extraConfigLuaPre = optionalString (config.diagnostics != { }) ''
extraConfigLuaPre = mkIf (config.diagnostics != { }) ''
vim.diagnostic.config(${helpers.toLuaObject config.diagnostics})
'';
};
Expand Down
80 changes: 41 additions & 39 deletions modules/highlights.nix
Original file line number Diff line number Diff line change
Expand Up @@ -35,50 +35,52 @@ with lib;
};
};

config = {
extraConfigLuaPre =
(optionalString (config.highlight != { })
# lua
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlight}
config = mkMerge [
{
extraConfigLuaPre =
mkIf (config.highlight != { })
stasjok marked this conversation as resolved.
Show resolved Hide resolved
# lua
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlight}

for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v)
for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v)
end
end
end
-- }}
''
)
+ (optionalString (config.match != { })
# lua
''
-- Match groups {{
do
local match = ${helpers.toLuaObject config.match}
-- }}
'';
extraConfigLuaPost =
mkIf (config.highlightOverride != { })
# lua
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlightOverride}

for k,v in pairs(match) do
vim.fn.matchadd(k, v)
for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v)
end
end
end
-- }}
''
);

extraConfigLuaPost =
optionalString (config.highlightOverride != { })
# lua
''
-- Highlight groups {{
do
local highlights = ${helpers.toLuaObject config.highlightOverride}
'';
}
{
extraConfigLuaPre =
mkIf (config.match != { })
# lua
''
-- Match groups {{
do
local match = ${helpers.toLuaObject config.match}

for k,v in pairs(highlights) do
vim.api.nvim_set_hl(0, k, v)
for k,v in pairs(match) do
vim.fn.matchadd(k, v)
end
end
end
-- }}
'';
};
-- }}
'';
}
];
}
2 changes: 1 addition & 1 deletion modules/keymaps.nix
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ with lib;
${concatStringsSep "\n" luaDefs}
'';

extraConfigLua = optionalString (config.keymaps != [ ]) ''
extraConfigLua = mkIf (config.keymaps != [ ]) ''
-- Set up keybinds {{{
do
local __nixvim_binds = ${helpers.toLuaObject (map normalizeMapping config.keymaps)}
Expand Down
54 changes: 29 additions & 25 deletions modules/opts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,31 +55,35 @@ in
};

config = {
extraConfigLuaPre = concatLines (
mapAttrsToList (
optionName:
{
prettyName,
luaVariableName,
luaApi,
...
}:
let
varName = "nixvim_${luaVariableName}";
optionDefinitions = config.${optionName};
in
optionalString (optionDefinitions != { }) ''
-- Set up ${prettyName} {{{
do
local ${varName} = ${helpers.toLuaObject optionDefinitions}
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
);
for k,v in pairs(${varName}) do
vim.${luaApi}[k] = v
end
end
-- }}}
''
) optionsAttrs
);
in
mkIf (content != "") content;
};
}
55 changes: 29 additions & 26 deletions modules/output.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
{ lib, config, ... }:
{
lib,
config,
helpers,
...
}:
with lib;
let
pluginWithConfigType = types.submodule {
Expand Down Expand Up @@ -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
]
))
];
};
}
16 changes: 5 additions & 11 deletions modules/top-level/output.nix
Original file line number Diff line number Diff line change
Expand Up @@ -107,16 +107,10 @@ with lib;
}
);

customRC =
let
hasContent = str: (builtins.match "[[:space:]]*" str) == null;
in
(optionalString (hasContent neovimConfig.neovimRcContent) ''
vim.cmd([[
${neovimConfig.neovimRcContent}
]])
'')
+ config.content;
customRC = helpers.concatNonEmptyLines [
(helpers.wrapVimscriptForLua neovimConfig.neovimRcContent)
config.content
];
stasjok marked this conversation as resolved.
Show resolved Hide resolved

init = helpers.writeLua "init.lua" customRC;

Expand Down Expand Up @@ -146,7 +140,7 @@ with lib;
'';
};

extraConfigLuaPre = lib.optionalString config.wrapRc ''
extraConfigLuaPre = lib.mkIf config.wrapRc ''
stasjok marked this conversation as resolved.
Show resolved Hide resolved
-- Ignore the user lua configuration
vim.opt.runtimepath:remove(vim.fn.stdpath('config')) -- ~/.config/nvim
vim.opt.runtimepath:remove(vim.fn.stdpath('config') .. "/after") -- ~/.config/nvim/after
Expand Down
Loading