feat(buffer): add configurable category-level folds (#91)
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:
parent
012bd9b043
commit
d8324e6a6d
3 changed files with 84 additions and 6 deletions
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -1,3 +1,10 @@
|
|||
---@class pending.FoldingConfig
|
||||
---@field foldtext? string|false
|
||||
|
||||
---@class pending.ResolvedFolding
|
||||
---@field enabled boolean
|
||||
---@field foldtext string|false
|
||||
|
||||
---@class pending.Icons
|
||||
---@field pending string
|
||||
---@field done string
|
||||
|
|
@ -55,6 +62,7 @@
|
|||
---@field drawer_height? integer
|
||||
---@field debug? boolean
|
||||
---@field keymaps pending.Keymaps
|
||||
---@field folding? boolean|pending.FoldingConfig
|
||||
---@field sync? pending.SyncConfig
|
||||
---@field icons pending.Icons
|
||||
|
||||
|
|
@ -70,6 +78,7 @@ local defaults = {
|
|||
date_syntax = 'due',
|
||||
recur_syntax = 'rec',
|
||||
someday_date = '9999-12-30',
|
||||
folding = true,
|
||||
category_order = {},
|
||||
keymaps = {
|
||||
close = 'q',
|
||||
|
|
@ -119,4 +128,15 @@ function M.reset()
|
|||
_resolved = nil
|
||||
end
|
||||
|
||||
---@return pending.ResolvedFolding
|
||||
function M.resolve_folding()
|
||||
local raw = M.get().folding
|
||||
if raw == false then
|
||||
return { enabled = false, foldtext = false }
|
||||
elseif raw == true or raw == nil then
|
||||
return { enabled = true, foldtext = '%c (%n tasks)' }
|
||||
end
|
||||
return { enabled = true, foldtext = raw.foldtext or false }
|
||||
end
|
||||
|
||||
return M
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue