feat: text objects and motions for the pending buffer (#39)
* feat: text objects and motions for the pending buffer
Problem: the pending buffer has action-button mappings but no Vim
grammar. You cannot dat to delete a task, cit to change a description,
or ]] to jump to the next category header.
Solution: add textobj.lua with at/it (a task / inner task), aC/iC
(a category / inner category), ]]/[[ (next/prev header), and ]t/[t
(next/prev task). All text objects work in operator-pending and visual
modes; motions work in normal, visual, and operator-pending. Mappings
are configurable via the keymaps table and exposed as <Plug> mappings.
* fix(textobj): escape Lua pattern hyphen, fix test expectations
Problem: inner_task_range used unescaped '-' in Lua patterns, which
acts as a lazy quantifier instead of matching a literal hyphen. The
metadata-stripping logic also tokenized the full line including the
prefix, so the rebuilt string could never be found after the prefix.
All test column expectations were off by one.
Solution: escape hyphens with %-, rewrite metadata stripping to
tokenize only the description portion after the prefix, and correct
all test assertions to match actual rendered column positions.
* feat(textobj): add debug mode, rename priority view buffer
Problem: the ]] motion reportedly lands one line past the header in
some environments, and ]t/[t may not override Neovim defaults. No
way to diagnose these at runtime. Also, pending://priority is a poor
buffer name for the flat ranked view.
Solution: add a debug config option (vim.g.pending = { debug = true })
that logs meta state, cursor positions, and mapping registration to
:messages at DEBUG level. Rename the buffer from pending://priority to
pending://queue. Internal view identifier stays 'priority'.
* docs: text objects, motions, debug mode, queue view rename
Problem: vimdoc had no documentation for the new text objects, motions,
debug config, or the pending://queue buffer rename.
Solution: add text object and motion tables to the mappings section,
document all eight <Plug> mappings, add debug field to the config
reference, update config example with new keymap defaults, rename
priority view references to queue throughout the vimdoc.
* fix(textobj): use correct config variable, raise log level
Problem: motion keymaps (]], [[, ]t, [t) were never set because
`config.get().debug` referenced an undefined `config` variable,
crashing _setup_buf_mappings before the motion loop. Debug logging
also used vim.log.levels.DEBUG which is filtered by default.
Solution: replace `config` with `cfg` (already in scope) and raise
both debug notify calls from DEBUG to INFO.
* ci: formt
This commit is contained in:
parent
c57cc0845b
commit
302bf8126f
7 changed files with 778 additions and 5 deletions
|
|
@ -223,7 +223,8 @@ 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 view_label = current_view == 'priority' and 'queue' or current_view
|
||||
vim.api.nvim_buf_set_name(bufnr, 'pending://' .. view_label)
|
||||
local tasks = store.active_tasks()
|
||||
|
||||
local lines, line_meta
|
||||
|
|
|
|||
|
|
@ -11,6 +11,14 @@
|
|||
---@field undo? string|false
|
||||
---@field open_line? string|false
|
||||
---@field open_line_above? string|false
|
||||
---@field a_task? string|false
|
||||
---@field i_task? string|false
|
||||
---@field a_category? string|false
|
||||
---@field i_category? string|false
|
||||
---@field next_header? string|false
|
||||
---@field prev_header? string|false
|
||||
---@field next_task? string|false
|
||||
---@field prev_task? string|false
|
||||
|
||||
---@class pending.Config
|
||||
---@field data_path string
|
||||
|
|
@ -22,6 +30,7 @@
|
|||
---@field someday_date string
|
||||
---@field category_order? string[]
|
||||
---@field drawer_height? integer
|
||||
---@field debug? boolean
|
||||
---@field keymaps pending.Keymaps
|
||||
---@field gcal? pending.GcalConfig
|
||||
|
||||
|
|
@ -47,6 +56,14 @@ local defaults = {
|
|||
undo = 'U',
|
||||
open_line = 'o',
|
||||
open_line_above = 'O',
|
||||
a_task = 'at',
|
||||
i_task = 'it',
|
||||
a_category = 'aC',
|
||||
i_category = 'iC',
|
||||
next_header = ']]',
|
||||
prev_header = '[[',
|
||||
next_task = ']t',
|
||||
prev_task = '[t',
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -88,6 +88,72 @@ function M._setup_buf_mappings(bufnr)
|
|||
vim.keymap.set('n', key --[[@as string]], fn, opts)
|
||||
end
|
||||
end
|
||||
|
||||
local textobj = require('pending.textobj')
|
||||
|
||||
---@type table<string, { modes: string[], fn: fun(count: integer), visual_fn?: fun(count: integer) }>
|
||||
local textobjs = {
|
||||
a_task = {
|
||||
modes = { 'o', 'x' },
|
||||
fn = textobj.a_task,
|
||||
visual_fn = textobj.a_task_visual,
|
||||
},
|
||||
i_task = {
|
||||
modes = { 'o', 'x' },
|
||||
fn = textobj.i_task,
|
||||
visual_fn = textobj.i_task_visual,
|
||||
},
|
||||
a_category = {
|
||||
modes = { 'o', 'x' },
|
||||
fn = textobj.a_category,
|
||||
visual_fn = textobj.a_category_visual,
|
||||
},
|
||||
i_category = {
|
||||
modes = { 'o', 'x' },
|
||||
fn = textobj.i_category,
|
||||
visual_fn = textobj.i_category_visual,
|
||||
},
|
||||
}
|
||||
|
||||
for name, spec in pairs(textobjs) do
|
||||
local key = km[name]
|
||||
if key and key ~= false then
|
||||
for _, mode in ipairs(spec.modes) do
|
||||
if mode == 'x' and spec.visual_fn then
|
||||
vim.keymap.set(mode, key --[[@as string]], function()
|
||||
spec.visual_fn(vim.v.count1)
|
||||
end, opts)
|
||||
else
|
||||
vim.keymap.set(mode, key --[[@as string]], function()
|
||||
spec.fn(vim.v.count1)
|
||||
end, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@type table<string, fun(count: integer)>
|
||||
local motions = {
|
||||
next_header = textobj.next_header,
|
||||
prev_header = textobj.prev_header,
|
||||
next_task = textobj.next_task,
|
||||
prev_task = textobj.prev_task,
|
||||
}
|
||||
|
||||
for name, fn in pairs(motions) do
|
||||
local key = km[name]
|
||||
if cfg.debug then
|
||||
vim.notify(
|
||||
('[pending] mapping motion %s → %s (buf=%d)'):format(name, key or 'nil', bufnr),
|
||||
vim.log.levels.INFO
|
||||
)
|
||||
end
|
||||
if key and key ~= false then
|
||||
vim.keymap.set({ 'n', 'x', 'o' }, key --[[@as string]], function()
|
||||
fn(vim.v.count1)
|
||||
end, opts)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
|
|
|
|||
384
lua/pending/textobj.lua
Normal file
384
lua/pending/textobj.lua
Normal file
|
|
@ -0,0 +1,384 @@
|
|||
local buffer = require('pending.buffer')
|
||||
local config = require('pending.config')
|
||||
|
||||
---@class pending.textobj
|
||||
local M = {}
|
||||
|
||||
---@param ... any
|
||||
---@return nil
|
||||
local function dbg(...)
|
||||
if config.get().debug then
|
||||
vim.notify('[pending.textobj] ' .. string.format(...), vim.log.levels.INFO)
|
||||
end
|
||||
end
|
||||
|
||||
---@param lnum integer
|
||||
---@param meta pending.LineMeta[]
|
||||
---@return string
|
||||
local function get_line_from_buf(lnum, meta)
|
||||
local _ = meta
|
||||
local bufnr = buffer.bufnr()
|
||||
if not bufnr then
|
||||
return ''
|
||||
end
|
||||
local lines = vim.api.nvim_buf_get_lines(bufnr, lnum - 1, lnum, false)
|
||||
return lines[1] or ''
|
||||
end
|
||||
|
||||
---@param line string
|
||||
---@return integer start_col
|
||||
---@return integer end_col
|
||||
function M.inner_task_range(line)
|
||||
local prefix_end = line:find('/') and select(2, line:find('^/%d+/%- %[.%] '))
|
||||
if not prefix_end then
|
||||
prefix_end = select(2, line:find('^%- %[.%] ')) or 0
|
||||
end
|
||||
local start_col = prefix_end + 1
|
||||
|
||||
local dk = config.get().date_syntax or 'due'
|
||||
local rk = config.get().recur_syntax or 'rec'
|
||||
local dk_pat = '^' .. vim.pesc(dk) .. ':%S+$'
|
||||
local rk_pat = '^' .. vim.pesc(rk) .. ':%S+$'
|
||||
|
||||
local rest = line:sub(start_col)
|
||||
local words = {}
|
||||
for word in rest:gmatch('%S+') do
|
||||
table.insert(words, word)
|
||||
end
|
||||
|
||||
local i = #words
|
||||
while i >= 1 do
|
||||
local word = words[i]
|
||||
if word:match(dk_pat) or word:match('^cat:%S+$') or word:match(rk_pat) then
|
||||
i = i - 1
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
if i < 1 then
|
||||
return start_col, start_col
|
||||
end
|
||||
|
||||
local desc = table.concat(words, ' ', 1, i)
|
||||
local end_col = start_col + #desc - 1
|
||||
return start_col, end_col
|
||||
end
|
||||
|
||||
---@param row integer
|
||||
---@param meta pending.LineMeta[]
|
||||
---@return integer? header_row
|
||||
---@return integer? last_row
|
||||
function M.category_bounds(row, meta)
|
||||
if not meta or #meta == 0 then
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local header_row = nil
|
||||
local m = meta[row]
|
||||
if not m then
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
if m.type == 'header' then
|
||||
header_row = row
|
||||
else
|
||||
for r = row, 1, -1 do
|
||||
if meta[r] and meta[r].type == 'header' then
|
||||
header_row = r
|
||||
break
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
if not header_row then
|
||||
return nil, nil
|
||||
end
|
||||
|
||||
local last_row = header_row
|
||||
local total = #meta
|
||||
for r = header_row + 1, total do
|
||||
if meta[r].type == 'header' then
|
||||
break
|
||||
end
|
||||
last_row = r
|
||||
end
|
||||
|
||||
return header_row, last_row
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.a_task(count)
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local m = meta[row]
|
||||
if not m or m.type ~= 'task' then
|
||||
return
|
||||
end
|
||||
|
||||
local start_row = row
|
||||
local end_row = row
|
||||
count = math.max(1, count)
|
||||
for _ = 2, count do
|
||||
local next_row = end_row + 1
|
||||
if next_row > #meta then
|
||||
break
|
||||
end
|
||||
if meta[next_row] and meta[next_row].type == 'task' then
|
||||
end_row = next_row
|
||||
else
|
||||
break
|
||||
end
|
||||
end
|
||||
|
||||
vim.cmd('normal! ' .. start_row .. 'GV' .. end_row .. 'G')
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.a_task_visual(count)
|
||||
vim.cmd('normal! \27')
|
||||
M.a_task(count)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.i_task(count)
|
||||
local _ = count
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local m = meta[row]
|
||||
if not m or m.type ~= 'task' then
|
||||
return
|
||||
end
|
||||
|
||||
local line = get_line_from_buf(row, meta)
|
||||
local start_col, end_col = M.inner_task_range(line)
|
||||
if start_col > end_col then
|
||||
return
|
||||
end
|
||||
|
||||
vim.api.nvim_win_set_cursor(0, { row, start_col - 1 })
|
||||
vim.cmd('normal! v')
|
||||
vim.api.nvim_win_set_cursor(0, { row, end_col - 1 })
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.i_task_visual(count)
|
||||
vim.cmd('normal! \27')
|
||||
M.i_task(count)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.a_category(count)
|
||||
local _ = count
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local view = buffer.current_view_name()
|
||||
if view == 'priority' then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local header_row, last_row = M.category_bounds(row, meta)
|
||||
if not header_row or not last_row then
|
||||
return
|
||||
end
|
||||
|
||||
local start_row = header_row
|
||||
if header_row > 1 and meta[header_row - 1] and meta[header_row - 1].type == 'blank' then
|
||||
start_row = header_row - 1
|
||||
end
|
||||
local end_row = last_row
|
||||
if last_row < #meta and meta[last_row + 1] and meta[last_row + 1].type == 'blank' then
|
||||
end_row = last_row + 1
|
||||
end
|
||||
|
||||
vim.cmd('normal! ' .. start_row .. 'GV' .. end_row .. 'G')
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.a_category_visual(count)
|
||||
vim.cmd('normal! \27')
|
||||
M.a_category(count)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.i_category(count)
|
||||
local _ = count
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local view = buffer.current_view_name()
|
||||
if view == 'priority' then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
local header_row, last_row = M.category_bounds(row, meta)
|
||||
if not header_row or not last_row then
|
||||
return
|
||||
end
|
||||
|
||||
local first_task = nil
|
||||
local last_task = nil
|
||||
for r = header_row + 1, last_row do
|
||||
if meta[r] and meta[r].type == 'task' then
|
||||
if not first_task then
|
||||
first_task = r
|
||||
end
|
||||
last_task = r
|
||||
end
|
||||
end
|
||||
|
||||
if not first_task or not last_task then
|
||||
return
|
||||
end
|
||||
|
||||
vim.cmd('normal! ' .. first_task .. 'GV' .. last_task .. 'G')
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.i_category_visual(count)
|
||||
vim.cmd('normal! \27')
|
||||
M.i_category(count)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.next_header(count)
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local view = buffer.current_view_name()
|
||||
if view == 'priority' then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
dbg('next_header: cursor=%d, meta_len=%d, view=%s', row, #meta, view or 'nil')
|
||||
local found = 0
|
||||
count = math.max(1, count)
|
||||
for r = row + 1, #meta do
|
||||
if meta[r] and meta[r].type == 'header' then
|
||||
found = found + 1
|
||||
dbg(
|
||||
'next_header: found header at row=%d, cat=%s, found=%d/%d',
|
||||
r,
|
||||
meta[r].category or '?',
|
||||
found,
|
||||
count
|
||||
)
|
||||
if found == count then
|
||||
vim.api.nvim_win_set_cursor(0, { r, 0 })
|
||||
dbg('next_header: cursor set to row=%d, actual=%d', r, vim.api.nvim_win_get_cursor(0)[1])
|
||||
return
|
||||
end
|
||||
else
|
||||
dbg('next_header: row=%d type=%s', r, meta[r] and meta[r].type or 'nil')
|
||||
end
|
||||
end
|
||||
dbg('next_header: no header found after row=%d', row)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.prev_header(count)
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
local view = buffer.current_view_name()
|
||||
if view == 'priority' then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
dbg('prev_header: cursor=%d, meta_len=%d', row, #meta)
|
||||
local found = 0
|
||||
count = math.max(1, count)
|
||||
for r = row - 1, 1, -1 do
|
||||
if meta[r] and meta[r].type == 'header' then
|
||||
found = found + 1
|
||||
dbg(
|
||||
'prev_header: found header at row=%d, cat=%s, found=%d/%d',
|
||||
r,
|
||||
meta[r].category or '?',
|
||||
found,
|
||||
count
|
||||
)
|
||||
if found == count then
|
||||
vim.api.nvim_win_set_cursor(0, { r, 0 })
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.next_task(count)
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
dbg('next_task: cursor=%d, meta_len=%d', row, #meta)
|
||||
local found = 0
|
||||
count = math.max(1, count)
|
||||
for r = row + 1, #meta do
|
||||
if meta[r] and meta[r].type == 'task' then
|
||||
found = found + 1
|
||||
if found == count then
|
||||
dbg('next_task: jumping to row=%d', r)
|
||||
vim.api.nvim_win_set_cursor(0, { r, 0 })
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
dbg('next_task: no task found after row=%d', row)
|
||||
end
|
||||
|
||||
---@param count integer
|
||||
---@return nil
|
||||
function M.prev_task(count)
|
||||
local meta = buffer.meta()
|
||||
if not meta or #meta == 0 then
|
||||
return
|
||||
end
|
||||
|
||||
local row = vim.api.nvim_win_get_cursor(0)[1]
|
||||
dbg('prev_task: cursor=%d, meta_len=%d', row, #meta)
|
||||
local found = 0
|
||||
count = math.max(1, count)
|
||||
for r = row - 1, 1, -1 do
|
||||
if meta[r] and meta[r].type == 'task' then
|
||||
found = found + 1
|
||||
if found == count then
|
||||
dbg('prev_task: jumping to row=%d', r)
|
||||
vim.api.nvim_win_set_cursor(0, { r, 0 })
|
||||
return
|
||||
end
|
||||
end
|
||||
end
|
||||
dbg('prev_task: no task found before row=%d', row)
|
||||
end
|
||||
|
||||
return M
|
||||
Loading…
Add table
Add a link
Reference in a new issue