From 77cc1191457bff2ebccd9b8205438057de1825e4 Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Thu, 21 Mar 2024 12:25:54 -0400 Subject: [PATCH 1/2] fix(api): Fix toolbox functions to work if items are deepcopied --- lua/legendary/toolbox.lua | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) diff --git a/lua/legendary/toolbox.lua b/lua/legendary/toolbox.lua index 79bf65bf..d8c69667 100644 --- a/lua/legendary/toolbox.lua +++ b/lua/legendary/toolbox.lua @@ -88,54 +88,46 @@ end ---@param keymap LegendaryItem ---@return boolean function M.is_keymap(keymap) - -- inline require to avoid circular dependency - return keymap.class == require('legendary.data.keymap') + return keymap.class.name == 'Keymap' end ---Check if an item is a Command ---@param cmd LegendaryItem ---@return boolean function M.is_command(cmd) - -- inline require to avoid circular dependency - return cmd.class == require('legendary.data.command') + return cmd.class.name == 'Command' end ---Check if an item is an Augroup ---@param au LegendaryItem ---@return boolean function M.is_augroup(au) - -- inline require to avoid circular dependency - return au.class == require('legendary.data.augroup') + return au.class.name == 'Augroup' end ---Check if an item is an Autocmd ---@param autocmd LegendaryItem ---@return boolean function M.is_autocmd(autocmd) - -- inline require to avoid circular dependency - return autocmd.class == require('legendary.data.autocmd') + return autocmd.class.name == 'Autocmd' end ---Check if an item is an Augroup or Autocmd ---@param au_or_autocmd LegendaryItem ---@return boolean function M.is_augroup_or_autocmd(au_or_autocmd) - -- inline require to avoid circular dependency - return au_or_autocmd.class == require('legendary.data.augroup') - or au_or_autocmd.class == require('legendary.data.autocmd') + return M.is_autocmd(au_or_autocmd) or M.is_augroup(au_or_autocmd) end function M.is_itemgroup(group) - -- inline require to avoid circular dependency - return group.class == require('legendary.data.itemgroup') + return group.class.name == 'ItemGroup' end ---Check if an item is a Function ---@param func LegendaryItem ---@return boolean function M.is_function(func) - -- inline require to avoid circular dependency - return func.class == require('legendary.data.function') + return func.class.name == 'Function' end ---Check if the given mode string indicates a visual mode or a sub-mode of visual mode. From 3b07f50d680b63b582ca5cdd9e3911a57e7cde83 Mon Sep 17 00:00:00 2001 From: Mat Jones Date: Thu, 21 Mar 2024 12:29:44 -0400 Subject: [PATCH 2/2] fix: use vim.tbl_get for safety --- lua/legendary/toolbox.lua | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/lua/legendary/toolbox.lua b/lua/legendary/toolbox.lua index d8c69667..d00476aa 100644 --- a/lua/legendary/toolbox.lua +++ b/lua/legendary/toolbox.lua @@ -84,32 +84,36 @@ function M.vsplit_then(fn) end end +local function check_class(item, class) + return vim.tbl_get(item or {}, 'class', 'name') == class +end + ---Check if an item is a Keymap ---@param keymap LegendaryItem ---@return boolean function M.is_keymap(keymap) - return keymap.class.name == 'Keymap' + return check_class(keymap, 'Keymap') end ---Check if an item is a Command ---@param cmd LegendaryItem ---@return boolean function M.is_command(cmd) - return cmd.class.name == 'Command' + return check_class(cmd, 'Command') end ---Check if an item is an Augroup ---@param au LegendaryItem ---@return boolean function M.is_augroup(au) - return au.class.name == 'Augroup' + return check_class(au, 'Augroup') end ---Check if an item is an Autocmd ---@param autocmd LegendaryItem ---@return boolean function M.is_autocmd(autocmd) - return autocmd.class.name == 'Autocmd' + return check_class(autocmd, 'Autocmd') end ---Check if an item is an Augroup or Autocmd @@ -120,14 +124,14 @@ function M.is_augroup_or_autocmd(au_or_autocmd) end function M.is_itemgroup(group) - return group.class.name == 'ItemGroup' + return check_class(group, 'ItemGroup') end ---Check if an item is a Function ---@param func LegendaryItem ---@return boolean function M.is_function(func) - return func.class.name == 'Function' + return check_class(func, 'Function') end ---Check if the given mode string indicates a visual mode or a sub-mode of visual mode.