Skip to content

Commit

Permalink
plugins/lz-n: init
Browse files Browse the repository at this point in the history
  • Loading branch information
psfloyd committed Jul 16, 2024
1 parent 3fc6938 commit 648f988
Show file tree
Hide file tree
Showing 3 changed files with 257 additions and 0 deletions.
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
147 changes: 147 additions & 0 deletions plugins/pluginmanagers/lz-n.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
{
lib,
helpers,
config,
pkgs,
...
}:
let
inherit (lib) mkOption;
inherit (helpers)
mkNullOrLuaFn'
nixvimTypes
keymaps
defaultNullOpts
;

name = "lz-n";
originalName = "lz.n";

mkLazyLoadOption =
{
originalName ? "this plugin",
}:
let
pluginDefault = {
name = originalName;
};
in
mkOption {
description = "Lazy-load settings for ${originalName}.";
type =
with nixvimTypes;
submodule {
options = with defaultNullOpts; {
name = mkOption {
type = str;
default = pluginDefault.name or null;
description = "The plugin's name (not the module name). This is what is passed to the load(name) function.";
};
enabledInSpec = mkStrLuaFnOr bool pluginDefault.enabledInSpec or null ''
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 pluginDefault.beforeAll
or null "Always executed before any plugins are loaded.";
before = mkLuaFn pluginDefault.before or null "Executed before ${originalName} is loaded.";
after = mkLuaFn pluginDefault.after or null "Executed after ${originalName} is loaded.";
event =
mkNullable (listOf str) pluginDefault.event or null
"Lazy-load on event. Events can be specified as BufEnter or with a pattern like BufEnter *.lua";
cmd = mkNullable (listOf str) pluginDefault.cmd or null "Lazy-load on command.";
ft = mkNullable (listOf str) pluginDefault.ft or null "Lazy-load on filetype.";
keys = mkNullable (listOf keymaps.mapOptionSubmodule) pluginDefault.keys
or null "Lazy-load on key mapping. Use the same format as `config.keymaps`.";
colorscheme = mkNullable (listOf str) pluginDefault.colorscheme or null "Lazy-load on colorscheme.";
priority = mkNullable number pluginDefault.priority or null ''
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 pluginDefault.load
or null "Can be used to override the vim.g.lz_n.load() function for ${originalName}.";
};
};
default = pluginDefault;
};
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

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 = plugin.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}
)
'';
};
}
109 changes: 109 additions & 0 deletions tests/test-sources/plugins/pluginmanagers/lz-n.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
{ 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
vimtex
telescope-nvim
nvim-biscuits
onedarker-nvim
];
plugins.treesitter.enable = true;
plugins.lz-n = {
enable = true;
plugins = [
# enabledInSpec, on keys
{
name = "neo-tree.nvim";
enabledInSpec = ''
function()
return false
end
'';
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
'';
}
# beforeAll, before, on filetype
{
name = "vimtex";
ft = [ "plaintex" ];
beforeAll = # lua
''
function()
vim.g.vimtex_compiler_method = "latexrun"
end
'';
before = # lua
''
function()
vim.g.vimtex_compiler_method = "latexmk"
end
'';
}
# On event
{
name = "nvim-biscuits";
event = [ "BufEnter *.lua" ];
after = ''
function()
require('nvim-biscuits').setup({})
end
'';
}
# On command no setup function, priority
{
name = "telescope.nvim";
cmd = [ "Telescope" ];
priority = 500;
}
# On colorschme
{
name = "onedarker.nvim";
colorscheme = [ "onedarker" ];
}
];
};
};

}

0 comments on commit 648f988

Please sign in to comment.