From d30773298b638c1035b7298dba888fcba55890c5 Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 30 Nov 2026 14:47:12 -0800 Subject: [PATCH] Leave quickfix lists with foreign rows alone --- CHANGELOG.md | 1 + lua/scratch_comments/list.lua | 43 +++++++++++++------ tests/features/list_export_spec.lua | 64 ++++++++++++++++------------- tests/features/show_delete_spec.lua | 24 +++++------ 4 files changed, 78 insertions(+), 54 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index f968a9c..5641ceb 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -16,6 +16,7 @@ - Keep comments with renamed source buffers so export and quickfix use the new name. - Fall back to filesystem lookup when Git cannot start. - Remove stale quickfix entries when clearing comments without touching other lists. +- Leave the quickfix list alone if it contains rows from another plugin. ### Changed diff --git a/lua/scratch_comments/list.lua b/lua/scratch_comments/list.lua index c9baf85..73957a6 100644 --- a/lua/scratch_comments/list.lua +++ b/lua/scratch_comments/list.lua @@ -75,10 +75,34 @@ local function by_id(id) end) end +local function row_comment_id(item) + local data = item and item.user_data + if type(data) == "table" and type(data.scratch_comments_id) == "string" then + return data.scratch_comments_id + end +end + +-- An ID survives in-place replacements; never rewrite a list with foreign rows. +---@param id integer +local function owns_list(id) + if id ~= list_id then + return false + end + local qf = vim.fn.getqflist({ id = id, items = 1 }) + if qf.id ~= id then + return false + end + return vim.iter(qf.items):all(row_comment_id) +end + ---@param expected_id integer? ---@return integer? local function manager_window(expected_id) - if not expected_id or vim.fn.getqflist({ id = 0 }).id ~= expected_id then + if + not expected_id + or vim.fn.getqflist({ id = 0 }).id ~= expected_id + or not owns_list(expected_id) + then return nil end if @@ -115,8 +139,7 @@ local function selected() end local qf = vim.fn.getqflist({ items = 1 }) local index = vim.api.nvim_win_get_cursor(0)[1] - local data = qf.items[index] and qf.items[index].user_data - return index, data and data.scratch_comments_id + return index, row_comment_id(qf.items[index]) end ---@param target ScratchJump @@ -143,10 +166,7 @@ local function try_jump(target) 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 + if manager_window(target.list_id) == target.list_win then vim.api.nvim_set_current_win(target.list_win) refresh(target.comment_id, target.list_row, target.list_id) end @@ -197,7 +217,7 @@ end -- Empty only the list we own, even if another list is current. function M.clear() local id = list_id - if id and vim.fn.getqflist({ id = id }).id == id then + if id and owns_list(id) then -- Replacing items in history redraws the current list; empty lines do not. vim.fn.setqflist({}, "r", { id = id, lines = {}, efm = "%m" }) end @@ -215,7 +235,7 @@ refresh = function(id, index, expected_id) end local anchored, orphans = filtered(active_filter) local items = quickfix.items(anchored, orphans) - vim.fn.setqflist({}, "r", { title = "Comments", items = items }) + vim.fn.setqflist({}, "r", { id = expected_id, items = items }) if #items == 0 then vim.api.nvim_win_call(qf_win, function() vim.cmd.cclose() @@ -241,8 +261,7 @@ function M.refresh_comment(id) return end local row = vim.api.nvim_win_get_cursor(qf_win)[1] - local item = vim.fn.getqflist({ items = 1 }).items[row] - local selected_id = item and item.user_data and item.user_data.scratch_comments_id + local selected_id = row_comment_id(vim.fn.getqflist({ items = 1 }).items[row]) refresh(selected_id or id or "", row, list_id) end @@ -389,7 +408,7 @@ function M.open(filter) if #anchored + #orphans == 0 then local qf_win = manager_window(list_id) if qf_win then - vim.fn.setqflist({}, "r", { items = {} }) + M.clear() vim.api.nvim_win_call(qf_win, function() vim.cmd.cclose() end) diff --git a/tests/features/list_export_spec.lua b/tests/features/list_export_spec.lua index b9da7cd..fa69aea 100644 --- a/tests/features/list_export_spec.lua +++ b/tests/features/list_export_spec.lua @@ -105,9 +105,11 @@ describe(":CommentList", function() end) describe("the comment list manager", function() - it("edits a comment in the same card, then refreshes the list", function() - vim.cmd("CommentList") + it("edits and refreshes a comment after the native quickfix index changes", function() + vim.cmd.CommentList() local id = vim.fn.getqflist()[1].user_data.scratch_comments_id + vim.fn.setqflist({}, "r", { idx = 2 }) + vim.api.nvim_win_set_cursor(0, { 1, 0 }) vim.cmd.Comment() expect.equality(helpers.text(), "on a") expect.equality(vim.bo.modifiable, true) @@ -402,22 +404,33 @@ describe("the comment list manager", function() vim.cmd.cclose() end) - it("does not open a card for a stale quickfix row", function() + it("does not open a card after another plugin replaces our quickfix rows in place", function() vim.cmd.CommentList() - local items = vim.fn.getqflist() - items[1].user_data = {} - vim.fn.setqflist({}, "r", { items = items }) + vim.fn.setqflist({}, "r", { items = { { text = "other diagnostics" } } }) vim.cmd.Comment() expect.equality(#helpers.every_float(), 0) - expect.equality(helpers.notified()[#helpers.notified()], "This comment is gone") - vim.cmd.cclose() + expect.equality(helpers.notified()[#helpers.notified()], "Not a Scratch Comments list") end) - it("does not delete another plugin's quickfix entry", function() + it("does not rewrite a replaced list when saving an open card", function() vim.cmd.CommentList() - vim.fn.setqflist({}, " ", { items = { { filename = "other.lua", lnum = 1, text = "other" } } }) + vim.cmd.Comment() + vim.fn.setqflist({}, "r", { items = { { text = "other diagnostics" } } }) + helpers.write("revised") + + expect.equality(store.all()[2].comment, "revised") + expect.equality(vim.fn.getqflist()[1].text, "other diagnostics") + end) + + it("does not delete a comment from a quickfix list mixed with diagnostics", function() + vim.cmd.CommentList() + vim.fn.setqflist({}, "a", { items = { { text = "other diagnostics" } } }) + local rows = vim.fn.getqflist() + vim.api.nvim_win_set_cursor(0, { 1, 0 }) + vim.cmd.CommentDelete() expect.equality(#store.all(), 2) + expect.equality(vim.fn.getqflist(), rows) vim.cmd.cclose() end) @@ -536,25 +549,18 @@ describe("the comment list manager", function() expect.equality(vim.api.nvim_get_current_win(), source_win) end) - it( - "does not enter a replacement quickfix list when a delayed read invalidates a location", - function() - local pending, qf_win = jump_to_pending_source("review-test:///replaced-list.lua") - local source_win = vim.api.nvim_get_current_win() - vim.fn.setqflist( - {}, - " ", - { items = { { filename = "other.lua", lnum = 1, text = "other" } } } - ) - vim.api.nvim_buf_set_lines(pending, 0, -1, false, { "first", "changed" }) - vim.api.nvim_exec_autocmds("BufReadPost", { buffer = pending }) - vim.wait(0) - expect.equality(store.all()[#store.all()].state, "inactive") - expect.equality(vim.api.nvim_get_current_win(), source_win) - expect.equality(vim.api.nvim_win_is_valid(qf_win), true) - expect.equality(vim.fn.getqflist()[1].text, "other") - end - ) + it("does not enter a list replaced in place after a delayed jump", function() + local pending, qf_win = jump_to_pending_source("review-test:///replaced-list.lua") + local source_win = vim.api.nvim_get_current_win() + vim.fn.setqflist({}, "r", { items = { { filename = "other.lua", lnum = 1, text = "other" } } }) + vim.api.nvim_buf_set_lines(pending, 0, -1, false, { "first", "changed" }) + vim.api.nvim_exec_autocmds("BufReadPost", { buffer = pending }) + vim.wait(0) + expect.equality(store.all()[#store.all()].state, "inactive") + expect.equality(vim.api.nvim_get_current_win(), source_win) + expect.equality(vim.api.nvim_win_is_valid(qf_win), true) + expect.equality(vim.fn.getqflist()[1].text, "other") + end) it("refreshes the list after leaving a delayed jump that becomes an orphan", function() local pending, qf_win = jump_to_pending_source("review-test:///left-source.lua") diff --git a/tests/features/show_delete_spec.lua b/tests/features/show_delete_spec.lua index fabc98a..37ef3c8 100644 --- a/tests/features/show_delete_spec.lua +++ b/tests/features/show_delete_spec.lua @@ -143,19 +143,6 @@ describe(":CommentDelete", function() expect.equality(helpers.notified()[#helpers.notified()], "Not a Scratch Comments list") end) - it("does not use an open card when the quickfix row is stale", function() - local qf_win = vim.api.nvim_get_current_win() - vim.cmd.Comment() - local items = vim.fn.getqflist() - items[2].user_data = {} - vim.fn.setqflist({}, "r", { items = items }) - vim.api.nvim_set_current_win(qf_win) - vim.api.nvim_win_set_cursor(qf_win, { 2, 0 }) - vim.cmd.CommentDelete() - expect.equality(#store.all(), 2) - expect.equality(helpers.notified()[#helpers.notified()], "This comment is gone") - end) - it("does nothing after a card has closed", function() vim.cmd.Comment() vim.cmd.quit() @@ -265,6 +252,17 @@ describe(":CommentClear", function() expect.equality(vim.fn.getqflist({ id = comment_list_id, items = 1 }).items, {}) end) + it("leaves a quickfix list alone when it contains both comments and diagnostics", function() + local id = vim.fn.getqflist({ id = 0 }).id + vim.fn.setqflist({}, "a", { items = { { text = "other diagnostics" } } }) + expect.equality(vim.fn.getqflist({ id = 0 }).id, id) + local rows = vim.fn.getqflist() + + vim.cmd.CommentClear() + expect.equality(#store.all(), 0) + expect.equality(vim.fn.getqflist(), rows) + end) + it("empties the comment list in history without changing the current list", function() local comment_list_id = vim.fn.getqflist({ id = 0 }).id vim.fn.setqflist({}, " ", { -- 2.54.1