From e2686015ec3ebb3185ad00d226db6bbd692bb497 Mon Sep 17 00:00:00 2001 From: Caio Daniel Nunes Santos Date: Fri, 16 May 2025 10:53:16 -0300 Subject: [PATCH 1/2] Disambiguate field "keys" from method with the same name. The method "keys" raised an error that I was trying to call a table. I just renamed the field "keys" to "mappings'. --- lua/strive/init.lua | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lua/strive/init.lua b/lua/strive/init.lua index afaf03c..130db6e 100644 --- a/lua/strive/init.lua +++ b/lua/strive/init.lua @@ -607,7 +607,7 @@ function Plugin.new(spec) events = {}, -- Events to trigger loading filetypes = {}, -- Filetypes to trigger loading commands = {}, -- Commands to trigger loading - keys = {}, -- Keys to trigger loading + mappings = {}, -- Keys to trigger loading -- Configuration setup_opts = spec.setup or {}, -- Options for plugin setup() @@ -924,9 +924,9 @@ end -- Set up lazy loading for specific keymaps function Plugin:keys(mappings) self.is_lazy = true - self.keys = type(mappings) ~= 'table' and { mappings } or mappings + self.mappings = type(mappings) ~= 'table' and { mappings } or mappings - for _, mapping in ipairs(self.keys) do + for _, mapping in ipairs(self.mappings) do local mode, lhs, rhs, opts if type(mapping) == 'table' then From db3dba4e942069b75012c07faefeea7babb98dc9 Mon Sep 17 00:00:00 2001 From: Caio Daniel Nunes Santos Date: Fri, 16 May 2025 11:30:52 -0300 Subject: [PATCH 2/2] Allow mapping to be created inside plugin setup, but still specified as a trigger to load the plugin. --- lua/strive/init.lua | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lua/strive/init.lua b/lua/strive/init.lua index 130db6e..777f29c 100644 --- a/lua/strive/init.lua +++ b/lua/strive/init.lua @@ -932,21 +932,26 @@ function Plugin:keys(mappings) if type(mapping) == 'table' then mode = mapping[1] or 'n' lhs = mapping[2] - rhs = mapping[3] or function() end + rhs = mapping[3] opts = mapping[4] or {} else mode, lhs = 'n', mapping - rhs = function() end opts = {} end -- Create a keymap that loads the plugin first vim.keymap.set(mode, lhs, function() - if self:load() and type(rhs) == 'function' then - rhs() - elseif self:load() and type(rhs) == 'string' then - -- If rhs is a string command - vim.cmd(rhs) + if type(rhs) == 'function' then + self:load(nil, rhs) + elseif type(rhs) == 'string' then + self:load(nil, function() vim.cmd(rhs) end) + elseif type(rhs) == 'nil' then + -- If rhs not specified, it should be defined in plugin config + -- In this case, we need to pass a callback + self:load(nil, function() + vim.schedule(function() + vim.fn.feedkeys(lhs) + end) end) end end, opts) end