From 03a0561a8fae438cb19377a0223963fff255e937 Mon Sep 17 00:00:00 2001 From: Heitor Augusto Date: Thu, 8 May 2025 17:34:03 -0300 Subject: [PATCH] plugins/lz-n: add support for keymap().set API Add a new `keymaps` option to the lz-n plugin that allows users to define keymaps that use lz.n's keymap().set functionality. This enables users to create keymaps that lazy-load plugins when specific keys are pressed, using the familiar vim.keymap.set() API style. The implementation: - Adds a new `keymaps` option using mkMapOptionSubmodule with a custom plugin property - Exposes a simple and consistent interface for configuring lazy-loaded keymaps - Processes these keymaps to generate corresponding Lua code with require('lz.n').keymap().set() - Cleanly separates plugin specs from keymap definitions while preserving their relationship --- plugins/pluginmanagers/lz-n.nix | 58 ++++++++++++++++++++++++++++++--- 1 file changed, 53 insertions(+), 5 deletions(-) diff --git a/plugins/pluginmanagers/lz-n.nix b/plugins/pluginmanagers/lz-n.nix index 4c7068ec..7aff86b8 100644 --- a/plugins/pluginmanagers/lz-n.nix +++ b/plugins/pluginmanagers/lz-n.nix @@ -7,16 +7,19 @@ let inherit (lib) id literalMD - mkIf + mkMerge mkOption + optional types ; inherit (lib.nixvim) defaultNullOpts mkNullOrLuaFn mkNullOrOption' + nestedLiteralLua toLuaObject ; + inherit (lib.nixvim.keymaps) mkMapOptionSubmodule; in lib.nixvim.plugins.mkNeovimPlugin { name = "lz-n"; @@ -146,6 +149,40 @@ lib.nixvim.plugins.mkNeovimPlugin { }; in { + keymaps = mkOption { + type = types.listOf (mkMapOptionSubmodule { + extraOptions.plugin = mkOption { + type = with types; either str lzPluginType; + example = "telescope.nvim"; + description = '' + The plugin (name or spec) to lazy-load when the keymap is triggered. + + > [!NOTE] + > This must match the name used in the `plugins` list or be a valid plugin that can be loaded. + ''; + }; + }); + default = [ ]; + example = [ + { + plugin = "telescope.nvim"; + key = "ff"; + action = nestedLiteralLua "function() require('telescope.builtin').find_files() end"; + options.desc = "Find files"; + } + { + plugin = "neo-tree.nvim"; + key = "ft"; + action = "Neotree toggle"; + options.desc = "NeoTree toggle"; + } + ]; + description = '' + Define keymaps that will use lz.n's `keymap().set` functionality. + This provides a more intuitive way to define lazy-loaded keymaps with the familiar `keymaps` option API style. + ''; + }; + plugins = mkOption { description = '' List of plugin specs provided to the `require('lz.n').load` function. @@ -158,7 +195,7 @@ lib.nixvim.plugins.mkNeovimPlugin { __unkeyed-1 = "neo-tree.nvim"; enabled = '' function() - return true + return true end ''; keys = [ @@ -208,8 +245,19 @@ lib.nixvim.plugins.mkNeovimPlugin { extraConfig = cfg: { globals.lz_n = lib.modules.mkAliasAndWrapDefsWithPriority id options.plugins.lz-n.settings; - plugins.lz-n.luaConfig.content = mkIf (cfg.plugins != [ ]) '' - require('lz.n').load( ${toLuaObject cfg.plugins}) - ''; + plugins.lz-n.luaConfig.content = mkMerge ( + optional (cfg.plugins != [ ]) "require('lz.n').load(${toLuaObject cfg.plugins})" + ++ map ( + { + plugin, + mode, + key, + action, + options, + ... + }: + "require('lz.n').keymap(${toLuaObject plugin}).set(${toLuaObject mode}, ${toLuaObject key}, ${toLuaObject action}, ${toLuaObject options})" + ) cfg.keymaps + ); }; }