Leave invalidated URI comment anchors orphaned #11
No files matched your search
@@ -11,6 +11,8 @@
|
||||
- Keep a draft open and reject its first save if the source was edited,
|
||||
reloaded, or saved with pending changes.
|
||||
- Restore comments when reopening a multi-line URI buffer with a blank first line.
|
||||
- Keep comments orphaned when a live URI edit invalidates their range, even if
|
||||
identical text remains. Parked comments still reattach on a matching reopen.
|
||||
|
||||
## [0.1.0] - 2026-09-30
|
||||
|
||||
|
||||
@@ -86,8 +86,6 @@ function M.clear()
|
||||
store.clear()
|
||||
end
|
||||
|
||||
-- Remember the latest live range for parking, or for a URI provider that
|
||||
-- replaces an already-attached line without changing its contents.
|
||||
---@param comment ScratchComment
|
||||
---@param bufnr integer
|
||||
---@return boolean
|
||||
@@ -122,8 +120,19 @@ function M.park_buffer(bufnr)
|
||||
end
|
||||
end
|
||||
|
||||
-- An active source may reopen under a different bufnr. Its old line number is
|
||||
-- only trusted when the saved text still occupies that exact range.
|
||||
---@param bufnr integer
|
||||
---@param comment ScratchComment
|
||||
local function saved_text_matches(bufnr, comment)
|
||||
local where = comment.last_location
|
||||
if where.end_line > vim.api.nvim_buf_line_count(bufnr) then
|
||||
return false
|
||||
end
|
||||
local ok, text =
|
||||
pcall(views.text, bufnr, where.start_line, where.end_line, where.start_col, where.end_col)
|
||||
return ok and table.concat(text, "\n") == comment.snippet
|
||||
end
|
||||
|
||||
-- A parked comment reattaches only if its saved text is still at that range.
|
||||
---@param bufnr integer
|
||||
---@param definitive? boolean true after a completed ordinary file read
|
||||
function M.attach_buffer(bufnr, definitive)
|
||||
@@ -142,27 +151,9 @@ function M.attach_buffer(bufnr, definitive)
|
||||
end
|
||||
local changed = false
|
||||
for _, comment in ipairs(store.all()) do
|
||||
local where = comment.last_location
|
||||
local matches = false
|
||||
if
|
||||
(comment.source_name == name or comment.bufnr == bufnr)
|
||||
and where.end_line <= vim.api.nvim_buf_line_count(bufnr)
|
||||
then
|
||||
local ok, text =
|
||||
pcall(views.text, bufnr, where.start_line, where.end_line, where.start_col, where.end_col)
|
||||
matches = ok and table.concat(text, "\n") == comment.snippet
|
||||
end
|
||||
-- A provider may replace an already-matching line without changing its
|
||||
-- text. Reanchor that exact range; a changed or deleted line is an orphan.
|
||||
if is_uri and comment.bufnr == bufnr and anchors.is_orphaned(comment) then
|
||||
if matches then
|
||||
anchors.clear_all({ comment })
|
||||
anchors.anchor(comment, where.start_line, where.end_line, where.start_col, where.end_col)
|
||||
end
|
||||
changed = true
|
||||
end
|
||||
if comment.source_name == name and comment.state == "parked" then
|
||||
if matches then
|
||||
if saved_text_matches(bufnr, comment) then
|
||||
local where = comment.last_location
|
||||
comment.bufnr = bufnr
|
||||
anchors.anchor(comment, where.start_line, where.end_line, where.start_col, where.end_col)
|
||||
comment.state = nil
|
||||
@@ -171,6 +162,9 @@ function M.attach_buffer(bufnr, definitive)
|
||||
comment.state = "inactive"
|
||||
changed = true
|
||||
end
|
||||
elseif is_uri and comment.bufnr == bufnr and anchors.is_orphaned(comment) then
|
||||
-- Refresh the list, but leave the invalid mark in place for undo.
|
||||
changed = true
|
||||
end
|
||||
end
|
||||
if changed then
|
||||
@@ -228,24 +222,16 @@ vim.api.nvim_create_autocmd("BufEnter", {
|
||||
end
|
||||
if not watched[bufnr] then
|
||||
watched[bufnr] = vim.api.nvim_buf_attach(bufnr, false, {
|
||||
on_lines = function(_, _, _, first, last, new_last)
|
||||
-- The next provider change can invalidate the mark before a
|
||||
-- scheduled snapshot runs. Track line shifts above it immediately.
|
||||
for _, comment in ipairs(store.in_buffer(bufnr)) do
|
||||
local where = comment.last_location
|
||||
if first <= where.start_line - 1 and last <= where.start_line - 1 then
|
||||
local delta = new_last - last
|
||||
where.start_line = where.start_line + delta
|
||||
where.end_line = where.end_line + delta
|
||||
end
|
||||
end
|
||||
on_lines = function()
|
||||
vim.schedule(function()
|
||||
if vim.api.nvim_buf_is_valid(bufnr) then
|
||||
for _, comment in ipairs(store.in_buffer(bufnr)) do
|
||||
snapshot(comment, bufnr)
|
||||
end
|
||||
M.attach_buffer(bufnr)
|
||||
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||
return
|
||||
end
|
||||
-- Keep the last live snippet searchable if the comment becomes an orphan.
|
||||
for _, comment in ipairs(store.in_buffer(bufnr)) do
|
||||
snapshot(comment, bufnr)
|
||||
end
|
||||
M.attach_buffer(bufnr)
|
||||
end)
|
||||
if
|
||||
not paths.is_uri(name)
|
||||
|
||||
@@ -209,80 +209,40 @@ describe("a file's comments", function()
|
||||
expect.equality(views.in_buffer(vim.api.nvim_get_current_buf())[1].comment, "virtual note")
|
||||
end)
|
||||
|
||||
it("makes a reattached URI comment an orphan when its line is deleted", function()
|
||||
it("orphans a deleted URI comment instead of moving it to an identical line", function()
|
||||
local url = "review-test:///edited-after-attach.lua"
|
||||
local lines = { "first", "same", "same", "last" }
|
||||
vim.cmd.enew()
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
|
||||
vim.bo.bufhidden = "wipe"
|
||||
vim.cmd("2Comment")
|
||||
helpers.write("on two")
|
||||
helpers.write("on the first copy")
|
||||
vim.cmd("enew!")
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, lines)
|
||||
vim.api.nvim_exec_autocmds("BufEnter", { buffer = 0 })
|
||||
local source, source_win = vim.api.nvim_get_current_buf(), vim.api.nvim_get_current_win()
|
||||
vim.wait(0)
|
||||
expect.equality(store.all()[#store.all()].state, nil)
|
||||
local source_win = vim.api.nvim_get_current_win()
|
||||
vim.cmd.CommentList()
|
||||
expect.equality(views.in_buffer(source)[1].start_line, 2)
|
||||
-- Edit both copies without replacing their lines; filter by the live snippet.
|
||||
vim.api.nvim_buf_set_text(source, 1, 1, 1, 1, { "-new-" })
|
||||
vim.api.nvim_buf_set_text(source, 2, 1, 2, 1, { "-new-" })
|
||||
vim.wait(0)
|
||||
vim.cmd.CommentList("new")
|
||||
vim.api.nvim_win_call(source_win, function()
|
||||
vim.api.nvim_buf_set_lines(0, 1, 2, false, {})
|
||||
helpers.undo_break()
|
||||
vim.cmd("2delete")
|
||||
end)
|
||||
vim.wait(0)
|
||||
expect.equality(#views.orphans(), 1)
|
||||
local items = vim.fn.getqflist()
|
||||
expect.equality(items[#items].text, "[orphaned] on two")
|
||||
end)
|
||||
expect.equality(views.orphans()[1] and views.orphans()[1].comment, "on the first copy")
|
||||
expect.equality(vim.fn.getqflist()[1].text, "[orphaned] on the first copy")
|
||||
|
||||
it("uses the latest live URI range after a provider replaces its lines", function()
|
||||
local url = "review-test:///live-range.lua"
|
||||
vim.cmd.enew()
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.bo.bufhidden = "wipe"
|
||||
vim.cmd("2Comment")
|
||||
helpers.write("on two")
|
||||
vim.cmd("enew!")
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.api.nvim_exec_autocmds("BufEnter", { buffer = 0 })
|
||||
vim.wait(0)
|
||||
local comment = store.all()[#store.all()]
|
||||
expect.equality(comment.state, nil)
|
||||
vim.api.nvim_buf_set_text(0, 1, 1, 1, 2, { "E" })
|
||||
vim.wait(0)
|
||||
expect.equality(comment.snippet, "tEo")
|
||||
vim.api.nvim_buf_set_lines(0, 0, 0, false, { "leading" })
|
||||
vim.wait(0)
|
||||
expect.equality(comment.last_location.start_line, 3)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "leading", "one", "tEo" })
|
||||
vim.wait(0)
|
||||
expect.equality(comment.state, nil)
|
||||
expect.equality(views.in_buffer(vim.api.nvim_get_current_buf())[1].start_line, 3)
|
||||
end)
|
||||
|
||||
it("keeps a URI anchor through batched movement and provider replacement", function()
|
||||
local url = "review-test:///batched.lua"
|
||||
vim.cmd.enew()
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.bo.bufhidden = "wipe"
|
||||
vim.cmd("2Comment")
|
||||
helpers.write("on two")
|
||||
vim.cmd("enew!")
|
||||
vim.api.nvim_buf_set_name(0, url)
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" })
|
||||
vim.api.nvim_exec_autocmds("BufEnter", { buffer = 0 })
|
||||
vim.wait(0)
|
||||
local comment = store.all()[#store.all()]
|
||||
vim.api.nvim_buf_set_lines(0, 0, 0, false, { "first" })
|
||||
vim.wait(0)
|
||||
vim.api.nvim_buf_set_lines(0, 0, 0, false, { "second" })
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "second", "first", "one", "two" })
|
||||
vim.wait(0)
|
||||
expect.equality(comment.state, nil)
|
||||
expect.equality(comment.last_location.start_line, 4)
|
||||
expect.equality(views.in_buffer(vim.api.nvim_get_current_buf())[1].start_line, 4)
|
||||
vim.api.nvim_win_call(source_win, function()
|
||||
vim.cmd("silent undo")
|
||||
end)
|
||||
expect.equality(#views.orphans(), 0)
|
||||
expect.equality(views.in_buffer(source)[1].start_line, 2)
|
||||
end)
|
||||
|
||||
it("parks a moved range at its current location and reattaches there", function()
|
||||
@@ -343,11 +303,11 @@ describe("a file's comments", function()
|
||||
vim.wait(0)
|
||||
expect.equality(store.all()[#store.all()].state, nil)
|
||||
expect.equality(views.in_buffer(pending)[1].start_line, 2)
|
||||
-- A provider may replace an already-matching partial result again.
|
||||
-- A later replacement is a live edit, not another reattachment.
|
||||
vim.api.nvim_buf_set_lines(pending, 1, 2, false, { "two" })
|
||||
vim.wait(0)
|
||||
expect.equality(store.all()[#store.all()].state, nil)
|
||||
expect.equality(views.in_buffer(pending)[1].start_line, 2)
|
||||
expect.equality(#views.in_buffer(pending), 0)
|
||||
expect.equality(views.orphans()[1].comment, "delayed")
|
||||
end)
|
||||
|
||||
it("marks a changed saved range inactive after reopening", function()
|
||||
|
||||
Reference in new issue
Block a user