feat(toggle): allow toggling global options. Fixes #534

This commit is contained in:
Folke Lemaitre 2025-01-17 14:41:19 +01:00
parent 770df9e5cf
commit b50effc967
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
5 changed files with 30 additions and 23 deletions

View file

@ -138,7 +138,7 @@ function Toggle:_wk(keys, mode)
end
---@param option string
---@param opts? snacks.toggle.Config | {on?: unknown, off?: unknown}
---@param opts? snacks.toggle.Config | {on?: unknown, off?: unknown, global?: boolean}
function M.option(option, opts)
opts = opts or {}
local on = opts.on == nil and true or opts.on
@ -147,9 +147,16 @@ function M.option(option, opts)
id = option,
name = option,
get = function()
if opts.global then
return vim.opt[option]:get() == on
end
return vim.opt_local[option]:get() == on
end,
set = function(state)
if opts.global then
vim.opt[option] = state and on or off
return
end
vim.opt_local[option] = state and on or off
end,
}, opts)