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 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)
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