pending.nvim/lua/pending/buffer.lua
Barrett Ruth 3a35fab6cf feat: overdue highlighting, relative dates, undo write, buffer mappings (#1)
* feat(config): add category_order field

Problem: category display order was always insertion order with no way
to configure it.

Solution: add category_order to config defaults so users can declare a
preferred category ordering; unspecified categories append after.

* feat(parse): add relative date resolution

Problem: due dates required full YYYY-MM-DD input, adding friction for
common cases like "today" or "next monday".

Solution: add resolve_date() supporting today, tomorrow, +Nd, and
weekday abbreviations; extend inline token parsing to resolve relative
values before falling back to strict date validation.

* feat(views): overdue flag, category in priority view, category ordering

Problem: overdue tasks were visually indistinct from upcoming ones;
priority view had no category context; category display order was not
configurable.

Solution: compute overdue meta flag for pending tasks past their due
date; set show_category on priority view task meta; reorder categories
according to config.category_order when present.

* feat(buffer): overdue highlight, category virt text in priority view

Problem: overdue tasks had no visual distinction; priority view showed
no category context alongside due dates.

Solution: add PendingOverdue highlight group; render category name as
right-aligned virtual text in priority view, composited with the due
date when both are present.

* feat(init): undo write and buffer-local default mappings

Problem: _undo_state was captured on every save but never consumed;
toggle_priority and prompt_date had no buffer-local defaults, requiring
manual <Plug> configuration.

Solution: implement undo_write() to restore pre-save task state; add !,
d, and U as buffer-local defaults following fugitive's philosophy of
owning the buffer; expose :Pending undo as a command alias.

* test(views): add views spec

Problem: views.lua had no test coverage.

Solution: add 26 tests covering category_view and priority_view
including sort order, line format, overdue detection, show_category
meta, and category_order config behavior.

* test(archive): add archive spec

Problem: archive had no test coverage.

Solution: add 9 tests covering cutoff logic, custom day counts, pending
task preservation, deleted task cleanup, and notify output.

* docs: add vimdoc

Problem: no :help documentation existed.

Solution: add doc/pending.txt covering all features — commands,
mappings, views, configuration, Google Calendar sync, highlight groups,
data format, and health check — following standard vimdoc conventions.

* ci: format

* fix: resolve lint and type check errors

Problem: selene flagged unused variables in new spec files; LuaLS
flagged os.date/os.time return type mismatches, integer? assignments,
and stale task.Task/task.GcalConfig type references.

Solution: prefix unused spec variables with _ or drop unnecessary
assignments; add --[[@as string/integer]] casts for os.date and
os.time calls; add category_order field to pending.Config annotation;
fix task.GcalConfig -> pending.GcalConfig and task.Task[] ->
pending.Task[]; add nil guards on meta[row].id before store calls;
cast store.data() return to non-optional.

* ci: format

* fix: sync

* ci: format
2026-02-24 18:33:07 -05:00

211 lines
5.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 = {}
---@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 /^\S.*$/ contains=taskId
syntax match taskPriority /! / contained containedin=taskLine
syntax match taskLine /^\/\d\+\/ .*$/ contains=taskId,taskPriority
]])
end)
end
---@param bufnr integer
local function setup_indentexpr(bufnr)
vim.bo[bufnr].indentexpr = 'v:lua.require("pending.buffer").get_indent()'
end
---@return integer
function M.get_indent()
local lnum = vim.v.lnum
if lnum <= 1 then
return 0
end
local prev = vim.fn.getline(lnum - 1)
if prev == '' or prev:match('^%S') then
return 0
end
return 2
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 = 'right_align',
})
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',
})
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
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()
local function hl(name, opts)
if vim.fn.hlexists(name) == 0 or vim.tbl_isempty(vim.api.nvim_get_hl(0, { name = name })) then
vim.api.nvim_set_hl(0, name, opts)
end
end
hl('PendingHeader', { bold = true })
hl('PendingDue', { fg = '#888888', italic = true })
hl('PendingOverdue', { fg = '#e06c75', italic = true })
hl('PendingDone', { strikethrough = true, fg = '#666666' })
hl('PendingPriority', { fg = '#e06c75', bold = true })
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
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
vim.bo[bufnr].modifiable = true
vim.api.nvim_buf_set_lines(bufnr, 0, -1, false, lines)
vim.bo[bufnr].modified = false
setup_syntax(bufnr)
apply_extmarks(bufnr, line_meta)
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)
vim.api.nvim_buf_set_name(task_bufnr, 'pending://')
set_buf_options(task_bufnr)
setup_indentexpr(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