Skip to content

Commit b53f24a

Browse files
committed
modules/options: Allow to add code around the globals
This will be used to implement `luaConfig` for plugins that use global variables for their configuration
1 parent f2a991a commit b53f24a

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

modules/opts.nix

+30-9
Original file line numberDiff line numberDiff line change
@@ -36,15 +36,29 @@ let
3636
};
3737
in
3838
{
39-
options = lib.mapAttrs (
40-
_:
41-
{ description, ... }:
42-
lib.mkOption {
43-
type = with lib.types; attrsOf anything;
44-
default = { };
45-
inherit description;
46-
}
47-
) optionsAttrs;
39+
options =
40+
(lib.mapAttrs (
41+
_:
42+
{ description, ... }:
43+
lib.mkOption {
44+
type = with lib.types; attrsOf anything;
45+
default = { };
46+
inherit description;
47+
}
48+
) optionsAttrs)
49+
// {
50+
globalsPre = lib.mkOption {
51+
type = lib.types.lines;
52+
default = "";
53+
internal = true;
54+
};
55+
56+
globalsPost = lib.mkOption {
57+
type = lib.types.lines;
58+
default = "";
59+
internal = true;
60+
};
61+
};
4862

4963
# Added 2024-03-29 (do not remove)
5064
imports = lib.mapAttrsToList (old: new: lib.mkRenamedOptionModule [ old ] [ new ]) {
@@ -68,16 +82,23 @@ in
6882
let
6983
varName = "nixvim_${luaVariableName}";
7084
optionDefinitions = helpers.toLuaObject config.${optionName};
85+
ifGlobals = lib.optionalString (optionName == "globals");
7186
in
7287
lib.optionalString (optionDefinitions != "{ }") ''
7388
-- Set up ${prettyName} {{{
89+
''
90+
+ (ifGlobals config.globalsPre)
91+
+ ''
7492
do
7593
local ${varName} = ${optionDefinitions}
7694
7795
for k,v in pairs(${varName}) do
7896
vim.${luaApi}[k] = v
7997
end
8098
end
99+
''
100+
+ (ifGlobals config.globalsPost)
101+
+ ''
81102
-- }}}
82103
''
83104
) optionsAttrs

tests/test-sources/modules/options.nix

+10
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,20 @@
2323
};
2424

2525
globals = {
26+
globalsPre = ''
27+
dummy = 45
28+
'';
2629
globals = {
30+
with_pre.__raw = "dummy";
2731
loaded_ruby_provider = 0;
2832
loaded_perl_provider = 0;
2933
loaded_python_provider = 0;
3034
};
35+
globalsPost = # lua
36+
''
37+
if vim.g.with_pre ~= dummy then
38+
print("Mismatch for vim.g.with_pre, expected " .. dummy .. " got " .. vim.g.with_pre)
39+
end
40+
'';
3141
};
3242
}

0 commit comments

Comments
 (0)