fix(toggle): safe toggle get

This commit is contained in:
Folke Lemaitre 2024-07-20 22:06:16 +02:00
parent 0d561a3226
commit c8ab5d7554
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040

View file

@ -39,14 +39,21 @@ function M.wk(lhs, toggle)
if not LazyVim.has("which-key.nvim") then if not LazyVim.has("which-key.nvim") then
return return
end end
local function safe_get()
local ok, enabled = pcall(toggle.get)
if not ok then
LazyVim.error({ "Failed to get toggle state for **" .. toggle.name .. "**:\n", enabled }, { once = true })
end
return enabled
end
require("which-key").add({ require("which-key").add({
{ {
lhs, lhs,
icon = function() icon = function()
return toggle.get() and { icon = "", color = "green" } or { icon = "", color = "yellow" } return safe_get() and { icon = "", color = "green" } or { icon = "", color = "yellow" }
end, end,
desc = function() desc = function()
return (toggle.get() and "Disable " or "Enable ") .. toggle.name return (safe_get() and "Disable " or "Enable ") .. toggle.name
end, end,
}, },
}) })