refactor(nvim): clean up ufo foldlevel logic

This commit is contained in:
Price Hiller 2023-12-22 11:33:13 -06:00
parent 3a833516a8
commit bf11bbce31
Signed by: Price
SSH Key Fingerprint: SHA256:Y4S9ZzYphRn1W1kbJerJFO6GGsfu9O70VaBSxJO7dF8

View File

@ -1,23 +1,27 @@
vim.api.nvim_create_autocmd("BufReadPre", { vim.api.nvim_create_autocmd("BufReadPre", {
callback = function() callback = function()
vim.b.ufo_foldlevel = 0 vim.b.ufo_foldlevel = 0
end, end
}) })
local change_buf_foldlevel_by = function(num) ---@param num integer Set the fold level to this number
local foldlevel = vim.b.ufo_foldlevel or 0
if foldlevel + num >= 0 then
foldlevel = foldlevel + num
end
vim.b.ufo_foldlevel = foldlevel
require("ufo").closeFoldsWith(foldlevel)
end
local set_buf_foldlevel = function(num) local set_buf_foldlevel = function(num)
vim.b.ufo_foldlevel = num vim.b.ufo_foldlevel = num
require("ufo").closeFoldsWith(num) require("ufo").closeFoldsWith(num)
end end
---@param num integer The amount to change the UFO fold level by
local change_buf_foldlevel_by = function(num)
local foldlevel = vim.b.ufo_foldlevel or 0
-- Ensure the foldlevel can't be set negatively
if foldlevel + num >= 0 then
foldlevel = foldlevel + num
else
foldlevel = 0
end
set_buf_foldlevel(foldlevel)
end
return { return {
{ {
"kevinhwang91/nvim-ufo", "kevinhwang91/nvim-ufo",
@ -47,7 +51,7 @@ return {
if count == 0 then if count == 0 then
count = nil count = nil
end end
change_buf_foldlevel_by(count or -1) change_buf_foldlevel_by(-(count) or -1)
end, end,
desc = "UFO: Fold Less", desc = "UFO: Fold Less",
}, },