From 1b0d3e778aae8a3e92402a656fc53c958d751023 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Thu, 5 Dec 2024 22:11:47 -0600 Subject: [PATCH 1/3] lib/neovim-plugin: add lazy provider --- lib/neovim-plugin.nix | 19 +++ modules/lazyload.nix | 3 +- .../test-sources/plugins/lazyloading/lazy.nix | 139 ++++++++++++++++++ 3 files changed, 160 insertions(+), 1 deletion(-) create mode 100644 tests/test-sources/plugins/lazyloading/lazy.nix diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index e15856d4d..f9331c6c2 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -194,6 +194,25 @@ ) ]; }; + plugins.lazy = lib.mkIf config.plugins.lazy.enable { + plugins = [ + ( + { + name = originalName; + main = luaName; + pkg = cfg.package; + # Use provided config, otherwise fallback to normal lua content + config = + cfg.lazyLoad.settings.config or + # We need to wrap it in a function so it doesn't execute immediately + ("function()\n " + cfg.luaConfig.content + " \nend"); + } + // (lib.removeAttrs cfg.lazyLoad.settings [ + "config" + ]) + ) + ]; + }; }) ]) ) diff --git a/modules/lazyload.nix b/modules/lazyload.nix index 832d42579..f8cd75d00 100644 --- a/modules/lazyload.nix +++ b/modules/lazyload.nix @@ -45,13 +45,14 @@ in ) (builtins.attrNames config.plugins); count = builtins.length pluginsWithLazyLoad; in - lib.optionals (count > 0 && !config.plugins.lz-n.enable) [ + lib.optionals (count > 0 && !config.plugins.lz-n.enable && !config.plugins.lazy.enable) [ '' You have enabled lazy loading support for the following plugins but have not enabled a lazy loading provider. ${lib.concatImapStringsSep "\n" (i: x: "${toString i}. plugins.${x}") pluginsWithLazyLoad} Currently supported lazy providers: - lz-n + - lazy '' ]; }; diff --git a/tests/test-sources/plugins/lazyloading/lazy.nix b/tests/test-sources/plugins/lazyloading/lazy.nix new file mode 100644 index 000000000..a4f652e5b --- /dev/null +++ b/tests/test-sources/plugins/lazyloading/lazy.nix @@ -0,0 +1,139 @@ +{ + lazy-load-neovim-plugin-configured = + { config, lib, ... }: + { + plugins = { + lazy = { + enable = true; + }; + + neotest = { + enable = true; + lazyLoad = { + enable = true; + settings = { + cmd = [ "Neotest" ]; + }; + }; + }; + }; + + assertions = [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 1; + message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = + let + plugins = config.plugins.lazy.plugins or [ ]; + plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ]; + in + (builtins.length cmd) == 1; + message = "`lazy.plugins[0].cmd` should have contained a configuration."; + } + { + assertion = + let + plugins = config.plugins.lazy.plugins or [ ]; + plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + in + plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.config.__raw; + message = "`lazy.plugins[0].after` should have contained `neotest` lua content."; + } + ]; + }; + + dont-lazy-load-unconfigured = + { config, ... }: + { + plugins = { + neotest = { + enable = true; + # Empty attrset shouldn't trigger lazy loading + lazyLoad = { }; + }; + lazy = { + enable = true; + }; + }; + + assertions = [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 0; + message = "`lazy.plugins` should have contained no plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + ]; + }; + + lazy-load-enabled-automatically = + { config, ... }: + { + plugins = { + lazy = { + enable = true; + }; + neotest = { + enable = true; + lazyLoad = { + # Not setting lazyLoad.enable with configuration should enable + settings = { + cmd = [ "Neotest" ]; + }; + }; + }; + }; + + assertions = [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 1; + message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = + let + plugins = config.plugins.lazy.plugins or [ ]; + plugin = if builtins.length plugins > 0 then builtins.head plugins else null; + cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ]; + in + (builtins.length cmd) == 1; + message = "`lazy.plugins[0].cmd` should have contained a configuration."; + } + ]; + }; + wrap-functionless-luaConfig = + { config, ... }: + { + plugins = { + lazy = { + enable = true; + }; + web-devicons.enable = false; + telescope = { + enable = true; + lazyLoad = { + enable = true; + settings = { + cmd = [ "Telescope" ]; + }; + }; + }; + }; + + assertions = [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 1; + message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = + let + plugin = builtins.head config.plugins.lazy.plugins; + in + plugin.config.__raw == "function()\n " + config.plugins.telescope.luaConfig.content + " \nend"; + message = "`lazy.plugins[0].config` should have contained a function wrapped `telescope` lua content."; + } + ]; + }; +} From 832693b90ff9ade7da774b4cdd03c2775f201652 Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Thu, 5 Dec 2024 23:00:06 -0600 Subject: [PATCH 2/3] lib/neovim-plugin: lazy prefer opts over config Upstream documentation recommends just providing the opts instead of the function call explicitly. --- lib/neovim-plugin.nix | 9 ++--- .../test-sources/plugins/lazyloading/lazy.nix | 38 +------------------ 2 files changed, 5 insertions(+), 42 deletions(-) diff --git a/lib/neovim-plugin.nix b/lib/neovim-plugin.nix index f9331c6c2..121298e96 100644 --- a/lib/neovim-plugin.nix +++ b/lib/neovim-plugin.nix @@ -201,14 +201,11 @@ name = originalName; main = luaName; pkg = cfg.package; - # Use provided config, otherwise fallback to normal lua content - config = - cfg.lazyLoad.settings.config or - # We need to wrap it in a function so it doesn't execute immediately - ("function()\n " + cfg.luaConfig.content + " \nend"); + # Use provided opts, otherwise fallback to settings + opts = cfg.lazyLoad.settings.opts or cfg.settings; } // (lib.removeAttrs cfg.lazyLoad.settings [ - "config" + "opts" ]) ) ]; diff --git a/tests/test-sources/plugins/lazyloading/lazy.nix b/tests/test-sources/plugins/lazyloading/lazy.nix index a4f652e5b..9cd4b210b 100644 --- a/tests/test-sources/plugins/lazyloading/lazy.nix +++ b/tests/test-sources/plugins/lazyloading/lazy.nix @@ -39,8 +39,8 @@ plugins = config.plugins.lazy.plugins or [ ]; plugin = if builtins.length plugins > 0 then builtins.head plugins else null; in - plugin != null && lib.hasInfix config.plugins.neotest.luaConfig.content plugin.config.__raw; - message = "`lazy.plugins[0].after` should have contained `neotest` lua content."; + plugin != null && config.plugins.neotest.settings == plugin.opts; + message = "`lazy.plugins[0].opts` should have contained `neotest` settings."; } ]; }; @@ -102,38 +102,4 @@ } ]; }; - wrap-functionless-luaConfig = - { config, ... }: - { - plugins = { - lazy = { - enable = true; - }; - web-devicons.enable = false; - telescope = { - enable = true; - lazyLoad = { - enable = true; - settings = { - cmd = [ "Telescope" ]; - }; - }; - }; - }; - - assertions = [ - { - assertion = (builtins.length config.plugins.lazy.plugins) == 1; - message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; - } - { - assertion = - let - plugin = builtins.head config.plugins.lazy.plugins; - in - plugin.config.__raw == "function()\n " + config.plugins.telescope.luaConfig.content + " \nend"; - message = "`lazy.plugins[0].config` should have contained a function wrapped `telescope` lua content."; - } - ]; - }; } From 9b867f19d2daff5d318b103adbcf98d961e085ed Mon Sep 17 00:00:00 2001 From: Austin Horstman Date: Tue, 10 Dec 2024 10:34:20 -0600 Subject: [PATCH 3/3] tests/lazyloading/lazy: reduce repetition --- .../test-sources/plugins/lazyloading/lazy.nix | 86 ++++++++++--------- 1 file changed, 45 insertions(+), 41 deletions(-) diff --git a/tests/test-sources/plugins/lazyloading/lazy.nix b/tests/test-sources/plugins/lazyloading/lazy.nix index 9cd4b210b..a1d807253 100644 --- a/tests/test-sources/plugins/lazyloading/lazy.nix +++ b/tests/test-sources/plugins/lazyloading/lazy.nix @@ -1,3 +1,14 @@ +let + getFirstLazyPlugin = + config: + let + inherit (config.plugins.lazy) plugins; + in + if plugins == [ ] then null else builtins.head plugins; + + getPluginKeys = plugin: if plugin != null && builtins.isList plugin.keys then plugin.keys else [ ]; + getPluginCmd = plugin: if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ]; +in { lazy-load-neovim-plugin-configured = { config, lib, ... }: @@ -18,31 +29,25 @@ }; }; - assertions = [ - { - assertion = (builtins.length config.plugins.lazy.plugins) == 1; - message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; - } - { - assertion = - let - plugins = config.plugins.lazy.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; - cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ]; - in - (builtins.length cmd) == 1; - message = "`lazy.plugins[0].cmd` should have contained a configuration."; - } - { - assertion = - let - plugins = config.plugins.lazy.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; - in - plugin != null && config.plugins.neotest.settings == plugin.opts; - message = "`lazy.plugins[0].opts` should have contained `neotest` settings."; - } - ]; + assertions = + let + plugin = getFirstLazyPlugin config; + cmd = getPluginCmd plugin; + in + [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 1; + message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = (builtins.length cmd) == 1; + message = "`lazy.plugins[0].cmd` should have contained a configuration."; + } + { + assertion = plugin != null && config.plugins.neotest.settings == plugin.opts; + message = "`lazy.plugins[0].opts` should have contained `neotest` settings."; + } + ]; }; dont-lazy-load-unconfigured = @@ -85,21 +90,20 @@ }; }; - assertions = [ - { - assertion = (builtins.length config.plugins.lazy.plugins) == 1; - message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; - } - { - assertion = - let - plugins = config.plugins.lazy.plugins or [ ]; - plugin = if builtins.length plugins > 0 then builtins.head plugins else null; - cmd = if plugin != null && builtins.isList plugin.cmd then plugin.cmd else [ ]; - in - (builtins.length cmd) == 1; - message = "`lazy.plugins[0].cmd` should have contained a configuration."; - } - ]; + assertions = + let + plugin = getFirstLazyPlugin config; + cmd = getPluginCmd plugin; + in + [ + { + assertion = (builtins.length config.plugins.lazy.plugins) == 1; + message = "`lazy.plugins` should have contained a single plugin configuration, but contained ${builtins.toJSON config.plugins.lz-n.plugins}"; + } + { + assertion = (builtins.length cmd) == 1; + message = "`lazy.plugins[0].cmd` should have contained a configuration."; + } + ]; }; }