From bf11bbce319f36df658d47ab114d626dcb8a98fb Mon Sep 17 00:00:00 2001 From: Price Hiller Date: Fri, 22 Dec 2023 11:33:13 -0600 Subject: [PATCH] refactor(nvim): clean up ufo foldlevel logic --- .../nvim/lua/plugins/configs/nvim-ufo.lua | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/dots/.config/nvim/lua/plugins/configs/nvim-ufo.lua b/dots/.config/nvim/lua/plugins/configs/nvim-ufo.lua index ecf0ff72..7aaeb54b 100644 --- a/dots/.config/nvim/lua/plugins/configs/nvim-ufo.lua +++ b/dots/.config/nvim/lua/plugins/configs/nvim-ufo.lua @@ -1,23 +1,27 @@ vim.api.nvim_create_autocmd("BufReadPre", { callback = function() vim.b.ufo_foldlevel = 0 - end, + end }) -local change_buf_foldlevel_by = function(num) - 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 - +---@param num integer Set the fold level to this number local set_buf_foldlevel = function(num) vim.b.ufo_foldlevel = num require("ufo").closeFoldsWith(num) 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 { { "kevinhwang91/nvim-ufo", @@ -47,7 +51,7 @@ return { if count == 0 then count = nil end - change_buf_foldlevel_by(count or -1) + change_buf_foldlevel_by(-(count) or -1) end, desc = "UFO: Fold Less", },