Skip to content

Commit 1b9f331

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 1b9f331

File tree

2 files changed

+51
-18
lines changed

2 files changed

+51
-18
lines changed

modules/opts.nix

Lines changed: 41 additions & 18 deletions
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,18 +82,27 @@ in
6882
let
6983
varName = "nixvim_${luaVariableName}";
7084
optionDefinitions = helpers.toLuaObject config.${optionName};
85+
ifGlobals = lib.optionalString (optionName == "globals");
7186
in
72-
lib.optionalString (optionDefinitions != "{ }") ''
73-
-- Set up ${prettyName} {{{
74-
do
75-
local ${varName} = ${optionDefinitions}
87+
lib.optionalString (optionDefinitions != "{ }") (
88+
''
89+
-- Set up ${prettyName} {{{
90+
''
91+
+ (ifGlobals config.globalsPre)
92+
+ ''
93+
do
94+
local ${varName} = ${optionDefinitions}
7695
77-
for k,v in pairs(${varName}) do
78-
vim.${luaApi}[k] = v
96+
for k,v in pairs(${varName}) do
97+
vim.${luaApi}[k] = v
98+
end
7999
end
80-
end
81-
-- }}}
82-
''
100+
''
101+
+ (ifGlobals config.globalsPost)
102+
+ ''
103+
-- }}}
104+
''
105+
)
83106
) optionsAttrs
84107
);
85108
in

tests/test-sources/modules/options.nix

Lines changed: 10 additions & 0 deletions
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)