Use one retry path for delayed quickfix jumps #16
No files matched your search
@@ -21,6 +21,7 @@
|
||||
|
||||
- Simplify quickfix setup and remove unused test helpers and redundant tests.
|
||||
- Share card layout code between resizing and moving to another split.
|
||||
- Simplify delayed quickfix jumps by using the existing source reattachment callback.
|
||||
|
||||
## [0.1.0] - 2026-09-30
|
||||
|
||||
|
||||
+80
-111
@@ -1,4 +1,5 @@
|
||||
local actions = require("scratch_comments.actions")
|
||||
local anchors = require("scratch_comments.model.anchors")
|
||||
local comments = require("scratch_comments.comments")
|
||||
local store = require("scratch_comments.model.store")
|
||||
local notify = require("scratch_comments.ui.notify")
|
||||
@@ -13,8 +14,16 @@ local list_bufnr
|
||||
local list_win
|
||||
local list_tab
|
||||
local active_filter
|
||||
local pending_jump = {}
|
||||
local listening = {}
|
||||
---@class ScratchJump
|
||||
---@field comment_id string
|
||||
---@field source_buf integer
|
||||
---@field source_win integer
|
||||
---@field list_id integer
|
||||
---@field list_win integer
|
||||
---@field list_row integer
|
||||
|
||||
---@type ScratchJump?
|
||||
local pending_jump
|
||||
local refresh
|
||||
local bind_list
|
||||
|
||||
@@ -110,30 +119,55 @@ local function selected()
|
||||
return index, data and data.scratch_comments_id
|
||||
end
|
||||
|
||||
local function jump()
|
||||
-- Only the most recent manager jump may move the cursor after a delayed read.
|
||||
for bufnr in pairs(pending_jump) do
|
||||
pending_jump[bufnr] = nil
|
||||
---@param target ScratchJump
|
||||
local function try_jump(target)
|
||||
if pending_jump ~= target then
|
||||
return
|
||||
end
|
||||
if
|
||||
vim.api.nvim_get_current_win() ~= target.source_win
|
||||
or vim.api.nvim_get_current_buf() ~= target.source_buf
|
||||
then
|
||||
pending_jump = nil
|
||||
return
|
||||
end
|
||||
local comment = by_id(target.comment_id)
|
||||
if comment and comment.state == "parked" then
|
||||
return -- source_updated() will retry after the read
|
||||
end
|
||||
pending_jump = nil
|
||||
if not comment then
|
||||
return
|
||||
end
|
||||
local line, _, col = anchors.range(comment)
|
||||
if line and comment.bufnr == target.source_buf then
|
||||
vim.api.nvim_win_set_cursor(target.source_win, { line, col or 0 })
|
||||
elseif comment.state == "inactive" or anchors.is_orphaned(comment) then
|
||||
if
|
||||
vim.api.nvim_win_is_valid(target.list_win)
|
||||
and vim.fn.getqflist({ id = 0 }).id == target.list_id
|
||||
then
|
||||
vim.api.nvim_set_current_win(target.list_win)
|
||||
refresh(target.comment_id, target.list_row, target.list_id)
|
||||
end
|
||||
notify.warn("This comment no longer has a location; use :Comment to edit it")
|
||||
end
|
||||
end
|
||||
|
||||
local function jump()
|
||||
pending_jump = nil
|
||||
local index, id = selected()
|
||||
if not index then
|
||||
-- Another plugin replaced our quickfix list; leave its normal navigation usable.
|
||||
vim.cmd.cc(vim.api.nvim_win_get_cursor(0)[1])
|
||||
return
|
||||
end
|
||||
if not id then
|
||||
notify.warn("This comment is gone")
|
||||
return
|
||||
end
|
||||
local comment = by_id(id)
|
||||
local comment = id and by_id(id)
|
||||
if not comment then
|
||||
notify.warn("This comment is gone")
|
||||
return
|
||||
end
|
||||
local entry = vim.iter(views.anchored()):find(function(view)
|
||||
return view.id == id
|
||||
end)
|
||||
if not entry then
|
||||
if comment.state ~= "parked" and not anchors.range(comment) then
|
||||
notify.warn("This comment has no source location; use :Comment to edit it")
|
||||
return
|
||||
end
|
||||
@@ -149,91 +183,15 @@ local function jump()
|
||||
notify.warn("Could not open the comment source")
|
||||
return
|
||||
end
|
||||
local bufnr, win = vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win()
|
||||
local expected_id = list_id
|
||||
local function return_to_manager()
|
||||
if vim.api.nvim_win_is_valid(qf_win) and vim.fn.getqflist({ id = 0 }).id == expected_id then
|
||||
vim.api.nvim_set_current_win(qf_win)
|
||||
refresh(id, index, expected_id)
|
||||
end
|
||||
end
|
||||
-- Loading a parked source can reveal that its saved location is gone.
|
||||
if vim.iter(views.orphans()):find(function(orphan)
|
||||
return orphan.id == id
|
||||
end) then
|
||||
return_to_manager()
|
||||
notify.warn("This comment no longer has a location; use :Comment to edit it")
|
||||
return
|
||||
end
|
||||
local function position()
|
||||
if not vim.api.nvim_win_is_valid(win) or vim.api.nvim_win_get_buf(win) ~= bufnr then
|
||||
return true
|
||||
end
|
||||
if vim.api.nvim_get_current_win() ~= win then
|
||||
return true
|
||||
end
|
||||
local current = by_id(id)
|
||||
if not current then
|
||||
return true
|
||||
end
|
||||
local view = vim.iter(views.in_buffer(bufnr)):find(function(v)
|
||||
return v.id == id
|
||||
end)
|
||||
if view then
|
||||
vim.api.nvim_win_set_cursor(win, { view.start_line, view.start_col or 0 })
|
||||
return true
|
||||
end
|
||||
if current.state == "inactive" then
|
||||
return_to_manager()
|
||||
notify.warn("This comment no longer has a location; use :Comment to edit it")
|
||||
return true
|
||||
end
|
||||
return false
|
||||
end
|
||||
if position() then
|
||||
return
|
||||
end
|
||||
-- An async BufReadCmd may fill the buffer after :cc has already moved the
|
||||
-- cursor. Keep one listener per buffer even if the user jumps repeatedly.
|
||||
pending_jump[bufnr] = position
|
||||
-- A delayed read must not reposition a source the user has left and
|
||||
-- returned to in the meantime.
|
||||
vim.api.nvim_create_autocmd("WinLeave", {
|
||||
once = true,
|
||||
callback = function()
|
||||
if pending_jump[bufnr] == position then
|
||||
pending_jump[bufnr] = nil
|
||||
end
|
||||
end,
|
||||
})
|
||||
-- BufReadPost reattachment runs on the next scheduled turn. Recheck after
|
||||
-- it, even if the file was fully populated before our on_lines listener.
|
||||
vim.schedule(function()
|
||||
if pending_jump[bufnr] == position and position() then
|
||||
pending_jump[bufnr] = nil
|
||||
end
|
||||
end)
|
||||
if listening[bufnr] then
|
||||
return
|
||||
end
|
||||
listening[bufnr] = vim.api.nvim_buf_attach(bufnr, false, {
|
||||
on_lines = function()
|
||||
local current = pending_jump[bufnr]
|
||||
if not current then
|
||||
listening[bufnr] = nil
|
||||
return true
|
||||
end
|
||||
vim.schedule(function()
|
||||
if pending_jump[bufnr] == current and current() then
|
||||
pending_jump[bufnr] = nil
|
||||
end
|
||||
end)
|
||||
end,
|
||||
on_detach = function()
|
||||
pending_jump[bufnr] = nil
|
||||
listening[bufnr] = nil
|
||||
end,
|
||||
})
|
||||
pending_jump = {
|
||||
comment_id = comment.id,
|
||||
source_buf = vim.api.nvim_get_current_buf(),
|
||||
source_win = vim.api.nvim_get_current_win(),
|
||||
list_id = list_id,
|
||||
list_win = qf_win,
|
||||
list_row = index,
|
||||
}
|
||||
try_jump(pending_jump)
|
||||
end
|
||||
|
||||
-- Empty only the list we own, even if another list is current.
|
||||
@@ -288,17 +246,14 @@ function M.refresh_comment(id)
|
||||
refresh(selected_id or id or "", row, list_id)
|
||||
end
|
||||
|
||||
-- Recheck a pending navigation after reattachment, which can happen after
|
||||
-- the last on_lines notification from an asynchronous source.
|
||||
-- The source watcher reports when a parked comment reattaches or is orphaned.
|
||||
---@param bufnr integer
|
||||
function M.source_updated(bufnr)
|
||||
M.refresh_comment()
|
||||
local position = pending_jump[bufnr]
|
||||
if position then
|
||||
local target = pending_jump
|
||||
if target and target.source_buf == bufnr then
|
||||
vim.schedule(function()
|
||||
if pending_jump[bufnr] == position and position() then
|
||||
pending_jump[bufnr] = nil
|
||||
end
|
||||
try_jump(target)
|
||||
end)
|
||||
end
|
||||
end
|
||||
@@ -396,8 +351,24 @@ bind_list = function(bufnr)
|
||||
vim.keymap.set("n", "<Esc>", escape, { buffer = bufnr, desc = "Dismiss comment card" })
|
||||
end
|
||||
|
||||
local group = vim.api.nvim_create_augroup("scratch_comments_list", { clear = true })
|
||||
|
||||
vim.api.nvim_create_autocmd({ "WinLeave", "BufLeave", "BufUnload" }, {
|
||||
group = group,
|
||||
callback = function(args)
|
||||
-- Returning to the source later should not revive an abandoned jump.
|
||||
if
|
||||
pending_jump
|
||||
and args.buf == pending_jump.source_buf
|
||||
and (args.event == "BufUnload" or vim.api.nvim_get_current_win() == pending_jump.source_win)
|
||||
then
|
||||
pending_jump = nil
|
||||
end
|
||||
end,
|
||||
})
|
||||
|
||||
vim.api.nvim_create_autocmd({ "WinEnter", "BufWinEnter" }, {
|
||||
group = vim.api.nvim_create_augroup("scratch_comments_list", { clear = true }),
|
||||
group = group,
|
||||
callback = function()
|
||||
if vim.bo.buftype == "quickfix" and vim.api.nvim_get_current_tabpage() == list_tab then
|
||||
manager_window(list_id)
|
||||
@@ -412,9 +383,7 @@ function M.open(filter)
|
||||
notify.warn("Save or discard the comment first")
|
||||
return
|
||||
end
|
||||
for bufnr in pairs(pending_jump) do
|
||||
pending_jump[bufnr] = nil
|
||||
end
|
||||
pending_jump = nil
|
||||
active_filter = filter
|
||||
local anchored, orphans = filtered(filter)
|
||||
if #anchored + #orphans == 0 then
|
||||
|
||||
@@ -568,36 +568,30 @@ describe("the comment list manager", function()
|
||||
end)
|
||||
|
||||
it("doesn't move the source cursor after leaving an asynchronous jump", function()
|
||||
local url = "review-test:///late-cursor.lua"
|
||||
local pending
|
||||
vim.api.nvim_create_autocmd("BufReadCmd", {
|
||||
pattern = "review-test:///*",
|
||||
once = true,
|
||||
callback = function(args)
|
||||
pending = args.buf
|
||||
end,
|
||||
})
|
||||
vim.cmd.enew()
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "first", "second" })
|
||||
vim.bo.bufhidden = "wipe"
|
||||
vim.cmd("2Comment")
|
||||
helpers.write("late")
|
||||
vim.cmd("enew!")
|
||||
vim.cmd.CommentList()
|
||||
local qf_win = vim.api.nvim_get_current_win()
|
||||
local index = vim.iter(vim.fn.getqflist()):enumerate():find(function(_, item)
|
||||
return item.user_data.scratch_comments_id == store.all()[#store.all()].id
|
||||
end)
|
||||
vim.api.nvim_win_set_cursor(0, { index, 0 })
|
||||
vim.cmd("normal \r")
|
||||
local editor_win = vim.api.nvim_get_current_win()
|
||||
local source_win = vim.fn.bufwinid(url)
|
||||
local pending, qf_win = jump_to_pending_source("review-test:///late-cursor.lua")
|
||||
local source_win = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_win_set_cursor(source_win, { 1, 0 })
|
||||
vim.api.nvim_set_current_win(qf_win)
|
||||
vim.api.nvim_set_current_win(editor_win)
|
||||
vim.api.nvim_set_current_win(source_win)
|
||||
vim.api.nvim_buf_set_lines(pending, 0, -1, false, { "first", "second" })
|
||||
vim.wait(0)
|
||||
expect.equality(helpers.sign_at(2), "│")
|
||||
expect.equality(vim.api.nvim_win_get_cursor(source_win)[1], 1)
|
||||
end)
|
||||
|
||||
it("doesn't move the cursor after switching buffers during a delayed jump", function()
|
||||
local pending = jump_to_pending_source("review-test:///switched-buffer.lua")
|
||||
local source_win = vim.api.nvim_get_current_win()
|
||||
vim.bo[pending].bufhidden = "hide"
|
||||
vim.cmd.enew()
|
||||
vim.api.nvim_buf_set_lines(pending, 0, -1, false, { "first" })
|
||||
vim.wait(0)
|
||||
vim.cmd.buffer(pending)
|
||||
vim.api.nvim_win_set_cursor(source_win, { 1, 0 })
|
||||
vim.api.nvim_buf_set_lines(pending, 1, -1, false, { "second" })
|
||||
vim.wait(0)
|
||||
|
||||
expect.equality(helpers.sign_at(2), "│")
|
||||
expect.equality(vim.api.nvim_win_get_cursor(source_win)[1], 1)
|
||||
end)
|
||||
|
||||
|
||||
Reference in new issue
Block a user