Keep one comment card open at a time #8

Merged
eleith merged 1 commits from fix/single-comment-card into main 2026-11-30 16:58:03 +00:00
8 changed files with 343 additions and 69 deletions

No files matched your search

+2
View File
@@ -6,6 +6,8 @@
- Keep unsaved comment drafts open when saving fails because the source buffer
was unloaded or wiped, or because the comment being edited was deleted.
- Keep only one comment card open. Save or discard unsaved edits before opening
another card or clearing comments.
## [0.1.0] - 2026-09-30
+10 -1
View File
@@ -65,6 +65,7 @@ end
---@param source_win integer
---@param on_close? fun()
---@param on_saved? fun()
---@return integer? comment_win
function M.show(view, source_win, on_close, on_saved)
local frame
if view.start_line then
@@ -75,7 +76,7 @@ function M.show(view, source_win, on_close, on_saved)
frame.on_save = function(text)
return save_existing(view.id, text)
end
window.open(frame, {
return window.open(frame, {
id = view.id,
bufnr = view.bufnr,
source_name = view.source_name,
@@ -141,6 +142,10 @@ end
---@param use_selection? boolean
function M.comment(start_line, end_line, use_selection)
local bufnr = vim.api.nvim_get_current_buf()
if window.is_card_buffer(bufnr) then
notify.warn("Open comments from a source buffer or the comment list")
return
end
if vim.bo[bufnr].buftype == "quickfix" then
if not require("scratch_comments.list").edit_selected() then
notify.warn("Not a Scratch Comments list")
@@ -183,6 +188,10 @@ function M.toggle(on)
end
function M.clear()
if not window.close() then
notify.warn("Save or discard the comment first")
return
end
comments.clear()
notify.info("Cleared comments")
end
+9 -21
View File
@@ -314,15 +314,11 @@ function M.edit_selected()
notify.warn("This comment is gone")
return true
end
if not window.close() then
notify.warn("Save or discard the comment first")
return true
end
local qf_win = vim.api.nvim_get_current_win()
local tab = vim.api.nvim_get_current_tabpage()
local expected_id = list_id
local parent_win
actions.show(entry, qf_win, function()
local comment_win = actions.show(entry, qf_win, function()
vim.schedule(function()
local focused = vim.api.nvim_get_current_win()
if
@@ -336,9 +332,8 @@ function M.edit_selected()
end, function()
refresh(id, index, expected_id)
end)
local open = window.current()
if open and open.id == id then
parent_win = vim.api.nvim_win_get_config(open.comment_win).win
if comment_win then
parent_win = vim.api.nvim_win_get_config(comment_win).win
end
return true
end
@@ -362,15 +357,16 @@ function M.delete_selected()
notify.warn(index and "This comment is gone" or "No comment card open")
return
end
if open and open.id == id and vim.bo[vim.api.nvim_win_get_buf(open.comment_win)].modified then
notify.warn("Save or discard the comment first")
return
end
local comment = by_id(id)
if not comment then
notify.warn("This comment is gone")
return
end
-- Refreshing quickfix can run hooks that open another card.
if open and open.id == id and not window.close(index == nil) then
notify.warn("Save or discard the comment first")
return
end
comments.delete(comment)
notify.info("Deleted comment")
if index then
@@ -378,18 +374,10 @@ function M.delete_selected()
elseif open and open.on_saved then
open.on_saved()
end
if open and open.id == id then
if index then
-- A command run from the list should leave focus there.
window.close()
else
vim.api.nvim_win_close(open.comment_win, true)
end
end
end
local function escape()
if window.current() and not window.close() then
if not window.close() then
notify.warn("Save with :w, or discard with :q!")
end
end
+45 -17
View File
@@ -34,6 +34,7 @@ end
---@field comment_win integer
---@field comment_buf integer
---@field backdrop_win? integer
---@field close fun(): boolean closes the frame unless it contains unsaved edits
-- Two stacked floats, centered: the commented lines above, the comment below.
---@param frame ScratchFrame
@@ -155,6 +156,39 @@ function M.open(frame, parent)
local group = vim.api.nvim_create_augroup("scratch_comments_frame_" .. comment_win, {})
local tab = vim.api.nvim_win_get_tabpage(parent)
local closed = false
local function teardown()
if closed then
return
end
closed = true
vim.api.nvim_del_augroup_by_id(group)
pcall(vim.api.nvim_win_close, context_win, true)
backdrop.close(dim)
if frame.on_close then
frame.on_close()
end
end
local function modified()
return vim.api.nvim_buf_is_valid(comment_buf) and vim.bo[comment_buf].modified
end
local function close()
if modified() then
return false
end
if vim.api.nvim_win_is_valid(comment_win) then
pcall(vim.api.nvim_win_close, comment_win, false)
if vim.api.nvim_win_is_valid(comment_win) then
return false
end
end
-- WinClosed can be suppressed or interrupted by a failing autocmd.
teardown()
return true
end
local function watch_parent()
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
@@ -186,8 +220,7 @@ function M.open(frame, parent)
end
local replacement = file_win or other_win
if not replacement and qf_win then
if not vim.bo[comment_buf].modified then
vim.api.nvim_win_close(comment_win, true)
if close() then
return
end
-- Preserve unsaved text even if the editor cannot make a split.
@@ -216,11 +249,12 @@ function M.open(frame, parent)
vim.api.nvim_create_autocmd({ "VimResized", "WinResized" }, {
group = group,
callback = function()
if
not (vim.api.nvim_win_is_valid(context_win) and vim.api.nvim_win_is_valid(comment_win))
then
if not vim.api.nvim_win_is_valid(comment_win) then
teardown()
return
end
if not vim.api.nvim_win_is_valid(context_win) then
backdrop.close(dim)
pcall(vim.api.nvim_del_augroup_by_id, group)
return
end
if not vim.api.nvim_win_is_valid(parent) then
@@ -234,23 +268,16 @@ function M.open(frame, parent)
})
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
pattern = tostring(comment_win),
once = true,
callback = function()
pcall(vim.api.nvim_win_close, context_win, true)
backdrop.close(dim)
pcall(vim.api.nvim_del_augroup_by_id, group)
if frame.on_close then
frame.on_close()
end
end,
callback = teardown,
})
vim.api.nvim_create_autocmd("WinClosed", {
group = group,
pattern = tostring(context_win),
once = true,
callback = function()
-- Not forced, so an unsaved comment stays open instead of being lost.
pcall(vim.api.nvim_win_close, comment_win, false)
close()
end,
})
@@ -260,6 +287,7 @@ function M.open(frame, parent)
comment_win = comment_win,
comment_buf = comment_buf,
backdrop_win = dim,
close = close,
}
return panes
end
+75 -28
View File
@@ -23,8 +23,26 @@ local mode_names = {
---@field on_close? fun()
---@field on_saved? fun()
---@type ScratchCommentWindow?
---@type { record: ScratchCommentWindow, panes: ScratchFramePanes }?
local current
local changing_card = false
-- Opening or closing a frame can synchronously run user autocmds.
---@generic T
---@param change fun(): T
---@return T?
local function transition(change)
if changing_card then
return nil
end
changing_card = true
local ok, result = pcall(change)
changing_card = false
if not ok then
error(result, 0)
end
return result
end
-- A quickfix row with no source location still edits in the file area, while
-- closing its card returns focus to the list.
@@ -66,12 +84,33 @@ local function editing_window(record)
return vim.api.nvim_get_current_win()
end
-- Close the owned frame even if its editor window now shows another buffer.
---@param restore_focus? boolean
local function dismiss(restore_focus)
if not current then
return true
end
local record, panes = current.record, current.panes
if restore_focus then
return panes.close()
end
-- A command replacing the card (e.g. :CommentExport!) owns the next focus.
local on_close = record.on_close
record.on_close = nil
local closed = panes.close()
if not closed then
record.on_close = on_close
end
return closed
end
---@param spec ScratchFrame
---@param record ScratchCommentWindow
function M.open(spec, record)
---@return integer? comment_win
local function open_card(spec, record)
local save = assert(spec.on_save)
spec.on_close = function()
if current and current.comment_win == record.comment_win then
if current and current.record == record then
current = nil
end
if record.on_close then
@@ -85,12 +124,12 @@ function M.open(spec, record)
spec.comment_name = source_name .. ":" .. record.line .. " [comment]"
local parent = editing_window(record)
if not parent then
return
return nil
end
local panes = frame.open(spec, parent)
local comment_win, comment_buf = panes.comment_win, panes.comment_buf
record.comment_win = comment_win
current = record
current = { record = record, panes = panes }
local function show_mode()
if vim.api.nvim_win_is_valid(comment_win) then
@@ -108,11 +147,9 @@ function M.open(spec, record)
for _, buf in ipairs({ comment_buf, panes.context_buf }) do
vim.keymap.set("n", "<Esc>", function()
if vim.bo[comment_buf].modified then
if not transition(panes.close) then
notify.warn("Save with :w, or discard with :q!")
return
end
pcall(vim.api.nvim_win_close, comment_win, true)
end, { buffer = buf, desc = "Close the comment window" })
end
@@ -135,37 +172,47 @@ function M.open(spec, record)
end
if result == "deleted" then
vim.schedule(function()
if vim.api.nvim_win_is_valid(comment_win) then
vim.api.nvim_win_close(comment_win, true)
end
transition(panes.close)
end)
end
end,
})
return comment_win
end
-- Closes the open comment window, unless it has unsaved changes.
---@param spec ScratchFrame
---@param record ScratchCommentWindow
---@return integer? comment_win
function M.open(spec, record)
return transition(function()
if not dismiss() then
notify.warn("Save or discard the comment first")
return nil
end
return open_card(spec, record)
end)
end
---@param restore_focus? boolean return focus to the original window on close
---@return boolean closed
function M.close()
local open = M.current()
if not open then
return true
end
if vim.bo[vim.api.nvim_win_get_buf(open.comment_win)].modified then
return false
end
-- A command replacing the card (e.g. :CommentExport!) owns the next focus.
-- Only a user closing the card should return to its source or list.
open.on_close = nil
pcall(vim.api.nvim_win_close, open.comment_win, true)
return true
function M.close(restore_focus)
return transition(function()
return dismiss(restore_focus)
end) == true
end
---@return ScratchCommentWindow? window the open comment window, if any
---@return ScratchCommentWindow? window the owned card, even if its editor displays another buffer
function M.current()
if current and vim.api.nvim_win_is_valid(current.comment_win) then
return current
if current and vim.api.nvim_win_is_valid(current.panes.comment_win) then
return current.record
end
end
---@param bufnr integer
---@return boolean
function M.is_card_buffer(bufnr)
local panes = current and current.panes
return panes ~= nil and (bufnr == panes.comment_buf or bufnr == panes.context_buf)
end
return M
+106
View File
@@ -2,6 +2,7 @@ local helpers = require("helpers")
local scratch = require("scratch_comments")
local views = require("scratch_comments.model.views")
local store = require("scratch_comments.model.store")
local window = require("scratch_comments.ui.window")
local expect = MiniTest.expect
before_each(function()
@@ -127,6 +128,111 @@ describe("the comment window", function()
end)
end)
describe("opening a second card", function()
it("replaces a clean card when opened from another split", function()
local source_win = vim.api.nvim_get_current_win()
vim.cmd.vsplit()
local other_win = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(source_win)
vim.cmd("1Comment")
local first_card = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(other_win)
vim.cmd("2Comment")
expect.equality(vim.api.nvim_win_is_valid(first_card), false)
expect.equality(#helpers.floats(), 2)
helpers.write("on two")
expect.equality(views.anchored()[1].snippet, "two")
end)
it("keeps only the requested card when a close autocmd tries to open another", function()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("1Comment")
local first_card = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(source_win)
vim.api.nvim_create_autocmd("WinClosed", {
pattern = tostring(first_card),
once = true,
callback = function()
vim.cmd("3Comment")
end,
})
vim.cmd("2Comment")
expect.equality(#helpers.every_float(), 3) -- two panes and one backdrop
helpers.write("on two")
expect.equality(views.anchored()[1].snippet, "two")
end)
it("replaces the whole frame even when close autocmds do not run", function()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("1Comment")
vim.api.nvim_set_current_win(source_win)
vim.api.nvim_create_autocmd("User", {
pattern = "ScratchCommentsTestReplaceCard",
once = true,
callback = function()
vim.cmd("2Comment")
end,
})
vim.api.nvim_exec_autocmds("User", { pattern = "ScratchCommentsTestReplaceCard" })
expect.equality(#helpers.every_float(), 3)
helpers.write("on two")
expect.equality(views.anchored()[1].snippet, "two")
end)
it("closes the old frame after its editor displays a different buffer", function()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("1Comment")
local first_card = vim.api.nvim_get_current_win()
vim.cmd.enew()
vim.api.nvim_set_current_win(source_win)
vim.cmd("2Comment")
expect.equality(vim.api.nvim_win_is_valid(first_card), false)
expect.equality(#helpers.every_float(), 3)
end)
it("keeps a dirty card when opening a comment from another tab", function()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("tab split")
local other_win = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(source_win)
vim.cmd("1Comment")
local first_card = vim.api.nvim_get_current_win()
local draft = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(draft, 0, -1, false, { "keep this draft" })
vim.api.nvim_set_current_win(other_win)
scratch.add(2)
expect.equality(vim.api.nvim_get_current_win(), other_win)
expect.equality(helpers.notified(), { "Save or discard the comment first" })
expect.equality(assert(window.current()).comment_win, first_card)
expect.equality(vim.bo[draft].modified, true)
expect.equality(#helpers.floats(), 2)
vim.api.nvim_set_current_win(first_card)
vim.cmd("wq")
expect.equality(views.anchored()[1].comment, "keep this draft")
vim.cmd.tabonly()
end)
it("rejects attempts to comment on its own panes", function()
vim.cmd.Comment()
local context_win, comment_win = helpers.panes()
vim.cmd.Comment()
vim.api.nvim_set_current_win(context_win)
vim.cmd.Comment()
expect.equality(assert(window.current()).comment_win, comment_win)
expect.equality(vim.api.nvim_get_current_win(), context_win)
expect.equality(helpers.notified(), {
"Open comments from a source buffer or the comment list",
"Open comments from a source buffer or the comment list",
})
end)
end)
describe("a rejected comment write", function()
local function expect_rejected_write(command, reason)
local win = vim.api.nvim_get_current_win()
+12
View File
@@ -952,6 +952,18 @@ describe("quickfix navigation", function()
expect.equality(vim.api.nvim_win_get_buf(config.win), source)
end)
it("dismisses a card on <Esc> after its editor buffer is replaced", function()
vim.cmd.CommentList()
local qf_win = vim.api.nvim_get_current_win()
vim.cmd.Comment()
vim.cmd.enew()
vim.api.nvim_set_current_win(qf_win)
vim.cmd([[execute "normal \<Esc>"]])
expect.equality(#helpers.every_float(), 0)
expect.equality(vim.api.nvim_get_current_win(), qf_win)
end)
it("has nothing to dismiss on <Esc> before opening a card", function()
vim.cmd.CommentList()
vim.cmd([[execute "normal \<Esc>"]])
+84 -2
View File
@@ -34,6 +34,33 @@ describe(":CommentDelete", function()
expect.equality(vim.api.nvim_win_is_valid(qf_win), false)
end)
it("keeps a replacement card opened while refreshing the list", function()
vim.cmd.CommentList("on one to three")
local qf_win = vim.api.nvim_get_current_win()
vim.cmd("wincmd p")
local source_win = vim.api.nvim_get_current_win()
local source = vim.api.nvim_get_current_buf()
vim.cmd("1,3Comment")
vim.api.nvim_create_autocmd("WinClosed", {
pattern = tostring(qf_win),
once = true,
callback = function()
vim.api.nvim_win_call(source_win, function()
vim.cmd("2Comment")
end)
end,
})
vim.cmd.CommentDelete()
expect.equality(#store.all(), 1)
local replacement = assert(require("scratch_comments.ui.window").current())
expect.equality(#helpers.floats(), 2)
vim.bo[source].buftype = "nowrite" -- A stray write must not touch the source file.
vim.api.nvim_set_current_win(assert(replacement.comment_win))
helpers.write("on two")
expect.equality(views.anchored()[2].snippet, "two")
end)
it("deletes the focused card by its ID", function()
vim.cmd.Comment()
vim.cmd.CommentDelete()
@@ -68,6 +95,34 @@ describe(":CommentDelete", function()
vim.cmd("q!")
end)
it("honors the draft when deleting a row whose card displays another buffer", function()
local qf_win = vim.api.nvim_get_current_win()
vim.cmd.Comment()
local card_win = vim.api.nvim_get_current_win()
local draft = vim.api.nvim_get_current_buf()
vim.api.nvim_buf_set_lines(draft, 0, -1, false, { "keep this draft" })
vim.bo[draft].bufhidden = "hide"
vim.cmd.enew()
vim.api.nvim_set_current_win(qf_win)
vim.cmd.CommentDelete()
expect.equality(#store.all(), 2)
expect.equality(vim.bo[draft].modified, true)
vim.api.nvim_set_current_win(card_win)
vim.api.nvim_win_set_buf(card_win, draft)
vim.cmd.write()
expect.equality(views.anchored()[1].comment, "keep this draft")
vim.bo[draft].bufhidden = "wipe"
vim.cmd.enew()
vim.api.nvim_set_current_win(qf_win)
vim.cmd.CommentDelete()
expect.equality(#store.all(), 1)
expect.equality(#helpers.every_float(), 0)
expect.equality(vim.api.nvim_get_current_win(), qf_win)
end)
it("keeps quickfix focus when invoked there while an editor is open", function()
local qf_win = vim.api.nvim_get_current_win()
vim.cmd.Comment()
@@ -152,9 +207,36 @@ describe("saving an empty comment", function()
end)
describe(":CommentClear", function()
it("removes every comment and its marks", function()
it("does not remove the comment under a dirty card", function()
vim.cmd.Comment()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
vim.cmd.CommentClear()
expect.equality(#store.all(), 2)
expect.equality(vim.bo.modified, true)
expect.equality(helpers.notified()[#helpers.notified()], "Save or discard the comment first")
vim.cmd("wq")
expect.equality(views.anchored()[1].comment, "unsaved")
end)
it("clears comments and marks even if a close hook tries to reopen a card", function()
local source = assert(views.anchored()[1].bufnr)
vim.cmd("CommentClear")
vim.cmd.Comment()
local card_win = vim.api.nvim_get_current_win()
local source_win = assert(vim.api.nvim_win_get_config(card_win).win)
vim.api.nvim_create_autocmd("WinClosed", {
pattern = tostring(card_win),
once = true,
callback = function()
vim.api.nvim_win_call(source_win, function()
vim.cmd("1,3Comment")
end)
end,
})
vim.cmd.CommentClear()
vim.wait(0)
expect.equality(#helpers.every_float(), 0)
expect.equality(vim.api.nvim_get_current_win(), source_win)
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)