From 648f988da12286f572785fde76b46cac6c07b5e4 Mon Sep 17 00:00:00 2001 From: psfloyd Date: Tue, 16 Jul 2024 01:02:44 -0300 Subject: [PATCH] plugins/lz-n: init --- plugins/default.nix | 1 + plugins/pluginmanagers/lz-n.nix | 147 ++++++++++++++++++ .../plugins/pluginmanagers/lz-n.nix | 109 +++++++++++++ 3 files changed, 257 insertions(+) create mode 100644 plugins/pluginmanagers/lz-n.nix create mode 100644 tests/test-sources/plugins/pluginmanagers/lz-n.nix diff --git a/plugins/default.nix b/plugins/default.nix index e4e687b6e2..2a69a367d6 100644 --- a/plugins/default.nix +++ b/plugins/default.nix @@ -118,6 +118,7 @@ ./pluginmanagers/packer.nix ./pluginmanagers/lazy.nix + ./pluginmanagers/lz-n.nix ./snippets/friendly-snippets.nix ./snippets/luasnip diff --git a/plugins/pluginmanagers/lz-n.nix b/plugins/pluginmanagers/lz-n.nix new file mode 100644 index 0000000000..d56ec4076e --- /dev/null +++ b/plugins/pluginmanagers/lz-n.nix @@ -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} + ) + ''; + }; +} diff --git a/tests/test-sources/plugins/pluginmanagers/lz-n.nix b/tests/test-sources/plugins/pluginmanagers/lz-n.nix new file mode 100644 index 0000000000..b89e10451f --- /dev/null +++ b/tests/test-sources/plugins/pluginmanagers/lz-n.nix @@ -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 = "ft"; + action = "Neotree toggle"; + options = { + desc = "NeoTree toggle"; + }; + } + { + mode = [ + "n" + "v" + ]; + key = "gft"; + action = "Neotree toggle"; + 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" ]; + } + ]; + }; + }; + +}