Skip to content

Commit 3542fd5

Browse files
committed
lib/vim-plugin: Add a luaConfig option similar to mkNeovimPlugin
1 parent 1b9f331 commit 3542fd5

File tree

4 files changed

+84
-38
lines changed

4 files changed

+84
-38
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

+53-37
Original file line numberDiff line numberDiff line change
@@ -137,47 +137,63 @@ rec {
137137
}";
138138
};
139139

140-
pluginLuaConfig = types.submodule (
141-
{ config, ... }:
140+
pluginLuaConfig =
141+
{ hasContent }:
142142
let
143143
inherit (builtins) toString;
144144
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.
169-
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-
'';
145+
146+
commonModule = {
147+
options = {
148+
pre = lib.mkOption {
149+
type = with types; nullOr lines;
150+
default = null;
151+
description =
152+
''
153+
Lua code inserted at the start of the plugin's configuration.
154+
''
155+
+ lib.optionalString hasContent ''
156+
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
157+
'';
158+
};
159+
post = lib.mkOption {
160+
type = with types; nullOr lines;
161+
default = null;
162+
description =
163+
''
164+
Lua code inserted at the end of the plugin's configuration.
165+
''
166+
+ lib.optionalString hasContent ''
167+
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
168+
'';
169+
};
174170
};
175171
};
176172

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-
);
173+
contentModule =
174+
{ config, ... }:
175+
{
176+
options = {
177+
content = lib.mkOption {
178+
type = types.lines;
179+
default = "";
180+
description = ''
181+
Configuration of the plugin.
182+
183+
If `pre` and/or `post` are non-null, they will be merged using the order priorities
184+
${toString (mkBeforeSection null).priority} and ${toString (mkBeforeSection null).priority}
185+
respectively.
186+
'';
187+
};
188+
};
189+
190+
config = {
191+
content = lib.mkMerge (
192+
lib.optional (config.pre != null) (mkBeforeSection config.pre)
193+
++ lib.optional (config.post != null) (mkAfterSection config.post)
194+
);
195+
};
196+
};
197+
in
198+
types.submodule ([ commonModule ] ++ (lib.optional hasContent contentModule));
183199
}

lib/vim-plugin.nix

+10
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 =
@@ -115,6 +121,10 @@
115121
];
116122
globals = lib.mapAttrs' (n: lib.nameValuePair (globalPrefix + n)) (cfg.settings or { });
117123
}
124+
(lib.optionalAttrs createSettingsOption {
125+
globalsPre = lib.nixvim.mkIfNonNull cfg.luaConfig.pre;
126+
globalsPost = lib.nixvim.mkIfNonNull cfg.luaConfig.post;
127+
})
118128
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
119129
colorscheme = lib.mkDefault colorscheme;
120130
})

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)