Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(api): Fix toolbox functions to work if items are deepcopied #438

Merged
merged 2 commits into from
Mar 22, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 11 additions & 15 deletions lua/legendary/toolbox.lua
Original file line number Diff line number Diff line change
Expand Up @@ -84,58 +84,54 @@ 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)
-- inline require to avoid circular dependency
return keymap.class == require('legendary.data.keymap')
return check_class(keymap, '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 check_class(cmd, '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 check_class(au, '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 check_class(autocmd, '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 check_class(group, '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 check_class(func, 'Function')
end

---Check if the given mode string indicates a visual mode or a sub-mode of visual mode.
Expand Down
Loading