feat(buffer): persist fold state across sessions

Problem: folded category headers are lost when Neovim exits because
`_fold_state` only lives in memory. Users must re-fold categories
every session.

Solution: store folded category names in the JSON data file as a
top-level `folded_categories` field. On first render, `restore_folds`
seeds from the store instead of the empty in-memory state. Folds are
persisted on `M.close()` and `VimLeavePre`.
This commit is contained in:
Barrett Ruth 2026-03-07 20:17:10 -05:00
parent 26d43688d0
commit 5a4cc7f8a1
4 changed files with 121 additions and 1 deletions

View file

@ -20,6 +20,7 @@ local config = require('pending.config')
---@field next_id integer
---@field tasks pending.Task[]
---@field undo pending.Task[][]
---@field folded_categories string[]
---@class pending.Store
---@field path string
@ -39,6 +40,7 @@ local function empty_data()
next_id = 1,
tasks = {},
undo = {},
folded_categories = {},
}
end
@ -171,6 +173,7 @@ function Store:load()
next_id = decoded.next_id or 1,
tasks = {},
undo = {},
folded_categories = decoded.folded_categories or {},
}
for _, t in ipairs(decoded.tasks or {}) do
table.insert(self._data.tasks, table_to_task(t))
@ -199,6 +202,7 @@ function Store:save()
next_id = self._data.next_id,
tasks = {},
undo = {},
folded_categories = self._data.folded_categories,
}
for _, task in ipairs(self._data.tasks) do
table.insert(out.tasks, task_to_table(task))
@ -371,6 +375,17 @@ function Store:set_next_id(id)
self:data().next_id = id
end
---@return string[]
function Store:get_folded_categories()
return self:data().folded_categories
end
---@param cats string[]
---@return nil
function Store:set_folded_categories(cats)
self:data().folded_categories = cats
end
---@return nil
function Store:unload()
self._data = nil