Skip to content

Commit 2bd8f19

Browse files
committed
modules/output: produce empty files by default
1 parent 8eb5763 commit 2bd8f19

File tree

4 files changed

+139
-30
lines changed

4 files changed

+139
-30
lines changed

lib/utils.nix

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,15 @@
44
_nixvimTests,
55
}:
66
with lib;
7-
{
7+
rec {
8+
# Whether a string contains something other than whitespace
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+
concatNonEmptyStringsNewline =
14+
list: builtins.concatStringsSep "\n" (builtins.filter hasContent list);
15+
816
listToUnkeyedAttrs =
917
list:
1018
builtins.listToAttrs (lib.lists.imap0 (idx: lib.nameValuePair "__unkeyed-${toString idx}") list);
@@ -118,4 +126,24 @@ with lib;
118126
${string}
119127
end
120128
'';
129+
130+
# Wrap Vimscript for using in lua,
131+
# but only if the string contains something other than whitespaces
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+
wrapLuaForVimscript =
143+
string:
144+
optionalString (hasContent string) ''
145+
lua << EOF
146+
${string}
147+
EOF
148+
'';
121149
}

modules/output.nix

Lines changed: 22 additions & 19 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 {
@@ -99,25 +104,23 @@ in
99104

100105
config =
101106
let
102-
contentLua = ''
103-
${config.extraConfigLuaPre}
104-
vim.cmd([[
105-
${config.extraConfigVim}
106-
]])
107-
${config.extraConfigLua}
108-
${config.extraConfigLuaPost}
109-
'';
107+
contentLua = helpers.concatNonEmptyStringsNewline [
108+
config.extraConfigLuaPre
109+
(helpers.wrapVimscriptForLua config.extraConfigVim)
110+
config.extraConfigLua
111+
config.extraConfigLuaPost
112+
];
110113

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-
'';
114+
contentVim = helpers.concatNonEmptyStringsNewline [
115+
(helpers.wrapLuaForVimscript config.extraConfigLuaPre)
116+
config.extraConfigVim
117+
(helpers.wrapLuaForVimscript (
118+
helpers.concatNonEmptyStringsNewline [
119+
config.extraConfigLua
120+
config.extraConfigLuaPost
121+
]
122+
))
123+
];
121124
in
122125
{
123126
content = if config.type == "lua" then contentLua else contentVim;

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.concatNonEmptyStringsNewline [
111+
(helpers.wrapVimscriptForLua neovimConfig.neovimRcContent)
112+
config.content
113+
];
120114

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

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,91 @@
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+
configAssertion = name: config: [
23+
{
24+
assertion = lib.hasInfix "extraConfigLuaPre1" config;
25+
message = "Configuration file ${name} does not contain extraConfigLuaPre.";
26+
}
27+
{
28+
assertion = lib.hasInfix "extraConfigLua2" config;
29+
message = "Configuration file ${name} does not contain extraConfigLua.";
30+
}
31+
{
32+
assertion = lib.hasInfix "extraConfigLuaPost3" config;
33+
message = "Configuration file ${name} does not contain extraConfigLuaPost.";
34+
}
35+
{
36+
assertion = lib.hasInfix "extraConfigVim4" config;
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+
configAssertion "init.lua" config.content
58+
++ configAssertion "test.lua" config.files."test.lua".content
59+
++ configAssertion "test.vim" config.files."test.vim".content
60+
++ [
61+
# Only init.lua contains configuration from plugin definitions
62+
{
63+
# Plugin configs aren't available in options, only path to final init.lua
64+
assertion = lib.hasInfix "neovimRcContent5" (builtins.readFile config.initPath);
65+
message = "Configuration file init.lua does not contain plugin configs";
66+
}
67+
];
68+
};
69+
70+
files-default-empty.module =
71+
{ config, ... }:
72+
{
73+
files = {
74+
# lua type
75+
"test.lua" = { };
76+
# vim type
77+
"test.vim" = { };
78+
};
79+
80+
assertions = [
81+
{
82+
assertion = !helpers.hasContent config.files."test.lua".content;
83+
message = "Default content of test.lua file is expected to be empty.";
84+
}
85+
{
86+
assertion = !helpers.hasContent config.files."test.vim".content;
87+
message = "Default content of test.vim file is expected to be empty.";
88+
}
89+
];
90+
};
791
}

0 commit comments

Comments
 (0)