diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index bbb5324b37..e8049837e3 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -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"; }; diff --git a/lib/types.nix b/lib/types.nix index 9a5e637b66..5b4d6e9460 100644 --- a/lib/types.nix +++ b/lib/types.nix @@ -137,47 +137,60 @@ rec { }"; }; - pluginLuaConfig = types.submodule ( - { config, ... }: - 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. + pluginLuaConfig = + { hasContent }: + types.submodule ( + { config, ... }: + 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. + '' + + 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`. + ''; + }; + } + // (lib.optionalAttrs hasContent) { + 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. - ''; - }; - }; + 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) - ); - } - ); + config = lib.optionalAttrs hasContent { + content = lib.mkMerge ( + lib.optional (config.pre != null) (mkBeforeSection config.pre) + ++ lib.optional (config.post != null) (mkAfterSection config.post) + ); + }; + } + ); } diff --git a/lib/vim-plugin.nix b/lib/vim-plugin.nix index 2e564b4d1b..4382d2227f 100644 --- a/lib/vim-plugin.nix +++ b/lib/vim-plugin.nix @@ -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 = @@ -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; }) diff --git a/modules/opts.nix b/modules/opts.nix index 70162d619e..985467bbc3 100644 --- a/modules/opts.nix +++ b/modules/opts.nix @@ -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 diff --git a/tests/test-sources/plugins/lua-config.nix b/tests/test-sources/plugins/lua-config.nix index 54bbabd7b6..e0c1240d01 100644 --- a/tests/test-sources/plugins/lua-config.nix +++ b/tests/test-sources/plugins/lua-config.nix @@ -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 + ''; + }; }