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.
This commit is contained in:
Barrett Ruth 2026-02-24 23:14:41 -05:00
parent afb9e65f8d
commit fe2ee47b5e
2 changed files with 45 additions and 37 deletions

View file

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

View file

@ -25,12 +25,12 @@ describe('diff', function()
describe('parse_buffer', function() describe('parse_buffer', function()
it('parses headers and tasks', function() it('parses headers and tasks', function()
local lines = { local lines = {
'School', '## School',
'/1/ Do homework', '/1/- [ ] Do homework',
'/2/ ! Read chapter 5', '/2/- [!] Read chapter 5',
'', '',
'Errands', '## Errands',
'/3/ Buy groceries', '/3/- [ ] Buy groceries',
} }
local result = diff.parse_buffer(lines) local result = diff.parse_buffer(lines)
assert.are.equal(6, #result) assert.are.equal(6, #result)
@ -48,8 +48,8 @@ describe('diff', function()
it('handles new tasks without ids', function() it('handles new tasks without ids', function()
local lines = { local lines = {
'Inbox', '## Inbox',
' New task here', '- [ ] New task here',
} }
local result = diff.parse_buffer(lines) local result = diff.parse_buffer(lines)
assert.are.equal(2, #result) assert.are.equal(2, #result)
@ -62,9 +62,9 @@ describe('diff', function()
describe('apply', function() describe('apply', function()
it('creates new tasks from buffer lines', function() it('creates new tasks from buffer lines', function()
local lines = { local lines = {
'Inbox', '## Inbox',
' First task', '- [ ] First task',
' Second task', '- [ ] Second task',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -80,8 +80,8 @@ describe('diff', function()
store.add({ description = 'Delete me' }) store.add({ description = 'Delete me' })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Keep me', '/1/- [ ] Keep me',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -97,8 +97,8 @@ describe('diff', function()
store.add({ description = 'Original' }) store.add({ description = 'Original' })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Renamed', '/1/- [ ] Renamed',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -111,9 +111,9 @@ describe('diff', function()
store.add({ description = 'Original' }) store.add({ description = 'Original' })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Original', '/1/- [ ] Original',
'/1/ Copy of original', '/1/- [ ] Copy of original',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -126,8 +126,8 @@ describe('diff', function()
store.add({ description = 'Moving task', category = 'Inbox' }) store.add({ description = 'Moving task', category = 'Inbox' })
store.save() store.save()
local lines = { local lines = {
'Work', '## Work',
'/1/ Moving task', '/1/- [ ] Moving task',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -140,8 +140,8 @@ describe('diff', function()
store.add({ description = 'Stable task', category = 'Inbox' }) store.add({ description = 'Stable task', category = 'Inbox' })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Stable task', '/1/- [ ] Stable task',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -158,8 +158,8 @@ describe('diff', function()
store.add({ description = 'Pay bill', due = '2026-03-15' }) store.add({ description = 'Pay bill', due = '2026-03-15' })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Pay bill', '/1/- [ ] Pay bill',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()
@ -172,8 +172,8 @@ describe('diff', function()
store.add({ description = 'Task name', priority = 1 }) store.add({ description = 'Task name', priority = 1 })
store.save() store.save()
local lines = { local lines = {
'Inbox', '## Inbox',
'/1/ Task name', '/1/- [ ] Task name',
} }
diff.apply(lines) diff.apply(lines)
store.unload() store.unload()