Skip to content

Commit

Permalink
lib/vim-plugin: Add a luaConfig option similar to mkNeovimPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
traxys committed Dec 11, 2024
1 parent b53f24a commit 86fb737
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 53 deletions.
2 changes: 1 addition & 1 deletion lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@
}
// lib.optionalAttrs hasConfigAttrs {
luaConfig = lib.mkOption {
type = lib.types.pluginLuaConfig;
type = lib.types.pluginLuaConfig { hasContent = true; };
default = { };
description = "The plugin's lua configuration";
};
Expand Down
90 changes: 53 additions & 37 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -137,47 +137,63 @@ rec {
}";
};

pluginLuaConfig = types.submodule (
{ config, ... }:
pluginLuaConfig =
{ hasContent }:
let
inherit (builtins) toString;
inherit (lib.nixvim.utils) mkBeforeSection mkAfterSection;
in
{
options = {
pre = lib.mkOption {
type = with types; nullOr lines;
default = null;
description = ''
Lua code inserted at the start of the plugin's configuration.
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
'';
};
post = lib.mkOption {
type = with types; nullOr lines;
default = null;
description = ''
Lua code inserted at the end of the plugin's configuration.
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
'';
};
content = lib.mkOption {
type = types.lines;
default = "";
description = ''
Configuration of the plugin.
If `pre` and/or `post` are non-null, they will be merged using the order priorities
${toString (mkBeforeSection null).priority} and ${toString (mkBeforeSection null).priority}
respectively.
'';

commonModule = {
options = {
pre = lib.mkOption {
type = with types; nullOr lines;
default = null;
description =
''
Lua code inserted at the start of the plugin's configuration.
''
+ lib.optionalString hasContent ''
This is the same as using `lib.nixvim.utils.mkBeforeSection` when defining `content`.
'';
};
post = lib.mkOption {
type = with types; nullOr lines;
default = null;
description =
''
Lua code inserted at the end of the plugin's configuration.
''
+ lib.optionalString hasContent ''
This is the same as using `lib.nixvim.utils.mkAfterSection` when defining `content`.
'';
};
};
};

config.content = lib.mkMerge (
lib.optional (config.pre != null) (mkBeforeSection config.pre)
++ lib.optional (config.post != null) (mkAfterSection config.post)
);
}
);
contentModule =
{ config, ... }:
{
options = {
content = lib.mkOption {
type = types.lines;
default = "";
description = ''
Configuration of the plugin.
If `pre` and/or `post` are non-null, they will be merged using the order priorities
${toString (mkBeforeSection null).priority} and ${toString (mkBeforeSection null).priority}
respectively.
'';
};
};

config = {
content = lib.mkMerge (
lib.optional (config.pre != null) (mkBeforeSection config.pre)
++ lib.optional (config.post != null) (mkAfterSection config.post)
);
};
};
in
types.submodule ([ commonModule ] ++ (lib.optional hasContent contentModule));
}
10 changes: 10 additions & 0 deletions lib/vim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@
- `other_toggle = false` -> `:setglobal no${globalPrefix}other_toggle`
'';
};

luaConfig = lib.mkOption {
type = lib.types.pluginLuaConfig { hasContent = false; };
default = { };
description = "The plugin's lua configuration";
};
};

module =
Expand Down Expand Up @@ -115,6 +121,10 @@
];
globals = lib.mapAttrs' (n: lib.nameValuePair (globalPrefix + n)) (cfg.settings or { });
}
(lib.optionalAttrs createSettingsOption {
globalsPre = cfg.luaConfig.pre;
globalsPost = cfg.luaConfig.post;
})
(lib.optionalAttrs (isColorscheme && (colorscheme != null)) {
colorscheme = lib.mkDefault colorscheme;
})
Expand Down
32 changes: 17 additions & 15 deletions modules/opts.nix
Original file line number Diff line number Diff line change
Expand Up @@ -84,23 +84,25 @@ in
optionDefinitions = helpers.toLuaObject config.${optionName};
ifGlobals = lib.optionalString (optionName == "globals");
in
lib.optionalString (optionDefinitions != "{ }") ''
-- Set up ${prettyName} {{{
''
+ (ifGlobals config.globalsPre)
+ ''
do
local ${varName} = ${optionDefinitions}
lib.optionalString (optionDefinitions != "{ }") (
''
-- Set up ${prettyName} {{{
''
+ (ifGlobals config.globalsPre)
+ ''
do
local ${varName} = ${optionDefinitions}
for k,v in pairs(${varName}) do
vim.${luaApi}[k] = v
for k,v in pairs(${varName}) do
vim.${luaApi}[k] = v
end
end
end
''
+ (ifGlobals config.globalsPost)
+ ''
-- }}}
''
''
+ (ifGlobals config.globalsPost)
+ ''
-- }}}
''
)
) optionsAttrs
);
in
Expand Down
20 changes: 20 additions & 0 deletions tests/test-sources/plugins/lua-config.nix
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,24 @@
end
'';
};
lua-config-vim-plugin = {
plugins.typst-vim = {
enable = true;
luaConfig.pre = # lua
''
local command = "typst-wrapped" -- Let's say we got it through env vars
'';
settings.cmd.__raw = "command";
luaConfig.post = # lua
''
local globals_cmd = vim.g.typst_cmd
'';
};

extraConfigLuaPost = ''
if globals_cmd ~= command then
print("globals_cmd different than command: " .. globals_cmd .. ", " .. command)
end
'';
};
}

0 comments on commit 86fb737

Please sign in to comment.