pending.nvim/lua/pending/buffer.lua
Barrett Ruth e04440bb3d refactor: adopt markdown-style checkbox buffer format (#20)
* refactor(config): change default category from Inbox to Todo

* refactor(views): adopt markdown checkbox line format

Problem: task lines used an opaque /ID/  [N] prefix format that was
hard to read and inconsistent between category and priority views.
Header lines had no visual marker distinguishing them from tasks.

Solution: render headers as '## Cat', task lines as
'/ID/- [x|!| ] description'. State encoding: [x]=done, [!]=urgent,
[ ]=pending. Both views use the same construction.

* refactor(diff): parse and reconcile markdown checkbox format

Problem: parse_buffer matched the old '  text' indent pattern and
detected headers via '^%S'. Priority was read from a '[N] ' prefix.
apply() never reconciled status changes written into the buffer.

Solution: match '- [.] text' for tasks and '^## ' for headers.
Extract state char to derive priority (! -> 1) and status (x -> done).
apply() now reconciles status from the buffer, setting/clearing 'end'
timestamps — enabling the oil-style edit-checkbox-then-:w workflow.

* refactor(buffer): update syntax, extmarks, and render for checkbox format

Problem: syntax patterns matched the old indent/[N] format; right_align
virtual text produced a broken layout in narrow windows; the done
strikethrough skipped past the '  ' indent leaving '- [x] ' unstyled;
render() added undo history entries so 'u' could undo a re-render.

Solution: update taskHeader/taskLine patterns for '## '/'- [.]'; rename
taskPriority -> taskCheckbox matching '[!]'; switch virt_text_pos to
'eol'; drop the +2 col_start offset so strikethrough covers '- [x] ';
guard nvim_buf_set_lines with undolevels=-1 so renders are not undoable.
Also fix open_line to insert '- [ ] ' and position cursor at col 6.

* refactor(init): replace multi-level priority with binary toggle

Problem: <C-a>/<C-x> overrode Vim's native number increment and the
visual g<C-a>/g<C-x> variants added complexity for marginal value.
toggle_complete() left the cursor on the wrong line after re-render.

Solution: remove change_priority/change_priority_visual; add
toggle_priority() (0<->1) mapped to '!', with cursor-follow after
render matching the pattern already used in priority toggle. Add
cursor-follow to toggle_complete() for the same reason. Update plugin
plugs (priority-up/down -> priority) and add 'due'/'undo' to the
:Pending completion list. Update help text accordingly.

* feat(buffer): reflect current view in buffer name

Problem: no way to tell at a glance which view (category vs priority)
is active — the buffer was always named 'pending://'.

Solution: update the buffer name to 'pending://category' or
'pending://priority' on every render, so the view is visible in
the statusline/tabline without any extra UI.
2026-02-24 23:21:55 -05:00

278 lines
7.6 KiB
Lua

local config = require('pending.config')
local store = require('pending.store')
local views = require('pending.views')
---@class pending.buffer
local M = {}
---@type integer?
local task_bufnr = 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 = {}
---@return pending.LineMeta[]
function M.meta()
return _meta
end
---@return integer?
function M.bufnr()
return task_bufnr
end
---@return string?
function M.current_view_name()
return current_view
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
end
---@param winid integer
local function set_win_options(winid)
vim.wo[winid].conceallevel = 3
vim.wo[winid].concealcursor = 'nvic'
vim.wo[winid].wrap = false
vim.wo[winid].number = false
vim.wo[winid].relativenumber = false
vim.wo[winid].signcolumn = 'no'
vim.wo[winid].foldcolumn = '0'
vim.wo[winid].spell = false
vim.wo[winid].cursorline = 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
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)
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 == 'task' then
local due_hl = m.overdue and 'PendingOverdue' or 'PendingDue'
if m.show_category then
local virt_text
if m.category and m.due then
virt_text = { { m.category .. ' ', 'PendingHeader' }, { m.due, due_hl } }
elseif m.category then
virt_text = { { m.category, 'PendingHeader' } }
elseif m.due then
virt_text = { { m.due, due_hl } }
end
if virt_text then
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
virt_text = virt_text,
virt_text_pos = 'eol',
})
end
elseif m.due then
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
virt_text = { { m.due, due_hl } },
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
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',
})
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 })
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
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
vim.api.nvim_buf_set_name(bufnr, 'pending://' .. current_view)
local tasks = store.active_tasks()
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
_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
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()
store.load()
if task_bufnr and vim.api.nvim_buf_is_valid(task_bufnr) then
local wins = vim.fn.win_findbuf(task_bufnr)
if #wins > 0 then
vim.api.nvim_set_current_win(wins[1])
M.render(task_bufnr)
return task_bufnr
end
vim.api.nvim_set_current_buf(task_bufnr)
set_win_options(vim.api.nvim_get_current_win())
M.render(task_bufnr)
return task_bufnr
end
task_bufnr = vim.api.nvim_create_buf(true, false)
set_buf_options(task_bufnr)
vim.api.nvim_set_current_buf(task_bufnr)
set_win_options(vim.api.nvim_get_current_win())
M.render(task_bufnr)
return task_bufnr
end
return M