111 lines
3.2 KiB
Lua
111 lines
3.2 KiB
Lua
-- UI toggles under <leader>u. The chosen state is saved to stdpath("state")/ui_toggles.json
|
|
-- and applied again on the next start. Called from the snacks config in plugins/picker.lua.
|
|
local M = {}
|
|
|
|
function M.setup()
|
|
local toggle = require("snacks").toggle
|
|
local state_path = vim.fn.stdpath("state") .. "/ui_toggles.json"
|
|
local state = {}
|
|
|
|
local ok, lines = pcall(vim.fn.readfile, state_path)
|
|
if ok then
|
|
local decoded_ok, decoded = pcall(vim.json.decode, table.concat(lines, "\n"))
|
|
if decoded_ok and type(decoded) == "table" then
|
|
state = decoded
|
|
end
|
|
end
|
|
|
|
local function save()
|
|
vim.fn.mkdir(vim.fs.dirname(state_path), "p")
|
|
vim.fn.writefile({ vim.json.encode(state) }, state_path)
|
|
end
|
|
|
|
-- Wrap the toggle's setter so every change is stored, restore the saved value, then map it.
|
|
local function persist(t, lhs)
|
|
local set = t.opts.set
|
|
t.opts.set = function(value)
|
|
set(value)
|
|
state[t.opts.id] = value
|
|
save()
|
|
end
|
|
if state[t.opts.id] ~= nil then
|
|
t:set(state[t.opts.id])
|
|
end
|
|
t:map(lhs)
|
|
end
|
|
|
|
-- Window options are per window, so apply the saved values to every new window.
|
|
local function apply_window_state()
|
|
if state.spell ~= nil then
|
|
vim.wo.spell = state.spell
|
|
end
|
|
if state.wrap ~= nil then
|
|
vim.wo.wrap = state.wrap
|
|
end
|
|
if state.line_number ~= nil then
|
|
vim.wo.number = state.line_number
|
|
vim.wo.relativenumber = state.line_number and state.relativenumber ~= false or false
|
|
elseif state.relativenumber ~= nil then
|
|
vim.wo.relativenumber = state.relativenumber
|
|
end
|
|
end
|
|
|
|
persist(toggle.option("spell", { name = "Spelling" }), "<leader>us")
|
|
persist(toggle.option("wrap", { name = "Wrap" }), "<leader>uw")
|
|
persist(toggle.option("relativenumber", { name = "Relative number" }), "<leader>uL")
|
|
persist(toggle.line_number(), "<leader>ul")
|
|
persist(toggle.diagnostics(), "<leader>ud")
|
|
persist(
|
|
toggle({
|
|
id = "diagnostic_virtual_text",
|
|
name = "Diagnostic virtual text",
|
|
get = function()
|
|
return vim.diagnostic.config().virtual_text ~= false
|
|
end,
|
|
set = function(enabled)
|
|
vim.diagnostic.config({
|
|
virtual_text = enabled and { severity = { min = vim.diagnostic.severity.WARN } } or false,
|
|
})
|
|
end,
|
|
}),
|
|
"<leader>uv"
|
|
)
|
|
persist(toggle.treesitter(), "<leader>uT")
|
|
persist(toggle.inlay_hints(), "<leader>ui")
|
|
persist(
|
|
toggle({
|
|
id = "indent_guides",
|
|
name = "Indent guides",
|
|
get = function()
|
|
return not vim.g.miniindentscope_disable
|
|
end,
|
|
set = function(enabled)
|
|
vim.g.miniindentscope_disable = not enabled
|
|
end,
|
|
}),
|
|
"<leader>ug"
|
|
)
|
|
|
|
apply_window_state()
|
|
local group = vim.api.nvim_create_augroup("core.toggles", { clear = true })
|
|
vim.api.nvim_create_autocmd({ "BufWinEnter", "WinNew" }, {
|
|
group = group,
|
|
callback = function(ev)
|
|
apply_window_state()
|
|
if state.treesitter == false then
|
|
pcall(vim.treesitter.stop, ev.buf)
|
|
end
|
|
end,
|
|
})
|
|
vim.api.nvim_create_autocmd("LspAttach", {
|
|
group = group,
|
|
callback = function(ev)
|
|
if state.inlay_hints ~= nil then
|
|
vim.lsp.inlay_hint.enable(state.inlay_hints, { bufnr = ev.buf })
|
|
end
|
|
end,
|
|
})
|
|
end
|
|
|
|
return M
|