neovim-confs/lua/lazyvim/util/toggle.lua

77 lines
2.1 KiB
Lua
Raw Normal View History

2024-01-11 11:44:26 +01:00
local Util = require("lazyvim.util")
---@class lazyvim.util.toggle
local M = {}
---@param silent boolean?
---@param values? {[1]:any, [2]:any}
function M.option(option, silent, values)
if values then
if vim.opt_local[option]:get() == values[1] then
---@diagnostic disable-next-line: no-unknown
vim.opt_local[option] = values[2]
else
---@diagnostic disable-next-line: no-unknown
vim.opt_local[option] = values[1]
end
return Util.info("Set " .. option .. " to " .. vim.opt_local[option]:get(), { title = "Option" })
end
---@diagnostic disable-next-line: no-unknown
vim.opt_local[option] = not vim.opt_local[option]:get()
if not silent then
if vim.opt_local[option]:get() then
Util.info("Enabled " .. option, { title = "Option" })
else
Util.warn("Disabled " .. option, { title = "Option" })
end
end
end
local nu = { number = true, relativenumber = true }
function M.number()
if vim.opt_local.number:get() or vim.opt_local.relativenumber:get() then
nu = { number = vim.opt_local.number:get(), relativenumber = vim.opt_local.relativenumber:get() }
vim.opt_local.number = false
vim.opt_local.relativenumber = false
Util.warn("Disabled line numbers", { title = "Option" })
else
vim.opt_local.number = nu.number
vim.opt_local.relativenumber = nu.relativenumber
Util.info("Enabled line numbers", { title = "Option" })
end
end
local enabled = true
function M.diagnostics()
enabled = not enabled
if enabled then
vim.diagnostic.enable()
Util.info("Enabled diagnostics", { title = "Diagnostics" })
else
vim.diagnostic.disable()
Util.warn("Disabled diagnostics", { title = "Diagnostics" })
end
end
---@param buf? number
---@param value? boolean
function M.inlay_hints(buf, value)
local ih = vim.lsp.buf.inlay_hint or vim.lsp.inlay_hint
if type(ih) == "function" then
ih(buf, value)
elseif type(ih) == "table" and ih.enable then
if value == nil then
value = not ih.is_enabled(buf)
end
ih.enable(buf, value)
end
end
setmetatable(M, {
__call = function(m, ...)
return m.option(...)
end,
})
return M