Skip to content

Commit b593841

Browse files
committed
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
1 parent 34aa3e0 commit b593841

File tree

4 files changed

+149
-37
lines changed

4 files changed

+149
-37
lines changed

lib/utils.nix

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,14 @@
44
_nixvimTests,
55
}:
66
with lib;
7-
{
7+
rec {
8+
# Whether a string contains something other than whitespaces
9+
hasContent = str: builtins.match "[[:space:]]*" str == null;
10+
11+
# Concatenate a list of strings, adding a newline at the end of each one,
12+
# but skipping strings containing only whitespace characters
13+
concatNonEmptyLines = lines: concatLines (builtins.filter hasContent lines);
14+
815
listToUnkeyedAttrs =
916
list:
1017
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
@@ -118,4 +125,26 @@ with lib;
118125
${string}
119126
end
120127
'';
128+
129+
# Wrap Vimscript for using in lua,
130+
# but only if the string contains something other than whitespaces
131+
# TODO: account for a possible ']]' in the string
132+
wrapVimscriptForLua =
133+
string:
134+
optionalString (hasContent string) ''
135+
vim.cmd([[
136+
${string}
137+
]])
138+
'';
139+
140+
# Wrap lua script for using in Vimscript,
141+
# but only if the string contains something other than whitespaces
142+
# TODO: account for a possible 'EOF' if the string
143+
wrapLuaForVimscript =
144+
string:
145+
optionalString (hasContent string) ''
146+
lua << EOF
147+
${string}
148+
EOF
149+
'';
121150
}

modules/output.nix

Lines changed: 29 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,9 @@
1-
{ lib, config, ... }:
1+
{
2+
lib,
3+
config,
4+
helpers,
5+
...
6+
}:
27
with lib;
38
let
49
pluginWithConfigType = types.submodule {
@@ -97,29 +102,27 @@ in
97102
};
98103
};
99104

100-
config =
101-
let
102-
contentLua = ''
103-
${config.extraConfigLuaPre}
104-
vim.cmd([[
105-
${config.extraConfigVim}
106-
]])
107-
${config.extraConfigLua}
108-
${config.extraConfigLuaPost}
109-
'';
110-
111-
contentVim = ''
112-
lua << EOF
113-
${config.extraConfigLuaPre}
114-
EOF
115-
${config.extraConfigVim}
116-
lua << EOF
117-
${config.extraConfigLua}
118-
${config.extraConfigLuaPost}
119-
EOF
120-
'';
121-
in
122-
{
123-
content = if config.type == "lua" then contentLua else contentVim;
124-
};
105+
config = {
106+
content =
107+
if config.type == "lua" then
108+
# Lua
109+
helpers.concatNonEmptyLines [
110+
config.extraConfigLuaPre
111+
(helpers.wrapVimscriptForLua config.extraConfigVim)
112+
config.extraConfigLua
113+
config.extraConfigLuaPost
114+
]
115+
else
116+
# Vimscript
117+
helpers.concatNonEmptyLines [
118+
(helpers.wrapLuaForVimscript config.extraConfigLuaPre)
119+
config.extraConfigVim
120+
(helpers.wrapLuaForVimscript (
121+
helpers.concatNonEmptyLines [
122+
config.extraConfigLua
123+
config.extraConfigLuaPost
124+
]
125+
))
126+
];
127+
};
125128
}

modules/top-level/output.nix

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -107,16 +107,10 @@ with lib;
107107
}
108108
);
109109

110-
customRC =
111-
let
112-
hasContent = str: (builtins.match "[[:space:]]*" str) == null;
113-
in
114-
(optionalString (hasContent neovimConfig.neovimRcContent) ''
115-
vim.cmd([[
116-
${neovimConfig.neovimRcContent}
117-
]])
118-
'')
119-
+ config.content;
110+
customRC = helpers.concatNonEmptyLines [
111+
(helpers.wrapVimscriptForLua neovimConfig.neovimRcContent)
112+
config.content
113+
];
120114

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

tests/test-sources/modules/output.nix

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,90 @@
44
# Make sure jsregexp is in LUA_PATH
55
extraConfigLua = ''require("jsregexp")'';
66
};
7+
8+
# Test that all extraConfigs are present in output
9+
all-configs.module =
10+
{
11+
config,
12+
pkgs,
13+
lib,
14+
...
15+
}:
16+
let
17+
configs = {
18+
extraConfigLuaPre = "string.format('extraConfigLuaPre1')";
19+
extraConfigLua = "string.format('extraConfigLua2')";
20+
extraConfigLuaPost = "string.format('extraConfigLuaPost3')";
21+
extraConfigVim = "let g:var = 'extraConfigVim4'";
22+
};
23+
mkConfigAssertions = name: value: [
24+
{
25+
assertion = lib.hasInfix "extraConfigLuaPre1" value;
26+
message = "Configuration file ${name} does not contain extraConfigLuaPre.";
27+
}
28+
{
29+
assertion = lib.hasInfix "extraConfigLua2" value;
30+
message = "Configuration file ${name} does not contain extraConfigLua.";
31+
}
32+
{
33+
assertion = lib.hasInfix "extraConfigLuaPost3" value;
34+
message = "Configuration file ${name} does not contain extraConfigLuaPost.";
35+
}
36+
{
37+
assertion = lib.hasInfix "extraConfigVim4" value;
38+
message = "Configuration file ${name} does not contain extraConfigVim.";
39+
}
40+
];
41+
in
42+
configs
43+
// {
44+
files = {
45+
"test.lua" = configs;
46+
"test.vim" = configs;
47+
};
48+
49+
# Plugin configs
50+
extraPlugins = [
51+
{
52+
plugin = pkgs.emptyDirectory;
53+
config = "let g:var = 'neovimRcContent5'";
54+
}
55+
];
56+
57+
assertions =
58+
mkConfigAssertions "init.lua" config.content
59+
++ mkConfigAssertions "test.lua" config.files."test.lua".content
60+
++ mkConfigAssertions "test.vim" config.files."test.vim".content
61+
# Check the final generated init.lua too
62+
++ mkConfigAssertions "initPath" (builtins.readFile config.initPath)
63+
++ [
64+
# Only init.lua contains configuration from plugin definitions
65+
{
66+
assertion = lib.hasInfix "neovimRcContent5" (builtins.readFile config.initPath);
67+
message = "Configuration file init.lua does not contain plugin configs";
68+
}
69+
];
70+
};
71+
72+
files-default-empty.module =
73+
{ config, helpers, ... }:
74+
{
75+
files = {
76+
# lua type
77+
"test.lua" = { };
78+
# vim type
79+
"test.vim" = { };
80+
};
81+
82+
assertions = [
83+
{
84+
assertion = !helpers.hasContent config.files."test.lua".content;
85+
message = "Default content of test.lua file is expected to be empty.";
86+
}
87+
{
88+
assertion = !helpers.hasContent config.files."test.vim".content;
89+
message = "Default content of test.vim file is expected to be empty.";
90+
}
91+
];
92+
};
793
}

0 commit comments

Comments
 (0)