diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index 1f1b05d2..e15856d4 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -178,20 +178,14 @@ ( { __unkeyed-1 = originalName; - # Use provided after, otherwise fallback to normal lua content + # Use provided after, otherwise fallback to normal function wrapped lua content after = - if cfg.lazyLoad.settings.after != null then - cfg.lazyLoad.settings.after - else - # We need to wrap it in a function so it doesn't execute immediately - "function()\n " + cfg.luaConfig.content + " \nend"; - colorscheme = - if cfg.lazyLoad.settings.colorscheme != null then - cfg.lazyLoad.settings.colorscheme - else if (isColorscheme && colorscheme != null) then - colorscheme - else - null; + let + after = cfg.lazyLoad.settings.after or null; + default = "function()\n " + cfg.luaConfig.content + " \nend"; + in + if (lib.isString after || lib.types.rawLua.check after) then after else default; + colorscheme = lib.mkIf isColorscheme (cfg.lazyLoad.settings.colorscheme or colorscheme); } // lib.removeAttrs cfg.lazyLoad.settings [ "after" diff --git a/lib/options.nix b/lib/options.nix index 050ad1ab..a0040874 100644 --- a/lib/options.nix +++ b/lib/options.nix @@ -339,108 +339,47 @@ rec { lib.mkOption { description = '' Lazy-load settings for ${originalName}. + + > [!WARNING] + > This is an experimental option and may not work as expected with all plugins. + > The API may change without notice. + > Please report any issues you encounter. ''; - default = { - enable = false; - }; - type = - let - triggerType = - with types; - oneOf [ - rawLua - str - (listOf str) - ]; - in - types.submodule ( - { config, ... }: - { - options = with defaultNullOpts; { - enable = lib.mkOption { - default = lib.any (x: x != null) (builtins.attrValues config.settings); - description = '' - lazy-loading for ${originalName} - ''; - }; + default = { }; + type = types.submodule ( + { config, ... }: + { + options = { + enable = lib.mkOption { + default = lib.any (x: x != null) (builtins.attrValues config.settings); + defaultText = lib.literalMD '' + `true` when `settings` has a non-null attribute + ''; + description = '' + lazy-loading for ${originalName} + ''; + }; - settings = lib.mkOption { - description = ''''; - default = { }; - type = - with types; - submodule { - freeformType = attrsOf anything; - options = { - # Spec loading: - enabled = mkStrLuaFnOr types.bool null '' - When false, or if the function returns false, then ${originalName} will not be included in the spec. + settings = lib.nixvim.mkSettingsOption { + description = '' + Lazy provider configuration settings. - Equivalence: lz.n => enabled; lazy.nvim => enabled - ''; - - priority = mkNullable types.number null '' - Only useful for start plugins (not lazy-loaded) to force loading certain plugins first. - - Equivalence: lz.n => priority; lazy.nvim => priority - ''; - - # Spec setup - # Actions - beforeAll = mkLuaFn null '' - Always executed before any plugins are loaded. - - Equivalence: lz.n => beforeAll; lazy.nvim => init - ''; - - before = mkLuaFn null '' - Executed before ${originalName} is loaded. - - Equivalence: lz.n => before; lazy.nvim => None - ''; - - after = mkLuaFn null '' - Executed after ${originalName} is loaded. - - Equivalence: lz.n => after; lazy.nvim => config - ''; - - # Triggers - event = mkNullable triggerType null '' - Lazy-load on event. Events can be specified as `BufEnter` or with a pattern like `BufEnter *.lua` - - Equivalence: lz.n => event; lazy.nvim => event - ''; - - cmd = mkNullable triggerType null '' - Lazy-load on command. - - Equivalence: lz.n => cmd; lazy.nvim => cmd - ''; - - ft = mkNullable triggerType null '' - Lazy-load on filetype. - - Equivalence: lz.n => ft; lazy.nvim => ft - ''; - - keys = mkListOf (types.attrsOf types.anything) null '' - Lazy-load on key mapping. - - Equivalence: lz.n => keys; lazy.nvim => keys - ''; - - colorscheme = mkNullable triggerType null '' - Lazy-load on colorscheme. - - Equivalence: lz.n => colorscheme; lazy.nvim => None - ''; - }; - }; + Check your lazy loading provider's documentation on settings to configure. + ''; + example = { + cmd = "Neotest"; + keys = [ + { + __unkeyed-1 = "nt"; + __unkeyed-3 = "Neotest summary"; + desc = "Summary toggle"; + } + ]; }; }; - } - ); + }; + } + ); }; } // removed diff --git a/tests/test-sources/plugins/lazyloading/lz-n.nix b/tests/test-sources/plugins/lazyloading/lz-n.nix index 98cfb7bd..4c714fca 100644 --- a/tests/test-sources/plugins/lazyloading/lz-n.nix +++ b/tests/test-sources/plugins/lazyloading/lz-n.nix @@ -32,8 +32,8 @@ { assertion = let - plugins = config.plugins.lz-n.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + inherit (config.plugins.lz-n) plugins; + plugin = if plugins == [ ] then null else builtins.head plugins; keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ]; in (builtins.length keys) == 1; @@ -42,8 +42,8 @@ { assertion = let - plugins = config.plugins.lz-n.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + inherit (config.plugins.lz-n) plugins; + plugin = if plugins == [ ] then null else builtins.head plugins; in plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.after.__raw; message = "`lz-n.plugins[0].after` should have contained `neotest` lua content."; @@ -126,9 +126,10 @@ assertion = let plugin = builtins.head config.plugins.lz-n.plugins; - cmd = if builtins.isList plugin.cmd then plugin.cmd else [ ]; + cmd = plugin.cmd or null; + cmd' = lib.optionals (builtins.isList cmd) cmd; in - (builtins.length cmd) == 4; + (builtins.length cmd') == 4; message = let plugin = builtins.head config.plugins.lz-n.plugins; @@ -260,8 +261,8 @@ { assertion = let - plugins = config.plugins.lz-n.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + inherit (config.plugins.lz-n) plugins; + plugin = if plugins == [ ] then null else builtins.head plugins; keys = if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ]; in (builtins.length keys) == 1; @@ -282,6 +283,9 @@ enable = true; lazyLoad = { enable = true; + settings = { + cmd = [ "Telescope" ]; + }; }; }; }; @@ -301,4 +305,52 @@ } ]; }; + + use-provided-raw-after = + { config, ... }: + { + plugins = { + lz-n = { + enable = true; + }; + web-devicons.enable = false; + telescope = { + enable = true; + lazyLoad = { + enable = true; + settings = { + after.__raw = '' + function() + -- test string + ${config.plugins.telescope.luaConfig.content} + end + ''; + cmd = [ "Telescope" ]; + }; + }; + }; + }; + + assertions = + let + plugin = getFirstLznPlugin config; + in + [ + { + assertion = (builtins.length config.plugins.lz-n.plugins) == 1; + message = "`lz-n.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = + plugin.after.__raw == '' + function() + -- test string + ${config.plugins.telescope.luaConfig.content} + end + ''; + message = "`lz-n.plugins[0].after` should have contained a function wrapped `telescope` lua content."; + } + ]; + }; + }