Skip to content

Commit a98a4f9

Browse files
committed
lib/vim-plugin: Add a luaConfig option similar to mkNeovimPlugin
1 parent b53f24a commit a98a4f9

File tree

5 files changed

+100
-57
lines changed

5 files changed

+100
-57
lines changed

lib/neovim-plugin.nix

+1-1
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@
123123
}
124124
// lib.optionalAttrs hasConfigAttrs {
125125
luaConfig = lib.mkOption {
126-
type = lib.types.pluginLuaConfig;
126+
type = lib.types.pluginLuaConfig { hasContent = true; };
127127
default = { };
128128
description = "The plugin's lua configuration";
129129
};

lib/types.nix

+54-41
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,60 @@ rec {
137137
}";
138138
};
139139

140-
pluginLuaConfig = types.submodule (
141-
{ config, ... }:
142-
let
143-
inherit (builtins) toString;
144-
inherit (lib.nixvim.utils) mkBeforeSection mkAfterSection;
145-
in
146-
{
147-
options = {
148-
pre = lib.mkOption {
149-
type = with types; nullOr lines;
150-
default = null;
151-
description = ''
152-
Lua code inserted at the start of the plugin's configuration.
153-
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
154-
'';
155-
};
156-
post = lib.mkOption {
157-
type = with types; nullOr lines;
158-
default = null;
159-
description = ''
160-
Lua code inserted at the end of the plugin's configuration.
161-
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
162-
'';
163-
};
164-
content = lib.mkOption {
165-
type = types.lines;
166-
default = "";
167-
description = ''
168-
Configuration of the plugin.
140+
pluginLuaConfig =
141+
{ hasContent }:
142+
types.submodule (
143+
{ config, ... }:
144+
let
145+
inherit (builtins) toString;
146+
inherit (lib.nixvim.utils) mkBeforeSection mkAfterSection;
147+
in
148+
{
149+
options =
150+
{
151+
pre = lib.mkOption {
152+
type = with types; nullOr lines;
153+
default = null;
154+
description =
155+
''
156+
Lua code inserted at the start of the plugin's configuration.
157+
''
158+
+ lib.optionalString hasContent ''
159+
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
160+
'';
161+
};
162+
post = lib.mkOption {
163+
type = with types; nullOr lines;
164+
default = null;
165+
description =
166+
''
167+
Lua code inserted at the end of the plugin's configuration.
168+
''
169+
+ lib.optionalString hasContent ''
170+
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
171+
'';
172+
};
173+
}
174+
// (lib.optionalAttrs hasContent) {
175+
content = lib.mkOption {
176+
type = types.lines;
177+
default = "";
178+
description = ''
179+
Configuration of the plugin.
169180
170-
If `pre` and/or `post` are non-null, they will be merged using the order priorities
171-
${toString (mkBeforeSection null).priority} and ${toString (mkBeforeSection null).priority}
172-
respectively.
173-
'';
174-
};
175-
};
181+
If `pre` and/or `post` are non-null, they will be merged using the order priorities
182+
${toString (mkBeforeSection null).priority} and ${toString (mkBeforeSection null).priority}
183+
respectively.
184+
'';
185+
};
186+
};
176187

177-
config.content = lib.mkMerge (
178-
lib.optional (config.pre != null) (mkBeforeSection config.pre)
179-
++ lib.optional (config.post != null) (mkAfterSection config.post)
180-
);
181-
}
182-
);
188+
config = lib.optionalAttrs hasContent {
189+
content = lib.mkMerge (
190+
lib.optional (config.pre != null) (mkBeforeSection config.pre)
191+
++ lib.optional (config.post != null) (mkAfterSection config.post)
192+
);
193+
};
194+
}
195+
);
183196
}

lib/vim-plugin.nix

+8
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@
4949
- `other_toggle = false` -> `:setglobal no${globalPrefix}other_toggle`
5050
'';
5151
};
52+
53+
luaConfig = lib.mkOption {
54+
type = lib.types.pluginLuaConfig { hasContent = false; };
55+
default = { };
56+
description = "The plugin's lua configuration";
57+
};
5258
};
5359

5460
module =
@@ -113,7 +119,9 @@
113119
extraPlugins = extraPlugins ++ [
114120
(cfg.packageDecorator cfg.package)
115121
];
122+
globalsPre = cfg.luaConfig.pre;
116123
globals = lib.mapAttrs' (n: lib.nameValuePair (globalPrefix + n)) (cfg.settings or { });
124+
globalsPost = cfg.luaConfig.post;
117125
}
118126
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
119127
colorscheme = lib.mkDefault colorscheme;

modules/opts.nix

+17-15
Original file line numberDiff line numberDiff line change
@@ -84,23 +84,25 @@ in
8484
optionDefinitions = helpers.toLuaObject config.${optionName};
8585
ifGlobals = lib.optionalString (optionName == "globals");
8686
in
87-
lib.optionalString (optionDefinitions != "{ }") ''
88-
-- Set up ${prettyName} {{{
89-
''
90-
+ (ifGlobals config.globalsPre)
91-
+ ''
92-
do
93-
local ${varName} = ${optionDefinitions}
87+
lib.optionalString (optionDefinitions != "{ }") (
88+
''
89+
-- Set up ${prettyName} {{{
90+
''
91+
+ (ifGlobals config.globalsPre)
92+
+ ''
93+
do
94+
local ${varName} = ${optionDefinitions}
9495
95-
for k,v in pairs(${varName}) do
96-
vim.${luaApi}[k] = v
96+
for k,v in pairs(${varName}) do
97+
vim.${luaApi}[k] = v
98+
end
9799
end
98-
end
99-
''
100-
+ (ifGlobals config.globalsPost)
101-
+ ''
102-
-- }}}
103-
''
100+
''
101+
+ (ifGlobals config.globalsPost)
102+
+ ''
103+
-- }}}
104+
''
105+
)
104106
) optionsAttrs
105107
);
106108
in

tests/test-sources/plugins/lua-config.nix

+20
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,24 @@
2323
end
2424
'';
2525
};
26+
lua-config-vim-plugin = {
27+
plugins.typst-vim = {
28+
enable = true;
29+
luaConfig.pre = # lua
30+
''
31+
local command = "typst-wrapped" -- Let's say we got it through env vars
32+
'';
33+
settings.cmd.__raw = "command";
34+
luaConfig.post = # lua
35+
''
36+
local globals_cmd = vim.g.typst_cmd
37+
'';
38+
};
39+
40+
extraConfigLuaPost = ''
41+
if globals_cmd ~= command then
42+
print("globals_cmd different than command: " .. globals_cmd .. ", " .. command)
43+
end
44+
'';
45+
};
2646
}

0 commit comments

Comments
 (0)