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.
This commit is contained in:
Barrett Ruth 2026-02-24 23:21:55 -05:00 committed by GitHub
parent 8e16744ebe
commit 5db242a9cf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
10 changed files with 101 additions and 130 deletions

View file

@ -58,9 +58,9 @@ local function setup_syntax(bufnr)
vim.cmd([[
syntax clear
syntax match taskId /^\/\d\+\// conceal
syntax match taskHeader /^\S.*$/ contains=taskId
syntax match taskPriority /\[\d\+\] / contained containedin=taskLine
syntax match taskLine /^\/\d\+\/ .*$/ contains=taskId,taskPriority
syntax match taskHeader /^## .*$/ contains=taskId
syntax match taskCheckbox /\[!\]/ contained containedin=taskLine
syntax match taskLine /^\/\d\+\/- \[.\] .*$/ contains=taskId,taskCheckbox
]])
end)
end
@ -74,8 +74,8 @@ function M.open_line(above)
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, 2 })
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
@ -115,18 +115,18 @@ local function apply_extmarks(bufnr, line_meta)
if virt_text then
vim.api.nvim_buf_set_extmark(bufnr, task_ns, row, 0, {
virt_text = virt_text,
virt_text_pos = 'right_align',
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 = 'right_align',
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+/')) + 2 or 0
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',
@ -200,6 +200,7 @@ function M.render(bufnr)
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
@ -213,8 +214,11 @@ function M.render(bufnr)
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)
@ -261,7 +265,6 @@ function M.open()
end
task_bufnr = vim.api.nvim_create_buf(true, false)
vim.api.nvim_buf_set_name(task_bufnr, 'pending://')
set_buf_options(task_bufnr)
vim.api.nvim_set_current_buf(task_bufnr)

View file

@ -18,7 +18,7 @@ local M = {}
local defaults = {
data_path = vim.fn.stdpath('data') .. '/pending/tasks.json',
default_view = 'category',
default_category = 'Inbox',
default_category = 'Todo',
date_format = '%b %d',
date_syntax = 'due',
category_order = {},

View file

@ -7,6 +7,7 @@ local store = require('pending.store')
---@field id? integer
---@field description? string
---@field priority? integer
---@field status? string
---@field category? string
---@field due? string
---@field lnum integer
@ -26,20 +27,17 @@ function M.parse_buffer(lines)
local current_category = nil
for i, line in ipairs(lines) do
local id, body = line:match('^/(%d+)/( .+)$')
local id, body = line:match('^/(%d+)/(- %[.%] .*)$')
if not id then
body = line:match('^( .+)$')
body = line:match('^(- %[.%] .*)$')
end
if line == '' then
table.insert(result, { type = 'blank', lnum = i })
elseif id or body then
local stripped = body:match('^ (.+)$') or body
local prio_str = stripped:match('^%[(%d+)%] ')
local priority = 0
if prio_str then
priority = tonumber(prio_str) --[[@as integer]]
stripped = stripped:sub(#prio_str + 4)
end
local stripped = body:match('^- %[.%] (.*)$') or body
local state_char = body:match('^- %[(.-)%]') or ' '
local priority = state_char == '!' and 1 or 0
local status = state_char == 'x' and 'done' or 'pending'
local description, metadata = parse.body(stripped)
if description and description ~= '' then
table.insert(result, {
@ -47,14 +45,15 @@ function M.parse_buffer(lines)
id = id and tonumber(id) or nil,
description = description,
priority = priority,
status = status,
category = metadata.cat or current_category or config.get().default_category,
due = metadata.due,
lnum = i,
})
end
elseif line:match('^%S') then
current_category = line
table.insert(result, { type = 'header', category = line, lnum = i })
elseif line:match('^## (.+)$') then
current_category = line:match('^## (.+)$')
table.insert(result, { type = 'header', category = current_category, lnum = i })
end
end
@ -113,6 +112,15 @@ function M.apply(lines)
task.due = entry.due
changed = true
end
if entry.status and task.status ~= entry.status then
task.status = entry.status
if entry.status == 'done' then
task['end'] = now
else
task['end'] = nil
end
changed = true
end
if task.order ~= order_counter then
task.order = order_counter
changed = true

View file

@ -52,17 +52,8 @@ function M._setup_buf_mappings(bufnr)
vim.keymap.set('n', 'g?', function()
M.show_help()
end, opts)
vim.keymap.set('n', '<C-a>', function()
M.change_priority(1)
end, opts)
vim.keymap.set('n', '<C-x>', function()
M.change_priority(-1)
end, opts)
vim.keymap.set('v', 'g<C-a>', function()
M.change_priority_visual(1)
end, opts)
vim.keymap.set('v', 'g<C-x>', function()
M.change_priority_visual(-1)
vim.keymap.set('n', '!', function()
M.toggle_priority()
end, opts)
vim.keymap.set('n', 'D', function()
M.prompt_date()
@ -126,10 +117,15 @@ function M.toggle_complete()
end
store.save()
buffer.render(bufnr)
for lnum, m in ipairs(buffer.meta()) do
if m.id == id then
vim.api.nvim_win_set_cursor(0, { lnum, 0 })
break
end
end
end
---@param delta integer
function M.change_priority(delta)
function M.toggle_priority()
local bufnr = buffer.bufnr()
if not bufnr then
return
@ -147,7 +143,7 @@ function M.change_priority(delta)
if not task then
return
end
local new_priority = math.max(0, task.priority + delta)
local new_priority = task.priority > 0 and 0 or 1
store.update(id, { priority = new_priority })
store.save()
buffer.render(bufnr)
@ -159,33 +155,6 @@ function M.change_priority(delta)
end
end
---@param delta integer
function M.change_priority_visual(delta)
local bufnr = buffer.bufnr()
if not bufnr then
return
end
local start_row = vim.fn.line("'<")
local end_row = vim.fn.line("'>")
local meta = buffer.meta()
local changed = false
for row = start_row, end_row do
local m = meta[row]
if m and m.type == 'task' and m.id then
local task = store.get(m.id)
if task then
local new_priority = math.max(0, task.priority + delta)
store.update(m.id, { priority = new_priority })
changed = true
end
end
end
if changed then
store.save()
buffer.render(bufnr)
end
end
function M.prompt_date()
local bufnr = buffer.bufnr()
if not bufnr then
@ -342,10 +311,7 @@ function M.show_help()
'',
'<CR> Toggle complete/uncomplete',
'<Tab> Switch category/priority view',
'<C-a> Raise priority level',
'<C-x> Lower priority level',
'g<C-a> Raise priority for visual selection',
'g<C-x> Lower priority for visual selection',
'! Toggle urgent',
'D Set due date',
'U Undo last write',
'o / O Add new task line',
@ -371,7 +337,7 @@ function M.show_help()
'',
'Highlights:',
' PendingOverdue overdue tasks (red)',
' PendingPriority [N] priority prefix',
' PendingPriority [!] urgent tasks',
'',
'Press q or <Esc> to close',
}

View file

@ -125,7 +125,7 @@ function M.category_view(tasks)
table.insert(lines, '')
table.insert(meta, { type = 'blank' })
end
table.insert(lines, cat)
table.insert(lines, '## ' .. cat)
table.insert(meta, { type = 'header', category = cat })
local all = {}
@ -138,9 +138,8 @@ function M.category_view(tasks)
for _, task in ipairs(all) do
local prefix = '/' .. task.id .. '/'
local indent = ' '
local prio = task.priority > 0 and ('[' .. task.priority .. '] ') or ''
local line = prefix .. indent .. prio .. task.description
local state = task.status == 'done' and 'x' or (task.priority > 0 and '!' or ' ')
local line = prefix .. '- [' .. state .. '] ' .. task.description
table.insert(lines, line)
table.insert(meta, {
type = 'task',
@ -189,9 +188,8 @@ function M.priority_view(tasks)
for _, task in ipairs(all) do
local prefix = '/' .. task.id .. '/'
local indent = ' '
local prio = task.priority == 1 and '! ' or ''
local line = prefix .. indent .. prio .. task.description
local state = task.status == 'done' and 'x' or (task.priority > 0 and '!' or ' ')
local line = prefix .. '- [' .. state .. '] ' .. task.description
table.insert(lines, line)
table.insert(meta, {
type = 'task',

View file

@ -8,7 +8,7 @@ vim.api.nvim_create_user_command('Pending', function(opts)
end, {
nargs = '*',
complete = function(arg_lead, cmd_line)
local subcmds = { 'add', 'sync', 'archive' }
local subcmds = { 'add', 'sync', 'archive', 'due', 'undo' }
if not cmd_line:match('^Pending%s+%S') then
return vim.tbl_filter(function(s)
return s:find(arg_lead, 1, true) == 1
@ -30,12 +30,8 @@ vim.keymap.set('n', '<Plug>(pending-view)', function()
require('pending.buffer').toggle_view()
end)
vim.keymap.set('n', '<Plug>(pending-priority-up)', function()
require('pending').change_priority(1)
end)
vim.keymap.set('n', '<Plug>(pending-priority-down)', function()
require('pending').change_priority(-1)
vim.keymap.set('n', '<Plug>(pending-priority)', function()
require('pending').toggle_priority()
end)
vim.keymap.set('n', '<Plug>(pending-date)', function()

View file

@ -25,12 +25,12 @@ describe('diff', function()
describe('parse_buffer', function()
it('parses headers and tasks', function()
local lines = {
'School',
'/1/ Do homework',
'/2/ [1] Read chapter 5',
'## School',
'/1/- [ ] Do homework',
'/2/- [!] Read chapter 5',
'',
'Errands',
'/3/ Buy groceries',
'## Errands',
'/3/- [ ] Buy groceries',
}
local result = diff.parse_buffer(lines)
assert.are.equal(6, #result)
@ -48,8 +48,8 @@ describe('diff', function()
it('handles new tasks without ids', function()
local lines = {
'Inbox',
' New task here',
'## Inbox',
'- [ ] New task here',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
@ -60,8 +60,8 @@ describe('diff', function()
it('inline cat: token overrides header category', function()
local lines = {
'Inbox',
'/1/ Buy milk cat:Work',
'## Inbox',
'/1/- [ ] Buy milk cat:Work',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
@ -71,8 +71,8 @@ describe('diff', function()
it('inline due: token is parsed', function()
local lines = {
'Inbox',
'/1/ Buy milk due:2026-03-15',
'## Inbox',
'/1/- [ ] Buy milk due:2026-03-15',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
@ -84,9 +84,9 @@ describe('diff', function()
describe('apply', function()
it('creates new tasks from buffer lines', function()
local lines = {
'Inbox',
' First task',
' Second task',
'## Inbox',
'- [ ] First task',
'- [ ] Second task',
}
diff.apply(lines)
store.unload()
@ -102,8 +102,8 @@ describe('diff', function()
store.add({ description = 'Delete me' })
store.save()
local lines = {
'Inbox',
'/1/ Keep me',
'## Inbox',
'/1/- [ ] Keep me',
}
diff.apply(lines)
store.unload()
@ -119,8 +119,8 @@ describe('diff', function()
store.add({ description = 'Original' })
store.save()
local lines = {
'Inbox',
'/1/ Renamed',
'## Inbox',
'/1/- [ ] Renamed',
}
diff.apply(lines)
store.unload()
@ -134,8 +134,8 @@ describe('diff', function()
t.modified = '2020-01-01T00:00:00Z'
store.save()
local lines = {
'Inbox',
'/1/ Renamed',
'## Inbox',
'/1/- [ ] Renamed',
}
diff.apply(lines)
store.unload()
@ -149,9 +149,9 @@ describe('diff', function()
store.add({ description = 'Original' })
store.save()
local lines = {
'Inbox',
'/1/ Original',
'/1/ Copy of original',
'## Inbox',
'/1/- [ ] Original',
'/1/- [ ] Copy of original',
}
diff.apply(lines)
store.unload()
@ -164,8 +164,8 @@ describe('diff', function()
store.add({ description = 'Moving task', category = 'Inbox' })
store.save()
local lines = {
'Work',
'/1/ Moving task',
'## Work',
'/1/- [ ] Moving task',
}
diff.apply(lines)
store.unload()
@ -178,8 +178,8 @@ describe('diff', function()
store.add({ description = 'Stable task', category = 'Inbox' })
store.save()
local lines = {
'Inbox',
'/1/ Stable task',
'## Inbox',
'/1/- [ ] Stable task',
}
diff.apply(lines)
store.unload()
@ -196,8 +196,8 @@ describe('diff', function()
store.add({ description = 'Pay bill', due = '2026-03-15' })
store.save()
local lines = {
'Inbox',
'/1/ Pay bill',
'## Inbox',
'/1/- [ ] Pay bill',
}
diff.apply(lines)
store.unload()
@ -210,8 +210,8 @@ describe('diff', function()
store.add({ description = 'Task name', priority = 1 })
store.save()
local lines = {
'Inbox',
'/1/ Task name',
'## Inbox',
'/1/- [ ] Task name',
}
diff.apply(lines)
store.unload()

View file

@ -92,7 +92,7 @@ describe('store', function()
assert.are.equal(1, t1.id)
assert.are.equal(2, t2.id)
assert.are.equal('pending', t1.status)
assert.are.equal('Inbox', t1.category)
assert.are.equal('Todo', t1.category)
end)
it('uses provided category', function()

View file

@ -27,7 +27,7 @@ describe('views', function()
store.add({ description = 'Task A', category = 'Work' })
store.add({ description = 'Task B', category = 'Work' })
local lines, meta = views.category_view(store.active_tasks())
assert.are.equal('Work', lines[1])
assert.are.equal('## Work', lines[1])
assert.are.equal('header', meta[1].type)
assert.is_true(lines[2]:find('Task A') ~= nil)
assert.is_true(lines[3]:find('Task B') ~= nil)
@ -113,10 +113,10 @@ describe('views', function()
task_line = lines[i]
end
end
assert.are.equal('/1/ My task', task_line)
assert.are.equal('/1/- [ ] My task', task_line)
end)
it('formats priority task lines as /ID/ [N] description', function()
it('formats priority task lines as /ID/- [!] description', function()
store.add({ description = 'Important', category = 'Inbox', priority = 1 })
local lines, meta = views.category_view(store.active_tasks())
local task_line
@ -125,7 +125,7 @@ describe('views', function()
task_line = lines[i]
end
end
assert.are.equal('/1/ [1] Important', task_line)
assert.are.equal('/1/- [!] Important', task_line)
end)
it('sets LineMeta type=header for header lines with correct category', function()
@ -220,8 +220,8 @@ describe('views', function()
end
end
end
assert.are.equal('Work', first_header)
assert.are.equal('Inbox', second_header)
assert.are.equal('## Work', first_header)
assert.are.equal('## Inbox', second_header)
end)
it('appends categories not in category_order after ordered ones', function()
@ -236,8 +236,8 @@ describe('views', function()
table.insert(headers, lines[i])
end
end
assert.are.equal('Work', headers[1])
assert.are.equal('Errands', headers[2])
assert.are.equal('## Work', headers[1])
assert.are.equal('## Errands', headers[2])
end)
it('preserves insertion order when category_order is empty', function()
@ -250,8 +250,8 @@ describe('views', function()
table.insert(headers, lines[i])
end
end
assert.are.equal('Alpha', headers[1])
assert.are.equal('Beta', headers[2])
assert.are.equal('## Alpha', headers[1])
assert.are.equal('## Beta', headers[2])
end)
end)
@ -325,10 +325,10 @@ describe('views', function()
assert.is_true(earlier_row < later_row)
end)
it('formats task lines as /ID/ description', function()
it('formats task lines as /ID/- [ ] description', function()
store.add({ description = 'My task', category = 'Inbox' })
local lines, _ = views.priority_view(store.active_tasks())
assert.are.equal('/1/ My task', lines[1])
assert.are.equal('/1/- [ ] My task', lines[1])
end)
it('sets show_category=true for all task meta entries', function()

View file

@ -3,12 +3,12 @@ if exists('b:current_syntax')
endif
syntax match taskId /^\/\d\+\// conceal
syntax match taskHeader /^\S.*$/ contains=taskId
syntax match taskPriority /!\ze / contained
syntax match taskLine /^\/\d\+\/ .*$/ contains=taskId,taskPriority
syntax match taskHeader /^## .*$/ contains=taskId
syntax match taskCheckbox /\[!\]/ contained containedin=taskLine
syntax match taskLine /^\/\d\+\/- \[.\] .*$/ contains=taskId,taskCheckbox
highlight default link taskHeader PendingHeader
highlight default link taskPriority PendingPriority
highlight default link taskCheckbox PendingPriority
highlight default link taskLine Normal
let b:current_syntax = 'task'