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.
This commit is contained in:
Barrett Ruth 2026-02-26 15:50:56 -05:00
parent 233ff31df1
commit cf1f5c39d8
2 changed files with 43 additions and 53 deletions

View file

@ -21,50 +21,40 @@ end
---@return integer start_col ---@return integer start_col
---@return integer end_col ---@return integer end_col
function M.inner_task_range(line) function M.inner_task_range(line)
local prefix_end = line:find('/') and select(2, line:find('^/%d+/- %[.%] ')) local prefix_end = line:find('/') and select(2, line:find('^/%d+/%- %[.%] '))
if not prefix_end then if not prefix_end then
prefix_end = select(2, line:find('^- %[.%] ')) or 0 prefix_end = select(2, line:find('^%- %[.%] ')) or 0
end end
local start_col = prefix_end + 1 local start_col = prefix_end + 1
local dk = config.get().date_syntax or 'due' local dk = config.get().date_syntax or 'due'
local rk = config.get().recur_syntax or 'rec' local rk = config.get().recur_syntax or 'rec'
local dk_esc = vim.pesc(dk) local dk_pat = '^' .. vim.pesc(dk) .. ':%S+$'
local rk_esc = vim.pesc(rk) local rk_pat = '^' .. vim.pesc(rk) .. ':%S+$'
local desc_end = #line local rest = line:sub(start_col)
local tokens = {} local words = {}
for token in line:gmatch('%S+') do for word in rest:gmatch('%S+') do
table.insert(tokens, token) table.insert(words, word)
end end
local i = #tokens local i = #words
while i >= 1 do while i >= 1 do
local token = tokens[i] local word = words[i]
if token:match('^' .. dk_esc .. ':%S+$') if word:match(dk_pat) or word:match('^cat:%S+$') or word:match(rk_pat) then
or token:match('^cat:%S+$')
or token:match('^' .. rk_esc .. ':%S+$')
then
i = i - 1 i = i - 1
else else
break break
end end
end end
if i < #tokens then if i < 1 then
local rebuilt = {} return start_col, start_col
for j = 1, i do
table.insert(rebuilt, tokens[j])
end
local desc_text = table.concat(rebuilt, ' ')
local search_start = prefix_end
local found = line:find(desc_text, search_start + 1, true)
if found then
desc_end = found + #desc_text - 1
end
end end
return start_col, desc_end local desc = table.concat(words, ' ', 1, i)
local end_col = start_col + #desc - 1
return start_col, end_col
end end
---@param row integer ---@param row integer

View file

@ -18,8 +18,8 @@ describe('textobj', function()
describe('inner_task_range', function() describe('inner_task_range', function()
it('returns description range for task with id prefix', function() it('returns description range for task with id prefix', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(23, e) assert.are.equal(22, e)
end) end)
it('returns description range for task without id prefix', function() it('returns description range for task without id prefix', function()
@ -30,78 +30,78 @@ describe('textobj', function()
it('excludes trailing due: token', function() it('excludes trailing due: token', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries due:2026-03-15') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries due:2026-03-15')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(23, e) assert.are.equal(22, e)
end) end)
it('excludes trailing cat: token', function() it('excludes trailing cat: token', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries cat:Errands') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries cat:Errands')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(23, e) assert.are.equal(22, e)
end) end)
it('excludes trailing rec: token', function() it('excludes trailing rec: token', function()
local s, e = textobj.inner_task_range('/1/- [ ] Take out trash rec:weekly') local s, e = textobj.inner_task_range('/1/- [ ] Take out trash rec:weekly')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(25, e) assert.are.equal(23, e)
end) end)
it('excludes multiple trailing metadata tokens', function() it('excludes multiple trailing metadata tokens', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy milk due:2026-03-15 cat:Errands rec:weekly') local s, e = textobj.inner_task_range('/1/- [ ] Buy milk due:2026-03-15 cat:Errands rec:weekly')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(18, e) assert.are.equal(17, e)
end) end)
it('handles priority checkbox', function() it('handles priority checkbox', function()
local s, e = textobj.inner_task_range('/1/- [!] Important task') local s, e = textobj.inner_task_range('/1/- [!] Important task')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(24, e) assert.are.equal(23, e)
end) end)
it('handles done checkbox', function() it('handles done checkbox', function()
local s, e = textobj.inner_task_range('/1/- [x] Finished task') local s, e = textobj.inner_task_range('/1/- [x] Finished task')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(23, e) assert.are.equal(22, e)
end) end)
it('handles multi-digit task ids', function() it('handles multi-digit task ids', function()
local s, e = textobj.inner_task_range('/123/- [ ] Some task') local s, e = textobj.inner_task_range('/123/- [ ] Some task')
assert.are.equal(13, s) assert.are.equal(12, s)
assert.are.equal(21, e) assert.are.equal(20, e)
end) end)
it('does not strip non-metadata tokens', function() it('does not strip non-metadata tokens', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries for dinner') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries for dinner')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(34, e) assert.are.equal(33, e)
end) end)
it('stops stripping at first non-metadata token from right', function() it('stops stripping at first non-metadata token from right', function()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries for dinner due:2026-03-15') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries for dinner due:2026-03-15')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(34, e) assert.are.equal(33, e)
end) end)
it('respects custom date_syntax', function() it('respects custom date_syntax', function()
vim.g.pending = { date_syntax = 'by' } vim.g.pending = { date_syntax = 'by' }
config.reset() config.reset()
local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries by:2026-03-15') local s, e = textobj.inner_task_range('/1/- [ ] Buy groceries by:2026-03-15')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(23, e) assert.are.equal(22, e)
end) end)
it('respects custom recur_syntax', function() it('respects custom recur_syntax', function()
vim.g.pending = { recur_syntax = 'repeat' } vim.g.pending = { recur_syntax = 'repeat' }
config.reset() config.reset()
local s, e = textobj.inner_task_range('/1/- [ ] Take trash repeat:weekly') local s, e = textobj.inner_task_range('/1/- [ ] Take trash repeat:weekly')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(20, e) assert.are.equal(19, e)
end) end)
it('handles task with only metadata after description', function() it('handles task with only metadata after description', function()
local s, e = textobj.inner_task_range('/1/- [ ] X due:tomorrow') local s, e = textobj.inner_task_range('/1/- [ ] X due:tomorrow')
assert.are.equal(11, s) assert.are.equal(10, s)
assert.are.equal(11, e) assert.are.equal(10, e)
end) end)
end) end)
@ -129,7 +129,7 @@ describe('textobj', function()
} }
local h, l = textobj.category_bounds(2, meta) local h, l = textobj.category_bounds(2, meta)
assert.are.equal(1, h) assert.are.equal(1, h)
assert.are.equal(2, l) assert.are.equal(3, l)
end) end)
it('returns bounds for second category', function() it('returns bounds for second category', function()