pending.nvim/lua/pending/buffer.lua
Barrett Ruth 649f238683 feat(detail): parse and validate editable frontmatter on save (#163)
Problem: the detail buffer rendered metadata as read-only virtual text
overlays. Users could not edit status, priority, category, due, or
recurrence from the detail view.

Solution: render frontmatter as real `Key: value` text lines highlighted
via extmarks. On `:w`, `parse_detail_frontmatter()` validates every
field (status, priority bounds, `resolve_date`, `recur.validate`) and
aborts with `log.error()` on any invalid input. Removing a line clears
the field; editing the `# title` updates the description.
2026-03-13 20:49:33 -04:00

1134 lines
32 KiB
Lua

local config = require('pending.config')
local log = require('pending.log')
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 ns_eol = vim.api.nvim_create_namespace('pending_eol')
local ns_inline = vim.api.nvim_create_namespace('pending_inline')
---@type 'category'|'priority'|nil
local current_view = nil
---@type pending.LineMeta[]
local _meta = {}
---@type table<integer, table<string, boolean>>
local _fold_state = {}
---@type boolean
local _initial_fold_loaded = false
---@type string[]
local _filter_predicates = {}
---@type table<integer, true>
local _hidden_ids = {}
---@type table<integer, true>
local _dirty_rows = {}
---@type boolean
local _on_bytes_active = false
---@type boolean
local _rendering = false
---@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
---@param winid integer
---@return nil
function M.update_winid(winid)
task_winid = winid
end
---@param b? integer
---@return nil
function M.clear_marks(b)
local bufnr = b or task_bufnr
vim.api.nvim_buf_clear_namespace(bufnr, ns_eol, 0, -1)
vim.api.nvim_buf_clear_namespace(bufnr, ns_inline, 0, -1)
end
---@param b integer
---@param row integer
---@return nil
function M.clear_inline_row(b, row)
vim.api.nvim_buf_clear_namespace(b, ns_inline, row - 1, row)
end
---@return table<integer, true>
function M.dirty_rows()
return _dirty_rows
end
---@return nil
function M.clear_dirty_rows()
_dirty_rows = {}
end
---@param bufnr integer
---@param row integer
---@param m pending.LineMeta
---@param icons table
local function apply_inline_row(bufnr, row, m, icons)
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, ns_inline, row, 0, {
end_col = #line,
hl_group = 'PendingFilter',
invalidate = true,
})
elseif m.type == 'task' then
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, ns_inline, row, col_start, {
end_col = #line,
hl_group = 'PendingDone',
invalidate = true,
})
elseif m.status == 'cancelled' 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, ns_inline, row, col_start, {
end_col = #line,
hl_group = 'PendingCancelled',
invalidate = true,
})
elseif m.status == 'blocked' 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, ns_inline, row, col_start, {
end_col = #line,
hl_group = 'PendingBlocked',
invalidate = true,
})
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.status == 'cancelled' then
icon, icon_hl = icons.cancelled, 'PendingCancelled'
elseif m.status == 'wip' then
icon, icon_hl = icons.wip, 'PendingWip'
elseif m.status == 'blocked' then
icon, icon_hl = icons.blocked, 'PendingBlocked'
elseif m.priority and m.priority >= 3 then
icon, icon_hl = icons.priority, 'PendingPriority3'
elseif m.priority and m.priority == 2 then
icon, icon_hl = icons.priority, 'PendingPriority2'
elseif m.priority and m.priority > 0 then
icon, icon_hl = icons.priority, 'PendingPriority'
else
icon, icon_hl = icons.pending, 'Normal'
end
vim.api.nvim_buf_set_extmark(bufnr, ns_inline, row, bracket_col, {
virt_text = { { '[' .. icon .. ']', icon_hl } },
virt_text_pos = 'overlay',
priority = 100,
invalidate = true,
})
if m.forge_spans then
local forge = require('pending.forge')
for _, span in ipairs(m.forge_spans) do
local label_text, hl_group = forge.format_label(span.ref, span.cache)
vim.api.nvim_buf_set_extmark(bufnr, ns_inline, row, span.col_start, {
end_col = span.col_end,
conceal = '',
virt_text = { { label_text, hl_group } },
virt_text_pos = 'inline',
priority = 90,
invalidate = true,
})
end
end
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, ns_inline, row, 0, {
end_col = #line,
hl_group = 'PendingHeader',
invalidate = true,
})
vim.api.nvim_buf_set_extmark(bufnr, ns_inline, row, 0, {
virt_text = { { icons.category .. ' ', 'PendingHeader' } },
virt_text_pos = 'overlay',
priority = 100,
invalidate = true,
})
end
end
---@param line string
---@return string?
local function infer_status(line)
local ch = line:match('^/%d+/%- %[(.)%]') or line:match('^%- %[(.)%]')
if not ch then
return nil
end
local icons = config.get().icons
if ch == icons.done then
return 'done'
elseif ch == icons.cancelled then
return 'cancelled'
elseif ch == icons.wip then
return 'wip'
elseif ch == icons.blocked then
return 'blocked'
end
return 'pending'
end
---@param bufnr integer
---@return nil
function M.reapply_dirty_inline(bufnr)
if not next(_dirty_rows) then
return
end
log.debug(('reapply_dirty: rows=%s'):format(vim.inspect(vim.tbl_keys(_dirty_rows))))
local icons = config.get().icons
for row in pairs(_dirty_rows) do
local m = _meta[row]
if m and m.type == 'task' then
local line = vim.api.nvim_buf_get_lines(bufnr, row - 1, row, false)[1] or ''
local old_status = m.status
m.status = infer_status(line) or m.status
m.forge_spans = nil
log.debug(
('reapply_dirty: row=%d line=%q old_status=%s new_status=%s'):format(
row,
line,
tostring(old_status),
tostring(m.status)
)
)
end
if m then
vim.api.nvim_buf_clear_namespace(bufnr, ns_inline, row - 1, row)
apply_inline_row(bufnr, row - 1, m, icons)
end
end
_dirty_rows = {}
end
---@param bufnr integer
---@return nil
function M.attach_bytes(bufnr)
if _on_bytes_active then
return
end
_on_bytes_active = true
vim.api.nvim_buf_attach(bufnr, false, {
on_bytes = function(_, buf, _, start_row, start_col, _, old_end_row, _, _, new_end_row, _, _)
if buf ~= task_bufnr then
_on_bytes_active = false
return true
end
if _rendering then
return
end
local delta = new_end_row - old_end_row
log.debug(
('on_bytes: start_row=%d start_col=%d old_end=%d new_end=%d delta=%d'):format(
start_row,
start_col,
old_end_row,
new_end_row,
delta
)
)
if delta > 0 then
for _ = 1, delta do
log.debug(('on_bytes: insert meta at %d'):format(start_row + 2))
table.insert(_meta, start_row + 2, { type = 'task' })
end
elseif delta < 0 then
for _ = 1, -delta do
if _meta[start_row + 2] then
log.debug(('on_bytes: remove meta at %d'):format(start_row + 2))
table.remove(_meta, start_row + 2)
end
end
end
for r = start_row + 1, start_row + 1 + math.max(0, new_end_row) do
_dirty_rows[r] = true
end
log.debug(('on_bytes: dirty rows=%s'):format(vim.inspect(vim.tbl_keys(_dirty_rows))))
for i, m in ipairs(_meta) do
log.debug(('on_bytes: _meta[%d] type=%s status=%s'):format(i, m.type, tostring(m.status)))
end
end,
})
end
---@return nil
function M.persist_folds()
log.debug(
('persist_folds: view=%s store=%s'):format(tostring(current_view), tostring(_store ~= nil))
)
if current_view ~= 'category' or not _store then
log.debug('persist_folds: early return (view or store)')
return
end
local bufnr = task_bufnr
if not bufnr or not vim.api.nvim_buf_is_valid(bufnr) then
log.debug('persist_folds: early return (no valid bufnr)')
return
end
local folded = {}
local seen = {}
local wins = vim.fn.win_findbuf(bufnr)
log.debug(
('persist_folds: checking %d windows for bufnr=%d, meta has %d entries'):format(
#wins,
bufnr,
#_meta
)
)
for _, winid in ipairs(wins) do
if vim.api.nvim_win_is_valid(winid) then
vim.api.nvim_win_call(winid, function()
for lnum, m in ipairs(_meta) do
if m.type == 'header' and m.category and not seen[m.category] then
local closed = vim.fn.foldclosed(lnum)
log.debug(
('persist_folds: win=%d lnum=%d cat=%s foldclosed=%d'):format(
winid,
lnum,
m.category,
closed
)
)
if closed ~= -1 then
seen[m.category] = true
table.insert(folded, m.category)
end
end
end
end)
end
end
log.debug(
('persist_folds: saving %d folded categories: %s'):format(#folded, table.concat(folded, ', '))
)
_store:set_folded_categories(folded)
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
M.persist_folds()
if _store then
_store:save()
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 = 'nic'
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
local meta_pos = insert_row + 1
_rendering = true
vim.bo[bufnr].modifiable = true
vim.api.nvim_buf_set_lines(bufnr, insert_row, insert_row, false, { '- [ ] ' })
_rendering = false
table.insert(_meta, meta_pos, { type = 'task' })
local icons = config.get().icons
local total = vim.api.nvim_buf_line_count(bufnr)
for r = meta_pos, math.min(meta_pos + 1, total) do
vim.api.nvim_buf_clear_namespace(bufnr, ns_inline, r - 1, r)
local m = _meta[r]
if m then
apply_inline_row(bufnr, r - 1, m, icons)
end
end
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
---@class pending.EolSegment
---@field type 'specifier'|'literal'
---@field key? 'c'|'r'|'d'
---@field text? string
---@param fmt string
---@return pending.EolSegment[]
local function parse_eol_format(fmt)
local segments = {}
local pos = 1
local len = #fmt
while pos <= len do
if fmt:sub(pos, pos) == '%' and pos + 1 <= len then
local key = fmt:sub(pos + 1, pos + 1)
if key == 'c' or key == 'r' or key == 'd' then
table.insert(segments, { type = 'specifier', key = key })
pos = pos + 2
else
table.insert(segments, { type = 'literal', text = '%' .. key })
pos = pos + 2
end
else
local next_pct = fmt:find('%%', pos + 1)
local chunk = next_pct and fmt:sub(pos, next_pct - 1) or fmt:sub(pos)
table.insert(segments, { type = 'literal', text = chunk })
pos = pos + #chunk
end
end
return segments
end
---@param segments pending.EolSegment[]
---@param m pending.LineMeta
---@param icons pending.Icons
---@return string[][]
local function build_eol_virt(segments, m, icons)
local due_hl = m.overdue and 'PendingOverdue' or 'PendingDue'
local resolved = {}
for i, seg in ipairs(segments) do
if seg.type == 'specifier' then
local text, hl
if seg.key == 'c' and m.show_category and m.category then
text = icons.category .. ' ' .. m.category
hl = 'PendingHeader'
elseif seg.key == 'r' and m.recur then
text = icons.recur .. ' ' .. m.recur
hl = 'PendingRecur'
elseif seg.key == 'd' and m.due then
text = icons.due .. ' ' .. m.due
hl = due_hl
end
resolved[i] = text and { text = text, hl = hl, present = true } or { present = false }
else
resolved[i] = { text = seg.text, hl = 'Normal', literal = true }
end
end
local virt_parts = {}
local pending_sep = nil
for _, r in ipairs(resolved) do
if r.literal then
if #virt_parts > 0 and not pending_sep then
pending_sep = { r.text, r.hl }
end
elseif r.present then
if pending_sep then
table.insert(virt_parts, pending_sep)
pending_sep = nil
end
table.insert(virt_parts, { r.text, r.hl })
else
pending_sep = nil
end
end
return virt_parts
end
---@param bufnr integer
---@param line_meta pending.LineMeta[]
local function apply_extmarks(bufnr, line_meta)
local cfg = config.get()
local icons = cfg.icons
local eol_segments = parse_eol_format(cfg.view.eol_format or '%c %r %d')
vim.api.nvim_buf_clear_namespace(bufnr, ns_eol, 0, -1)
vim.api.nvim_buf_clear_namespace(bufnr, ns_inline, 0, -1)
log.debug(('apply_extmarks: full render, %d lines'):format(#line_meta))
for i, m in ipairs(line_meta) do
log.debug(
('apply_extmarks: row=%d type=%s status=%s'):format(i - 1, m.type, tostring(m.status))
)
local row = i - 1
if m.type == 'task' then
local virt_parts = build_eol_virt(eol_segments, m, icons)
if #virt_parts > 0 then
vim.api.nvim_buf_set_extmark(bufnr, ns_eol, row, 0, {
virt_text = virt_parts,
virt_text_pos = 'eol',
invalidate = true,
})
end
end
apply_inline_row(bufnr, row, m, icons)
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, 'PendingPriority2', { link = 'DiagnosticError', default = true })
vim.api.nvim_set_hl(0, 'PendingPriority3', { link = 'DiagnosticError', default = true })
vim.api.nvim_set_hl(0, 'PendingWip', { link = 'DiagnosticInfo', default = true })
vim.api.nvim_set_hl(0, 'PendingBlocked', { link = 'DiagnosticError', default = true })
vim.api.nvim_set_hl(0, 'PendingCancelled', { link = 'NonText', 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, 'PendingForge', { link = 'DiagnosticInfo', default = true })
vim.api.nvim_set_hl(0, 'PendingForgeClosed', { link = 'Comment', default = true })
vim.api.nvim_set_hl(0, 'PendingDetailMeta', { link = 'Comment', 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' or not config.resolve_folding().enabled then
return
end
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
if _fold_state[winid] == nil and _initial_fold_loaded then
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
end
local function restore_folds(bufnr)
log.debug(
('restore_folds: view=%s folding_enabled=%s'):format(
tostring(current_view),
tostring(config.resolve_folding().enabled)
)
)
if current_view ~= 'category' or not config.resolve_folding().enabled then
return
end
for _, winid in ipairs(vim.fn.win_findbuf(bufnr)) do
local state = _fold_state[winid]
_fold_state[winid] = nil
log.debug(
('restore_folds: win=%d has_fold_state=%s initial_loaded=%s has_store=%s'):format(
winid,
tostring(state ~= nil),
tostring(_initial_fold_loaded),
tostring(_store ~= nil)
)
)
if not state and not _initial_fold_loaded and _store then
_initial_fold_loaded = true
local cats = _store:get_folded_categories()
log.debug(
('restore_folds: loaded %d categories from store: %s'):format(
#cats,
table.concat(cats, ', ')
)
)
if #cats > 0 then
state = {}
for _, cat in ipairs(cats) do
state[cat] = true
end
end
end
if state and next(state) ~= nil then
local applying = {}
for k in pairs(state) do
table.insert(applying, k)
end
log.debug(('restore_folds: applying folds for: %s'):format(table.concat(applying, ', ')))
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
log.debug(('restore_folds: folding lnum=%d cat=%s'):format(lnum, m.category))
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
vim.cmd('normal! zc')
end
end
vim.api.nvim_win_set_cursor(0, saved)
end)
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().view.default
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 #lines == 0 and #_filter_predicates == 0 then
local default_cat = config.get().default_category
lines = { '# ' .. default_cat }
line_meta = { { type = 'header', category = default_cat } }
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
_dirty_rows = {}
snapshot_folds(bufnr)
vim.bo[bufnr].modifiable = true
local saved = vim.bo[bufnr].undolevels
vim.bo[bufnr].undolevels = -1
_rendering = true
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
_rendering = false
vim.bo[bufnr].modified = false
vim.bo[bufnr].undolevels = saved
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' 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
end
end
restore_folds(bufnr)
end
---@return nil
function M.toggle_view()
snapshot_folds(task_bufnr)
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)
M.attach_bytes(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
local ns_detail = vim.api.nvim_create_namespace('pending_detail')
local DETAIL_SEPARATOR = '---'
---@type integer?
local _detail_bufnr = nil
---@type integer?
local _detail_task_id = nil
---@return integer?
function M.detail_bufnr()
return _detail_bufnr
end
---@return integer?
function M.detail_task_id()
return _detail_task_id
end
local VALID_STATUSES = {
pending = true,
done = true,
wip = true,
blocked = true,
cancelled = true,
}
---@param task pending.Task
---@return string[]
local function build_detail_frontmatter(task)
local lines = {}
table.insert(lines, 'Status: ' .. (task.status or 'pending'))
table.insert(lines, 'Priority: ' .. (task.priority or 0))
if task.category then
table.insert(lines, 'Category: ' .. task.category)
end
if task.due then
table.insert(lines, 'Due: ' .. task.due)
end
if task.recur then
local recur_val = task.recur
if task.recur_mode == 'completion' then
recur_val = '!' .. recur_val
end
table.insert(lines, 'Recur: ' .. recur_val)
end
return lines
end
---@param bufnr integer
---@param sep_row integer
---@return nil
local function apply_detail_extmarks(bufnr, sep_row)
vim.api.nvim_buf_clear_namespace(bufnr, ns_detail, 0, -1)
for i = 1, sep_row - 1 do
vim.api.nvim_buf_set_extmark(bufnr, ns_detail, i, 0, {
end_row = i,
end_col = #(vim.api.nvim_buf_get_lines(bufnr, i, i + 1, false)[1] or ''),
hl_group = 'PendingDetailMeta',
})
end
end
---@param task_id integer
---@return integer? bufnr
function M.open_detail(task_id)
if not _store then
return nil
end
if _detail_bufnr and vim.api.nvim_buf_is_valid(_detail_bufnr) then
if _detail_task_id == task_id then
return _detail_bufnr
end
vim.api.nvim_buf_delete(_detail_bufnr, { force = true })
_detail_bufnr = nil
_detail_task_id = nil
end
local task = _store:get(task_id)
if not task then
log.warn('task not found: ' .. task_id)
return nil
end
setup_highlights()
local bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_name(bufnr, 'pending://task/' .. task_id)
vim.bo[bufnr].buftype = 'acwrite'
vim.bo[bufnr].filetype = 'markdown'
vim.bo[bufnr].swapfile = false
local lines = { '# ' .. task.description }
local fm = build_detail_frontmatter(task)
for _, fl in ipairs(fm) do
table.insert(lines, fl)
end
table.insert(lines, DETAIL_SEPARATOR)
local notes = task.notes or ''
if notes ~= '' then
for note_line in (notes .. '\n'):gmatch('(.-)\n') do
table.insert(lines, note_line)
end
else
table.insert(lines, '')
end
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.bo[bufnr].modified = false
local sep_row = #fm + 1
apply_detail_extmarks(bufnr, sep_row)
local winid = task_winid
if winid and vim.api.nvim_win_is_valid(winid) then
vim.api.nvim_win_set_buf(winid, bufnr)
end
vim.wo[winid].conceallevel = 0
vim.wo[winid].foldmethod = 'manual'
vim.wo[winid].foldenable = false
_detail_bufnr = bufnr
_detail_task_id = task_id
local cursor_row = sep_row + 2
local total = vim.api.nvim_buf_line_count(bufnr)
if cursor_row > total then
cursor_row = total
end
pcall(vim.api.nvim_win_set_cursor, winid, { cursor_row, 0 })
return bufnr
end
---@return nil
function M.close_detail()
if _detail_bufnr and vim.api.nvim_buf_is_valid(_detail_bufnr) then
vim.api.nvim_buf_delete(_detail_bufnr, { force = true })
end
_detail_bufnr = nil
_detail_task_id = nil
if task_winid and vim.api.nvim_win_is_valid(task_winid) then
if task_bufnr and vim.api.nvim_buf_is_valid(task_bufnr) then
vim.api.nvim_win_set_buf(task_winid, task_bufnr)
set_win_options(task_winid)
M.render(task_bufnr)
end
end
end
---@param lines string[]
---@return integer? sep_row
---@return pending.DetailFields? fields
---@return string? err
local function parse_detail_frontmatter(lines)
local parse = require('pending.parse')
local recur = require('pending.recur')
local cfg = config.get()
local sep_row = nil
for i, line in ipairs(lines) do
if line == DETAIL_SEPARATOR then
sep_row = i
break
end
end
if not sep_row then
return nil, nil, 'missing separator (---)'
end
local desc = lines[1] and lines[1]:match('^# (.+)$')
if not desc or desc:match('^%s*$') then
return nil, nil, 'missing or empty title (first line must be # <title>)'
end
---@class pending.DetailFields
---@field description string
---@field status pending.TaskStatus
---@field priority integer
---@field category? string|userdata
---@field due? string|userdata
---@field recur? string|userdata
---@field recur_mode? pending.RecurMode|userdata
local fields = {
description = desc,
status = 'pending',
priority = 0,
category = vim.NIL,
due = vim.NIL,
recur = vim.NIL,
recur_mode = vim.NIL,
}
local seen = {} ---@type table<string, boolean>
for i = 2, sep_row - 1 do
local line = lines[i]
if line:match('^%s*$') then
goto continue
end
local key, val = line:match('^(%S+):%s*(.*)$')
if not key then
return nil, nil, 'invalid frontmatter line: ' .. line
end
key = key:lower()
if seen[key] then
return nil, nil, 'duplicate field: ' .. key
end
seen[key] = true
if key == 'status' then
val = val:lower()
if not VALID_STATUSES[val] then
return nil, nil, 'invalid status: ' .. val
end
fields.status = val --[[@as pending.TaskStatus]]
elseif key == 'priority' then
local n = tonumber(val)
if not n or n ~= math.floor(n) or n < 0 then
return nil, nil, 'invalid priority: ' .. val .. ' (must be integer >= 0)'
end
local max = cfg.max_priority or 3
if n > max then
return nil, nil, 'invalid priority: ' .. val .. ' (max is ' .. max .. ')'
end
fields.priority = n --[[@as integer]]
elseif key == 'category' then
if val == '' then
return nil, nil, 'empty category value'
end
fields.category = val
elseif key == 'due' then
if val == '' then
return nil, nil, 'empty due value (remove the line to clear)'
end
local resolved = parse.resolve_date(val)
if resolved then
fields.due = resolved
elseif
val:match('^%d%d%d%d%-%d%d%-%d%d$') or val:match('^%d%d%d%d%-%d%d%-%d%dT%d%d:%d%d$')
then
fields.due = val
else
return nil, nil, 'invalid due date: ' .. val
end
elseif key == 'recur' then
if val == '' then
return nil, nil, 'empty recur value (remove the line to clear)'
end
local raw_spec = val
local rec_mode = nil
if raw_spec:sub(1, 1) == '!' then
rec_mode = 'completion'
raw_spec = raw_spec:sub(2)
end
if not recur.validate(raw_spec) then
return nil, nil, 'invalid recurrence: ' .. val
end
fields.recur = raw_spec
fields.recur_mode = rec_mode or vim.NIL
else
return nil, nil, 'unknown field: ' .. key
end
::continue::
end
return sep_row, fields, nil
end
---@return nil
function M.save_detail()
if not _detail_bufnr or not _detail_task_id or not _store then
return
end
local task = _store:get(_detail_task_id)
if not task then
log.warn('task was deleted')
M.close_detail()
return
end
local lines = vim.api.nvim_buf_get_lines(_detail_bufnr, 0, -1, false)
local sep_row, fields, err = parse_detail_frontmatter(lines)
if err then
log.error(err)
return
end
---@cast sep_row integer
---@cast fields pending.DetailFields
local notes_text = ''
if sep_row < #lines then
local note_lines = {}
for i = sep_row + 1, #lines do
table.insert(note_lines, lines[i])
end
notes_text = table.concat(note_lines, '\n')
notes_text = notes_text:gsub('%s+$', '')
end
local update = {
description = fields.description,
status = fields.status,
priority = fields.priority,
category = fields.category,
due = fields.due,
recur = fields.recur,
recur_mode = fields.recur_mode,
}
if notes_text == '' then
update.notes = vim.NIL
else
update.notes = notes_text
end
_store:update(_detail_task_id, update)
_store:save()
vim.bo[_detail_bufnr].modified = false
apply_detail_extmarks(_detail_bufnr, sep_row - 1)
end
M._parse_detail_frontmatter = parse_detail_frontmatter
M._build_detail_frontmatter = build_detail_frontmatter
return M