Skip to content

Commit 5e3560c

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 5e3560c

File tree

4 files changed

+148
-37
lines changed

4 files changed

+148
-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 with a newline between each element
12+
# skipping strings containing only whitespace characters
13+
concatNonEmptyLines = list: builtins.concatStringsSep "\n" (builtins.filter hasContent list);
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

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

0 commit comments

Comments
 (0)