diff --git a/CHANGELOG.md b/CHANGELOG.md index be3f47a..4de6db5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,7 @@ identical text remains. Parked comments still reattach on a matching reopen. - 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. ## [0.1.0] - 2026-09-30 diff --git a/lua/scratch_comments/actions.lua b/lua/scratch_comments/actions.lua index de65cee..8fbeef5 100644 --- a/lua/scratch_comments/actions.lua +++ b/lua/scratch_comments/actions.lua @@ -198,6 +198,7 @@ function M.clear() return end comments.clear() + require("scratch_comments.list").clear() notify.info("Cleared comments") end diff --git a/lua/scratch_comments/list.lua b/lua/scratch_comments/list.lua index 9ba1c74..54417b5 100644 --- a/lua/scratch_comments/list.lua +++ b/lua/scratch_comments/list.lua @@ -236,6 +236,15 @@ local function jump() }) 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 + -- Replacing items in history redraws the current list; empty lines do not. + vim.fn.setqflist({}, "r", { id = id, lines = {}, efm = "%m" }) + end +end + -- Update the manager behind an open editor, without taking focus or opening -- a list that the user already closed. Replace items in the same quickfix ID. ---@param id string diff --git a/lua/scratch_comments/ui/quickfix.lua b/lua/scratch_comments/ui/quickfix.lua index b491a5d..ff623c2 100644 --- a/lua/scratch_comments/ui/quickfix.lua +++ b/lua/scratch_comments/ui/quickfix.lua @@ -45,8 +45,9 @@ function M.list(views, orphans) end vim.fn.setqflist({}, " ", { title = "Comments", items = M.items(views, orphans) }) + local id = vim.fn.getqflist({ id = 0 }).id vim.cmd.copen() - return vim.fn.getqflist({ id = 0 }).id + return id end return M diff --git a/tests/features/show_delete_spec.lua b/tests/features/show_delete_spec.lua index e17e85b..fabc98a 100644 --- a/tests/features/show_delete_spec.lua +++ b/tests/features/show_delete_spec.lua @@ -240,5 +240,51 @@ describe(":CommentClear", function() local namespace = vim.api.nvim_get_namespaces().scratch_comments expect.equality(#store.all(), 0) expect.equality(#vim.api.nvim_buf_get_extmarks(source, namespace, 0, -1, {}), 0) + expect.equality(vim.fn.getqflist(), {}) + end) + + it("does not clear a list installed by a quickfix window hook", function() + local qf_buf = vim.api.nvim_get_current_buf() + vim.cmd.cclose() + vim.api.nvim_buf_delete(qf_buf, { force = true }) + local comment_list_id + local hook = vim.api.nvim_create_autocmd("FileType", { + pattern = "qf", + once = true, + callback = function() + comment_list_id = vim.fn.getqflist({ id = 0 }).id + vim.fn.setqflist({}, " ", { items = { { text = "keep this" } } }) + end, + }) + + vim.cmd.CommentList() + pcall(vim.api.nvim_del_autocmd, hook) + assert(comment_list_id, "expected the quickfix FileType hook to run") + vim.cmd.CommentClear() + expect.equality(vim.fn.getqflist()[1].text, "keep this") + expect.equality(vim.fn.getqflist({ id = comment_list_id, items = 1 }).items, {}) + 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({}, " ", { + title = "Other diagnostics", + items = { + { filename = "other.lua", lnum = 2, text = "first" }, + { filename = "other.lua", lnum = 3, text = "second" }, + }, + }) + local current_list = vim.fn.getqflist({ id = 0, items = 1, title = 1 }) + local qf_win = vim.api.nvim_get_current_win() + vim.api.nvim_win_set_cursor(qf_win, { 2, 0 }) + + vim.cmd.CommentClear() + expect.equality(vim.fn.getqflist({ id = comment_list_id, items = 1 }), { + id = comment_list_id, + items = {}, + }) + expect.equality(vim.fn.getqflist({ id = 0, items = 1, title = 1 }), current_list) + expect.equality(vim.api.nvim_get_current_win(), qf_win) + expect.equality(vim.api.nvim_win_get_cursor(qf_win), { 2, 0 }) end) end)