fix(toggle): safe toggle get

This commit is contained in:
Folke Lemaitre
2024-07-20 22:06:16 +02:00
parent 0d561a3226
commit c8ab5d7554

View File

@ -39,14 +39,21 @@ function M.wk(lhs, toggle)
if not LazyVim.has("which-key.nvim") then
return
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({
{
lhs,
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,
desc = function()
return (toggle.get() and "Disable " or "Enable ") .. toggle.name
return (safe_get() and "Disable " or "Enable ") .. toggle.name
end,
},
})