Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

lib/options: lib/neovim-plugin: Add lazyLoad option to plugins #1866

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/helpers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@ let
nixvimBuilders = import ./builders.nix { inherit lib pkgs; };
nixvimTypes = import ./types.nix { inherit lib nixvimOptions; };
nixvimUtils = import ./utils.nix { inherit lib nixvimTypes _nixvimTests; };
nixvimOptions = import ./options.nix { inherit lib nixvimTypes nixvimUtils; };
nixvimOptions = import ./options.nix {
inherit
lib
nixvimTypes
nixvimUtils
nixvimKeymaps
;
};
nixvimDeprecation = import ./deprecation.nix { inherit lib; };
nixvimKeymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
in
rec {
maintainers = import ./maintainers.nix;
lua = import ./to-lua.nix { inherit lib; };
keymaps = import ./keymap-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
keymaps = nixvimKeymaps;
autocmd = import ./autocmd-helpers.nix { inherit lib nixvimOptions nixvimTypes; };
neovim-plugin = import ./neovim-plugin.nix {
inherit
Expand Down
6 changes: 6 additions & 0 deletions lib/maintainers.nix
Original file line number Diff line number Diff line change
Expand Up @@ -70,4 +70,10 @@
githubId = 4646110;
name = "Nikita Shirokov";
};
psfloyd = {
email = "[email protected]";
github = "psfloyd";
githubId = 30784060;
name = "Pedro Sánchez";
};
}
22 changes: 20 additions & 2 deletions lib/neovim-plugin.nix
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,12 @@ with lib;
extraPackages ? [ ],
callSetup ? true,
installPackage ? true,
allowLazyLoad ? true,
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
lazyLoad ? { },
}:
let
namespace = if isColorscheme then "colorschemes" else "plugins";
cfg = config.${namespace}.${name};
in
{
meta = {
Expand Down Expand Up @@ -99,21 +102,36 @@ with lib;
example = settingsExample;
};
}
// optionalAttrs allowLazyLoad {
lazyLoad = nixvimOptions.mkLazyLoadOption {
inherit originalName cfg;
optionsForPlugin = true;
lazyLoad = {
# TODO: extract extraConfigLua and extraConfigLuaPre from extraConfig
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
after = optionalString callSetup ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';
colorscheme = if (isColorscheme && (colorscheme != null)) then [ colorscheme ] else null;
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
} // lazyLoad;
};
}
// extraOptions;

config =
let
cfg = config.${namespace}.${name};
extraConfigNamespace = if isColorscheme then "extraConfigLuaPre" else "extraConfigLua";
lazyLoaded = if (cfg ? lazyLoad) then cfg.lazyLoad.enable else false;
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
in
mkIf cfg.enable (mkMerge [
{
extraPlugins = (optional installPackage cfg.package) ++ extraPlugins;
inherit extraPackages;

${extraConfigNamespace} = optionalString callSetup ''
${extraConfigNamespace} = optionalString (callSetup && !lazyLoaded) ''
require('${luaName}')${setup}(${optionalString (cfg ? settings) (toLuaObject cfg.settings)})
'';

plugins.lz-n.plugins = mkIf lazyLoaded [ cfg.lazyLoad ];
psfloyd marked this conversation as resolved.
Show resolved Hide resolved
}
(optionalAttrs (isColorscheme && (colorscheme != null)) { colorscheme = mkDefault colorscheme; })
(extraConfig cfg)
Expand Down
67 changes: 67 additions & 0 deletions lib/options.nix
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
lib,
nixvimTypes,
nixvimUtils,
nixvimKeymaps,
}:
with lib;
with nixvimUtils;
Expand Down Expand Up @@ -368,4 +369,70 @@ rec {
else
example;
};

mkLazyLoadOption =
{
originalName ? "this plugin",
cfg ? { },
lazyLoad ? { },
optionsForPlugin ? false,
}:
let
lazyLoadPluginDefault = {
enable = false;
name = originalName;
enabledInSpec = true;
} // lazyLoad;
getPluginDefault = n: if (lazyLoadPluginDefault ? ${n}) then lazyLoadPluginDefault.${n} else null;
in
mkOption {
description = "Lazy-load settings for ${originalName}.";
type =
with nixvimTypes;
submodule {
options = with defaultNullOpts; {
# enable option only has an effect when it is a sub-option for a plugin generated by mkNeovimPlugin
enable = mkOption {
type = bool;
default = getPluginDefault "enable";
description = "Enable lazy-loading for ${originalName}";
# Only show this option when it is a sub-option for a plugin
visible = optionsForPlugin;
};
name = mkOption {
type = str;
default = getPluginDefault "name";
description = "The plugin's name (not the module name). This is what is passed to the load(name) function.";
};
enabledInSpec = mkStrLuaFnOr bool (getPluginDefault "enabledInSpec") ''
When false, or if the function returns false, then ${originalName} will not be included in the spec.
This option corresponds to the `enabled` property of lz.n.
'';
beforeAll = mkLuaFn (getPluginDefault "beforeAll") "Always executed before any plugins are loaded.";
before = mkLuaFn (getPluginDefault "before") "Executed before ${originalName} is loaded.";
after = mkLuaFn (getPluginDefault "after") "Executed after ${originalName} is loaded.";
event =
mkNullable (listOf str) (getPluginDefault "event")
"Lazy-load on event. Events can be specified as BufEnter or with a pattern like BufEnter *.lua";
cmd = mkNullable (listOf str) (getPluginDefault "cmd") "Lazy-load on command.";
ft = mkNullable (listOf str) (getPluginDefault "ft") "Lazy-load on filetype.";
keys =
mkNullable (listOf nixvimKeymaps.mapOptionSubmodule) (getPluginDefault "keys")
"Lazy-load on key mapping. Use the same format as `config.keymaps`.";
colorscheme = mkNullable (listOf str) (getPluginDefault "colorscheme") "Lazy-load on colorscheme.";
priority = mkNullable number (getPluginDefault "priority") ''
Only useful for start plugins (not lazy-loaded) to force loading certain plugins first.
Default priority is 50 (or 1000 if colorscheme is set).
'';
load = mkLuaFn (getPluginDefault "load") "Can be used to override the vim.g.lz_n.load() function for ${originalName}.";
};
config = mkIf (cfg != { }) (
mapAttrs (
name: value: if (builtins.typeOf value == "list") then value else mkDefault value
) lazyLoadPluginDefault
);
};
default = lazyLoadPluginDefault;
};

}
1 change: 1 addition & 0 deletions plugins/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@

./pluginmanagers/packer.nix
./pluginmanagers/lazy.nix
./pluginmanagers/lz-n.nix

./snippets/friendly-snippets.nix
./snippets/luasnip
Expand Down
95 changes: 95 additions & 0 deletions plugins/pluginmanagers/lz-n.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
{ lib
, helpers
, config
, pkgs
, ...
}:
let
inherit (helpers) mkNullOrLuaFn' nixvimTypes mkLazyLoadOption;

name = "lz-n";
originalName = "lz.n";
cfg = config.plugins.${name};
in
with lib;
helpers.neovim-plugin.mkNeovimPlugin config {
inherit name originalName;
maintainers = with helpers.maintainers; [ psfloyd ];
defaultPackage = pkgs.vimPlugins.lz-n;

settingsDescription = ''
The configuration options for **${originalName}** using `vim.g.lz_n`.

`{ load = "fun"; }` -> `vim.g.lz_n = { load = fun, }`
'';

settingsOptions = {
load = mkNullOrLuaFn' {
description = ''
Function used by `lz.n` to load plugins.
'';
default = null;
pluginDefault = "vim.cmd.packadd";
};
};

settingsExample = {
load = "vim.cmd.packadd";
};

callSetup = false; # Does not use setup
allowLazyLoad = false;

extraOptions = with nixvimTypes; {
plugins = mkOption {
description = "List of plugins processed by lz.n";
default = [ ];
type = listOf (mkLazyLoadOption { }).type;
};
};

extraConfig = cfg: {
globals.lz_n = cfg.settings;
extraConfigLua =
let
processKeymap =
keymaps:
if keymaps == null then
null
else
map (
keymap:
{
__unkeyed_1 = keymap.key;
__unkeyed_2 = keymap.action;
inherit (keymap) mode;
}
// keymap.options
) keymaps;
pluginToLua = plugin: {
"__unkeyed" = plugin.name;
inherit (plugin)
beforeAll
before
after
event
cmd
ft
colorscheme
priority
load
;
enabled = enabledInSpec;
keys = processKeymap plugin.keys;
};
pluginListToLua = map pluginToLua;
plugins = pluginListToLua cfg.plugins;
pluginSpecs = if length plugins == 1 then head plugins else plugins;
in
mkIf (cfg.plugins != [ ]) ''
require('lz.n').load(
${helpers.toLuaObject pluginSpecs}
)
'';
};
}
86 changes: 86 additions & 0 deletions tests/test-sources/plugins/pluginmanagers/lz-n.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
{ pkgs, ... }:
{
# Empty configuration
empty = {
plugins.lz-n.enable = true;
};

# Settings
example = {
plugins.lz-n = {
enable = true;
settings = {
load = "vim.cmd.packadd";
};
};

};

test = {
extraPlugins = with pkgs.vimPlugins; [
neo-tree-nvim
telescope-nvim
onedarker-nvim
vimtex

];
plugins.lz-n = {
enable = true;
plugins = [
# On keymap with setup function
{
name = "neo-tree.nvim";
keys = [
{
mode = [ "n" ];
key = "<leader>ft";
action = "<CMD>Neotree toggle<CR>";
options = {
desc = "NeoTree toggle";
};
}
{
mode = [
"n"
"v"
];
key = "gft";
action = "<CMD>Neotree toggle<CR>";
options = {
desc = "NeoTree toggle";
};
}
];
after = # lua
''
function()
require("neo-tree").setup()
end
'';
}
# On command no setup function
{
name = "telescope.nvim";
cmd = [ "Telescope" ];
}
# On colorschme
{
name = "onedarker.nvim";
colorscheme = [ "onedarker" ];
}
# On filetype with before function
{
name = "vimtex";
ft = [ "plaintex" ];
before = # lua
''
function()
vim.g.vimtex_compiler_method = "latexrun"
end
'';
}
];
};
};

}