fix(init): preserve cursor column and position in mutation functions (#152)

Problem: `toggle_complete()`, `toggle_priority()`, `adjust_priority()`,
`toggle_status()`, and `move_task()` captured only the row from
`nvim_win_get_cursor` and restored the cursor to column 0 after
re-render. Additionally, `toggle_complete()` followed the toggled task
to its new sorted position at the bottom of the category, which is
disorienting when working through a list of tasks.

Solution: Capture both row and column from the cursor, and restore the
column in all five functions. For `toggle_complete()`, instead of
chasing the task ID after render, clamp the cursor to the original row
(or total lines if shorter) and advance to the nearest task line,
similar to the `]t` motion in `textobj.lua`.
This commit is contained in:
Barrett Ruth 2026-03-12 20:19:18 -04:00 committed by Barrett Ruth
parent 9593ab7fe8
commit ea59bbae96
Signed by: barrett
GPG key ID: A6C96C9349D2FC81

View file

@ -544,7 +544,8 @@ function M.toggle_complete()
if not require_saved() then if not require_saved() then
return return
end end
local row = vim.api.nvim_win_get_cursor(0)[1] local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local meta = buffer.meta() local meta = buffer.meta()
if not meta[row] or meta[row].type ~= 'task' then if not meta[row] or meta[row].type ~= 'task' then
return return
@ -578,12 +579,26 @@ function M.toggle_complete()
end end
_save_and_notify() _save_and_notify()
buffer.render(bufnr) buffer.render(bufnr)
for lnum, m in ipairs(buffer.meta()) do local new_meta = buffer.meta()
if m.id == id then local total = #new_meta
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) local target = math.min(row, total)
break if new_meta[target] and new_meta[target].type == 'task' then
vim.api.nvim_win_set_cursor(0, { target, col })
else
for r = target, total do
if new_meta[r] and new_meta[r].type == 'task' then
vim.api.nvim_win_set_cursor(0, { r, col })
return
end end
end end
for r = target, 1, -1 do
if new_meta[r] and new_meta[r].type == 'task' then
vim.api.nvim_win_set_cursor(0, { r, col })
return
end
end
vim.api.nvim_win_set_cursor(0, { target, col })
end
end end
---@param id_str? string ---@param id_str? string
@ -654,7 +669,8 @@ function M.toggle_priority()
if not require_saved() then if not require_saved() then
return return
end end
local row = vim.api.nvim_win_get_cursor(0)[1] local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local meta = buffer.meta() local meta = buffer.meta()
if not meta[row] or meta[row].type ~= 'task' then if not meta[row] or meta[row].type ~= 'task' then
return return
@ -675,7 +691,7 @@ function M.toggle_priority()
buffer.render(bufnr) buffer.render(bufnr)
for lnum, m in ipairs(buffer.meta()) do for lnum, m in ipairs(buffer.meta()) do
if m.id == id then if m.id == id then
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) vim.api.nvim_win_set_cursor(0, { lnum, col })
break break
end end
end end
@ -691,7 +707,8 @@ local function adjust_priority(delta)
if not require_saved() then if not require_saved() then
return return
end end
local row = vim.api.nvim_win_get_cursor(0)[1] local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local meta = buffer.meta() local meta = buffer.meta()
if not meta[row] or meta[row].type ~= 'task' then if not meta[row] or meta[row].type ~= 'task' then
return return
@ -715,7 +732,7 @@ local function adjust_priority(delta)
buffer.render(bufnr) buffer.render(bufnr)
for lnum, m in ipairs(buffer.meta()) do for lnum, m in ipairs(buffer.meta()) do
if m.id == id then if m.id == id then
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) vim.api.nvim_win_set_cursor(0, { lnum, col })
break break
end end
end end
@ -829,7 +846,8 @@ function M.toggle_status(target_status)
if not require_saved() then if not require_saved() then
return return
end end
local row = vim.api.nvim_win_get_cursor(0)[1] local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local meta = buffer.meta() local meta = buffer.meta()
if not meta[row] or meta[row].type ~= 'task' then if not meta[row] or meta[row].type ~= 'task' then
return return
@ -852,7 +870,7 @@ function M.toggle_status(target_status)
buffer.render(bufnr) buffer.render(bufnr)
for lnum, m in ipairs(buffer.meta()) do for lnum, m in ipairs(buffer.meta()) do
if m.id == id then if m.id == id then
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) vim.api.nvim_win_set_cursor(0, { lnum, col })
break break
end end
end end
@ -868,7 +886,8 @@ function M.move_task(direction)
if not require_saved() then if not require_saved() then
return return
end end
local row = vim.api.nvim_win_get_cursor(0)[1] local cursor = vim.api.nvim_win_get_cursor(0)
local row, col = cursor[1], cursor[2]
local meta = buffer.meta() local meta = buffer.meta()
if not meta[row] or meta[row].type ~= 'task' then if not meta[row] or meta[row].type ~= 'task' then
return return
@ -943,7 +962,7 @@ function M.move_task(direction)
for lnum, m in ipairs(buffer.meta()) do for lnum, m in ipairs(buffer.meta()) do
if m.id == id then if m.id == id then
vim.api.nvim_win_set_cursor(0, { lnum, 0 }) vim.api.nvim_win_set_cursor(0, { lnum, col })
break break
end end
end end