Skip to content

Commit 86fb737

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

File tree

5 files changed

+101
-53
lines changed

5 files changed

+101
-53
lines changed

lib/neovim-plugin.nix

Lines changed: 1 addition & 1 deletion
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

Lines changed: 53 additions & 37 deletions
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

Lines changed: 10 additions & 0 deletions
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 = cfg.luaConfig.pre;
126+
globalsPost = cfg.luaConfig.post;
127+
})
118128
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
119129
colorscheme = lib.mkDefault colorscheme;
120130
})

modules/opts.nix

Lines changed: 17 additions & 15 deletions
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

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