* refactor(store): convert singleton to Store.new() factory
Problem: store.lua used module-level _data singleton, making
project-local stores impossible and creating hidden global state.
Solution: introduce Store metatable with all operations as instance
methods. M.new(path) constructs an instance; M.resolve_path()
searches upward for .pending.json and falls back to
config.get().data_path. Singleton module API is removed.
* refactor(diff): accept store instance as parameter
Problem: diff.apply called store singleton methods directly, coupling
it to global state and preventing use with project-local stores.
Solution: change signature to apply(lines, s, hidden_ids?) where s is
a pending.Store instance. All store operations now go through s.
* refactor(buffer): add set_store/store accessors, drop singleton dep
Problem: buffer.lua imported store directly and called singleton
methods, preventing it from working with per-project store instances.
Solution: add module-level _store, M.set_store(s), and M.store()
accessors. open() and render() use _store instead of the singleton.
init.lua will call buffer.set_store(s) before buffer.open().
* refactor(complete,health,sync,plugin): update callers to store instance API
Problem: complete.lua, health.lua, sync/gcal.lua, and plugin/pending.lua
all called singleton store methods directly.
Solution: complete.lua uses buffer.store() for category lookups;
health.lua uses store.new(store.resolve_path()) and reports the
resolved path; gcal.lua calls require('pending').store() for task
access; plugin tab-completion creates ephemeral store instances via
store.new(store.resolve_path()). Add 'init' to the subcommands list.
* feat(init): thread Store instance through init, add :Pending init
Problem: init.lua called singleton store methods throughout, and there
was no way to create a project-local .pending.json file.
Solution: add module-level _store and private get_store() that
lazy-constructs via store.new(store.resolve_path()). Add public
M.store() accessor used by specs and sync backends. M.open() calls
buffer.set_store(get_store()) before buffer.open(). All store
callsites converted to get_store():method(). goto_file() and
add_here() derive the data directory from get_store().path.
Add M.init() which creates .pending.json in cwd and dispatches from
M.command() as ':Pending init'.
* test: update all specs for Store instance API
Problem: every spec used the old singleton API (store.unload(),
store.load(), store.add(), etc.) and diff.apply(lines, hidden).
Solution: lower-level specs (store, diff, views, complete, file) use
s = store.new(path); s:load() directly. Higher-level specs (archive,
edit, filter, status, sync) reset package.loaded['pending'] in
before_each and use pending.store() to access the live instance.
diff.apply calls updated to diff.apply(lines, s, hidden_ids).
* docs(pending): document :Pending init and store resolution
Add *pending-store-resolution* section explaining upward .pending.json
discovery and fallback to the global data_path. Document :Pending init
under COMMANDS. Add a cross-reference from the data_path config field.
* ci: format
* ci: remove unused variable
386 lines
10 KiB
Lua
386 lines
10 KiB
Lua
local config = require('pending.config')
|
|
local views = require('pending.views')
|
|
|
|
---@class pending.buffer
|
|
local M = {}
|
|
|
|
---@type pending.Store?
|
|
local _store = nil
|
|
|
|
---@type integer?
|
|
local task_bufnr = nil
|
|
---@type integer?
|
|
local task_winid = nil
|
|
local task_ns = vim.api.nvim_create_namespace('pending')
|
|
---@type 'category'|'priority'|nil
|
|
local current_view = nil
|
|
---@type pending.LineMeta[]
|
|
local _meta = {}
|
|
---@type table<integer, table<string, boolean>>
|
|
local _fold_state = {}
|
|
---@type string[]
|
|
local _filter_predicates = {}
|
|
---@type table<integer, true>
|
|
local _hidden_ids = {}
|
|
|
|
---@return pending.LineMeta[]
|
|
function M.meta()
|
|
return _meta
|
|
end
|
|
|
|
---@return integer?
|
|
function M.bufnr()
|
|
return task_bufnr
|
|
end
|
|
|
|
---@return integer?
|
|
function M.winid()
|
|
return task_winid
|
|
end
|
|
|
|
---@return string?
|
|
function M.current_view_name()
|
|
return current_view
|
|
end
|
|
|
|
---@param s pending.Store?
|
|
---@return nil
|
|
function M.set_store(s)
|
|
_store = s
|
|
end
|
|
|
|
---@return pending.Store?
|
|
function M.store()
|
|
return _store
|
|
end
|
|
|
|
---@return string[]
|
|
function M.filter_predicates()
|
|
return _filter_predicates
|
|
end
|
|
|
|
---@return table<integer, true>
|
|
function M.hidden_ids()
|
|
return _hidden_ids
|
|
end
|
|
|
|
---@param predicates string[]
|
|
---@param hidden table<integer, true>
|
|
---@return nil
|
|
function M.set_filter(predicates, hidden)
|
|
_filter_predicates = predicates
|
|
_hidden_ids = hidden
|
|
end
|
|
|
|
---@return nil
|
|
function M.clear_winid()
|
|
task_winid = nil
|
|
end
|
|
|
|
---@return nil
|
|
function M.close()
|
|
if not task_winid or not vim.api.nvim_win_is_valid(task_winid) then
|
|
task_winid = nil
|
|
return
|
|
end
|
|
local wins = vim.api.nvim_list_wins()
|
|
if #wins == 1 then
|
|
vim.cmd.enew()
|
|
else
|
|
vim.api.nvim_win_close(task_winid, false)
|
|
end
|
|
task_winid = nil
|
|
end
|
|
|
|
---@param bufnr integer
|
|
local function set_buf_options(bufnr)
|
|
vim.bo[bufnr].buftype = 'acwrite'
|
|
vim.bo[bufnr].bufhidden = 'hide'
|
|
vim.bo[bufnr].swapfile = false
|
|
vim.bo[bufnr].filetype = 'pending'
|
|
vim.bo[bufnr].modifiable = true
|
|
vim.bo[bufnr].omnifunc = 'v:lua.require("pending.complete").omnifunc'
|
|
end
|
|
|
|
---@param winid integer
|
|
local function set_win_options(winid)
|
|
vim.wo[winid].conceallevel = 3
|
|
vim.wo[winid].concealcursor = 'nvic'
|
|
vim.wo[winid].winfixheight = true
|
|
end
|
|
|
|
---@param bufnr integer
|
|
local function setup_syntax(bufnr)
|
|
vim.api.nvim_buf_call(bufnr, function()
|
|
vim.cmd([[
|
|
syntax clear
|
|
syntax match taskId /^\/\d\+\// conceal
|
|
syntax match taskHeader /^## .*$/ contains=taskId
|
|
syntax match taskCheckbox /\[!\]/ contained containedin=taskLine
|
|
syntax match taskLine /^\/\d\+\/- \[.\] .*$/ contains=taskId,taskCheckbox
|
|
]])
|
|
end)
|
|
end
|
|
|
|
---@param above boolean
|
|
---@return nil
|
|
function M.open_line(above)
|
|
local bufnr = task_bufnr
|
|
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then
|
|
return
|
|
end
|
|
local row = vim.api.nvim_win_get_cursor(0)[1]
|
|
local insert_row = above and (row - 1) or row
|
|
vim.bo[bufnr].modifiable = true
|
|
vim.api.nvim_buf_set_lines(bufnr, insert_row, insert_row, false, { '- [ ] ' })
|
|
vim.api.nvim_win_set_cursor(0, { insert_row + 1, 6 })
|
|
vim.cmd('startinsert!')
|
|
end
|
|
|
|
---@return string
|
|
function M.get_fold()
|
|
local lnum = vim.v.lnum
|
|
local m = _meta[lnum]
|
|
if not m then
|
|
return '0'
|
|
end
|
|
if m.type == 'header' then
|
|
return '>1'
|
|
elseif m.type == 'task' then
|
|
return '1'
|
|
else
|
|
return '0'
|
|
end
|
|
end
|
|
|
|
---@param bufnr integer
|
|
---@param line_meta pending.LineMeta[]
|
|
local function apply_extmarks(bufnr, line_meta)
|
|
local icons = config.get().icons
|
|
vim.api.nvim_buf_clear_namespace(bufnr, task_ns, 0, -1)
|
|
for i, m in ipairs(line_meta) do
|
|
local row = i - 1
|
|
if m.type == 'filter' then
|
|
local line = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or ''
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
|
|
end_col = #line,
|
|
hl_group = 'PendingFilter',
|
|
})
|
|
elseif m.type == 'task' then
|
|
local due_hl = m.overdue and 'PendingOverdue' or 'PendingDue'
|
|
local virt_parts = {}
|
|
if m.show_category and m.category then
|
|
table.insert(virt_parts, { icons.category .. ' ' .. m.category, 'PendingHeader' })
|
|
end
|
|
if m.recur then
|
|
table.insert(virt_parts, { icons.recur .. ' ' .. m.recur, 'PendingRecur' })
|
|
end
|
|
if m.due then
|
|
table.insert(virt_parts, { icons.due .. ' ' .. m.due, due_hl })
|
|
end
|
|
if m.file then
|
|
local display = m.file:match('([^/]+:%d+)$') or m.file
|
|
table.insert(virt_parts, { display, 'PendingFile' })
|
|
end
|
|
if #virt_parts > 0 then
|
|
for p = 1, #virt_parts - 1 do
|
|
virt_parts[p][1] = virt_parts[p][1] .. ' '
|
|
end
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
|
|
virt_text = virt_parts,
|
|
virt_text_pos = 'eol',
|
|
})
|
|
end
|
|
if m.status == 'done' then
|
|
local line = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or ''
|
|
local col_start = line:find('/%d+/') and select(2, line:find('/%d+/')) or 0
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, col_start, {
|
|
end_col = #line,
|
|
hl_group = 'PendingDone',
|
|
})
|
|
end
|
|
local line = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or ''
|
|
local bracket_col = (line:find('%[') or 1) - 1
|
|
local icon, icon_hl
|
|
if m.status == 'done' then
|
|
icon, icon_hl = icons.done, 'PendingDone'
|
|
elseif m.priority and m.priority > 0 then
|
|
icon, icon_hl = icons.priority, 'PendingPriority'
|
|
else
|
|
icon, icon_hl = icons.pending, 'Normal'
|
|
end
|
|
local icon_padded = icon .. ' '
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, bracket_col, {
|
|
virt_text = { { icon_padded, icon_hl } },
|
|
virt_text_pos = 'overlay',
|
|
priority = 100,
|
|
})
|
|
elseif m.type == 'header' then
|
|
local line = vim.api.nvim_buf_get_lines(bufnr, row, row + 1, false)[1] or ''
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
|
|
end_col = #line,
|
|
hl_group = 'PendingHeader',
|
|
})
|
|
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
|
|
virt_text = { { icons.header .. ' ', 'PendingHeader' } },
|
|
virt_text_pos = 'overlay',
|
|
priority = 100,
|
|
})
|
|
end
|
|
end
|
|
end
|
|
|
|
local function setup_highlights()
|
|
vim.api.nvim_set_hl(0, 'PendingHeader', { link = 'Title', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingDue', { link = 'DiagnosticHint', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingOverdue', { link = 'DiagnosticError', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingDone', { link = 'Comment', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingPriority', { link = 'DiagnosticWarn', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingRecur', { link = 'DiagnosticInfo', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingFilter', { link = 'DiagnosticWarn', default = true })
|
|
vim.api.nvim_set_hl(0, 'PendingFile', { link = 'Directory', default = true })
|
|
end
|
|
|
|
local function snapshot_folds(bufnr)
|
|
if current_view ~= 'category' then
|
|
return
|
|
end
|
|
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
|
|
local state = {}
|
|
vim.api.nvim_win_call(winid, function()
|
|
for lnum, m in ipairs(_meta) do
|
|
if m.type == 'header' and m.category then
|
|
if vim.fn.foldclosed(lnum) ~= -1 then
|
|
state[m.category] = true
|
|
end
|
|
end
|
|
end
|
|
end)
|
|
_fold_state[winid] = state
|
|
end
|
|
end
|
|
|
|
local function restore_folds(bufnr)
|
|
if current_view ~= 'category' then
|
|
return
|
|
end
|
|
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
|
|
local state = _fold_state[winid]
|
|
if state and next(state) ~= nil then
|
|
vim.api.nvim_win_call(winid, function()
|
|
vim.cmd('normal! zx')
|
|
local saved = vim.api.nvim_win_get_cursor(0)
|
|
for lnum, m in ipairs(_meta) do
|
|
if m.type == 'header' and m.category and state[m.category] then
|
|
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
|
|
vim.cmd('normal! zc')
|
|
end
|
|
end
|
|
vim.api.nvim_win_set_cursor(0, saved)
|
|
end)
|
|
_fold_state[winid] = nil
|
|
end
|
|
end
|
|
end
|
|
|
|
---@param bufnr? integer
|
|
---@return nil
|
|
function M.render(bufnr)
|
|
bufnr = bufnr or task_bufnr
|
|
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then
|
|
return
|
|
end
|
|
|
|
current_view = current_view or config.get().default_view
|
|
local view_label = current_view == 'priority' and 'queue' or current_view
|
|
vim.api.nvim_buf_set_name(bufnr, 'pending://' .. view_label)
|
|
local all_tasks = _store and _store:active_tasks() or {}
|
|
local tasks = {}
|
|
for _, task in ipairs(all_tasks) do
|
|
if not _hidden_ids[task.id] then
|
|
table.insert(tasks, task)
|
|
end
|
|
end
|
|
|
|
local lines, line_meta
|
|
if current_view == 'priority' then
|
|
lines, line_meta = views.priority_view(tasks)
|
|
else
|
|
lines, line_meta = views.category_view(tasks)
|
|
end
|
|
|
|
if #_filter_predicates > 0 then
|
|
table.insert(lines, 1, 'FILTER: ' .. table.concat(_filter_predicates, ' '))
|
|
table.insert(line_meta, 1, { type = 'filter' })
|
|
end
|
|
|
|
_meta = line_meta
|
|
|
|
snapshot_folds(bufnr)
|
|
vim.bo[bufnr].modifiable = true
|
|
local saved = vim.bo[bufnr].undolevels
|
|
vim.bo[bufnr].undolevels = -1
|
|
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
|
|
vim.bo[bufnr].modified = false
|
|
vim.bo[bufnr].undolevels = saved
|
|
|
|
setup_syntax(bufnr)
|
|
apply_extmarks(bufnr, line_meta)
|
|
|
|
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
|
|
if current_view == 'category' 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
|
|
else
|
|
vim.wo[winid].foldmethod = 'manual'
|
|
vim.wo[winid].foldenable = false
|
|
end
|
|
end
|
|
restore_folds(bufnr)
|
|
end
|
|
|
|
---@return nil
|
|
function M.toggle_view()
|
|
if current_view == 'category' then
|
|
current_view = 'priority'
|
|
else
|
|
current_view = 'category'
|
|
end
|
|
M.render()
|
|
end
|
|
|
|
---@return integer bufnr
|
|
function M.open()
|
|
setup_highlights()
|
|
if _store then
|
|
_store:load()
|
|
end
|
|
|
|
if task_winid and vim.api.nvim_win_is_valid(task_winid) then
|
|
vim.api.nvim_set_current_win(task_winid)
|
|
M.render(task_bufnr)
|
|
return task_bufnr --[[@as integer]]
|
|
end
|
|
|
|
if not (task_bufnr and vim.api.nvim_buf_is_valid(task_bufnr)) then
|
|
task_bufnr = vim.api.nvim_create_buf(true, false)
|
|
set_buf_options(task_bufnr)
|
|
end
|
|
|
|
vim.cmd('botright new')
|
|
task_winid = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_win_set_buf(task_winid, task_bufnr)
|
|
local h = config.get().drawer_height
|
|
if h and h > 0 then
|
|
vim.api.nvim_win_set_height(task_winid, h)
|
|
end
|
|
set_win_options(task_winid)
|
|
|
|
M.render(task_bufnr)
|
|
|
|
return task_bufnr
|
|
end
|
|
|
|
return M
|