feat(buffer): add configurable category-level folds

Problem: category folds were hardcoded with no config option, no custom
foldtext, and no vimdoc coverage.

Solution: add `folding` config field (boolean or table with `foldtext`
format string). Default foldtext is `%c (%n tasks)` with automatic
singular/plural. Gate all fold logic on the config so `folding = false`
disables folds entirely. Document the new option in vimdoc.
This commit is contained in:
Barrett Ruth 2026-03-06 20:04:22 -05:00
parent 12b9295c34
commit 03d3ac5851
3 changed files with 84 additions and 6 deletions

View file

@ -236,8 +236,30 @@ local function setup_highlights()
vim.api.nvim_set_hl(0, 'PendingFilter', { link = 'DiagnosticWarn', default = true })
end
---@return string
function M.get_foldtext()
local folding = config.resolve_folding()
if not folding.foldtext then
return vim.fn.foldtext()
end
local line = vim.fn.getline(vim.v.foldstart)
local cat = line:match('^#%s+(.+)$') or line
local task_count = vim.v.foldend - vim.v.foldstart
local icons = config.get().icons
local result = folding.foldtext
:gsub('%%c', cat)
:gsub('%%n', tostring(task_count))
:gsub('(%d+) (%w+)s%)', function(n, word)
if n == '1' then
return n .. ' ' .. word .. ')'
end
return n .. ' ' .. word .. 's)'
end)
return icons.category .. ' ' .. result
end
local function snapshot_folds(bufnr)
if current_view ~= 'category' then
if current_view ~= 'category' or not config.resolve_folding().enabled then
return
end
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
@ -256,7 +278,7 @@ local function snapshot_folds(bufnr)
end
local function restore_folds(bufnr)
if current_view ~= 'category' then
if current_view ~= 'category' or not config.resolve_folding().enabled then
return
end
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
@ -328,12 +350,18 @@ function M.render(bufnr)
setup_syntax(bufnr)
apply_extmarks(bufnr, line_meta)
local folding = config.resolve_folding()
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if current_view == 'category' then
if current_view == 'category' and folding.enabled then
vim.wo[winid].foldmethod = 'expr'
vim.wo[winid].foldexpr = 'v:lua.require("pending.buffer").get_fold()'
vim.wo[winid].foldlevel = 99
vim.wo[winid].foldenable = true
if folding.foldtext then
vim.wo[winid].foldtext = 'v:lua.require("pending.buffer").get_foldtext()'
else
vim.wo[winid].foldtext = 'foldtext()'
end
else
vim.wo[winid].foldmethod = 'manual'
vim.wo[winid].foldenable = false