pending.nvim/spec/diff_spec.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

223 lines
6.2 KiB
Lua

require('spec.helpers')
local config = require('pending.config')
local store = require('pending.store')
describe('diff', function()
local tmpdir
local diff = require('pending.diff')
before_each(function()
tmpdir = vim.fn.tempname()
vim.fn.mkdir(tmpdir, 'p')
vim.g.pending = { data_path = tmpdir .. '/tasks.json' }
config.reset()
store.unload()
store.load()
end)
after_each(function()
vim.fn.delete(tmpdir, 'rf')
vim.g.pending = nil
config.reset()
end)
describe('parse_buffer', function()
it('parses headers and tasks', function()
local lines = {
'## School',
'/1/- [ ] Do homework',
'/2/- [!] Read chapter 5',
'',
'## Errands',
'/3/- [ ] Buy groceries',
}
local result = diff.parse_buffer(lines)
assert.are.equal(6, #result)
assert.are.equal('header', result[1].type)
assert.are.equal('School', result[1].category)
assert.are.equal('task', result[2].type)
assert.are.equal(1, result[2].id)
assert.are.equal('Do homework', result[2].description)
assert.are.equal('School', result[2].category)
assert.are.equal('task', result[3].type)
assert.are.equal(1, result[3].priority)
assert.are.equal('blank', result[4].type)
assert.are.equal('Errands', result[6].category)
end)
it('handles new tasks without ids', function()
local lines = {
'## Inbox',
'- [ ] New task here',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
assert.are.equal('task', result[2].type)
assert.is_nil(result[2].id)
assert.are.equal('New task here', result[2].description)
end)
it('inline cat: token overrides header category', function()
local lines = {
'## Inbox',
'/1/- [ ] Buy milk cat:Work',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
assert.are.equal('task', result[2].type)
assert.are.equal('Work', result[2].category)
end)
it('inline due: token is parsed', function()
local lines = {
'## Inbox',
'/1/- [ ] Buy milk due:2026-03-15',
}
local result = diff.parse_buffer(lines)
assert.are.equal(2, #result)
assert.are.equal('task', result[2].type)
assert.are.equal('2026-03-15', result[2].due)
end)
end)
describe('apply', function()
it('creates new tasks from buffer lines', function()
local lines = {
'## Inbox',
'- [ ] First task',
'- [ ] Second task',
}
diff.apply(lines)
store.unload()
store.load()
local tasks = store.active_tasks()
assert.are.equal(2, #tasks)
assert.are.equal('First task', tasks[1].description)
assert.are.equal('Second task', tasks[2].description)
end)
it('deletes tasks removed from buffer', function()
store.add({ description = 'Keep me' })
store.add({ description = 'Delete me' })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Keep me',
}
diff.apply(lines)
store.unload()
store.load()
local active = store.active_tasks()
assert.are.equal(1, #active)
assert.are.equal('Keep me', active[1].description)
local deleted = store.get(2)
assert.are.equal('deleted', deleted.status)
end)
it('updates modified tasks', function()
store.add({ description = 'Original' })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Renamed',
}
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.are.equal('Renamed', task.description)
end)
it('updates modified when description is renamed', function()
local t = store.add({ description = 'Original', category = 'Inbox' })
t.modified = '2020-01-01T00:00:00Z'
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Renamed',
}
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.are.equal('Renamed', task.description)
assert.is_not.equal('2020-01-01T00:00:00Z', task.modified)
end)
it('handles duplicate ids as copies', function()
store.add({ description = 'Original' })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Original',
'/1/- [ ] Copy of original',
}
diff.apply(lines)
store.unload()
store.load()
local tasks = store.active_tasks()
assert.are.equal(2, #tasks)
end)
it('moves tasks between categories', function()
store.add({ description = 'Moving task', category = 'Inbox' })
store.save()
local lines = {
'## Work',
'/1/- [ ] Moving task',
}
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.are.equal('Work', task.category)
end)
it('does not update modified when task is unchanged', function()
store.add({ description = 'Stable task', category = 'Inbox' })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Stable task',
}
diff.apply(lines)
store.unload()
store.load()
local modified_after_first = store.get(1).modified
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.are.equal(modified_after_first, task.modified)
end)
it('clears due when removed from buffer line', function()
store.add({ description = 'Pay bill', due = '2026-03-15' })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Pay bill',
}
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.is_nil(task.due)
end)
it('clears priority when [N] is removed from buffer line', function()
store.add({ description = 'Task name', priority = 1 })
store.save()
local lines = {
'## Inbox',
'/1/- [ ] Task name',
}
diff.apply(lines)
store.unload()
store.load()
local task = store.get(1)
assert.are.equal(0, task.priority)
end)
end)
end)