Reject first saves when the comment source changes #9

Merged
eleith merged 1 commits from fix/reject-stale-comment-drafts into main 2026-11-30 18:32:35 +00:00
3 changed files with 35 additions and 0 deletions

No files matched your search

+2
View File
@@ -8,6 +8,8 @@
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.
- Keep a draft open and reject its first save if the source was edited,
reloaded, or saved with pending changes.
## [0.1.0] - 2026-09-30
+4
View File
@@ -93,6 +93,7 @@ end
---@param file_path string
---@param where ScratchLocation
local function edit_new(bufnr, file_path, where)
local source_tick = vim.api.nvim_buf_get_changedtick(bufnr)
local relative_path = paths.is_uri(file_path) and file_path
or paths.relative_path(paths.git_root(file_path), file_path)
---@type ScratchComment?
@@ -123,6 +124,9 @@ local function edit_new(bufnr, file_path, where)
if not vim.api.nvim_buf_is_loaded(bufnr) then
return nil, "The comment source is no longer loaded"
end
if vim.api.nvim_buf_get_changedtick(bufnr) ~= source_tick then
return nil, "The comment source changed. Copy draft, :q!, reopen."
end
added = comments.add({
bufnr = bufnr,
comment = text,
+29
View File
@@ -20,6 +20,20 @@ describe(":Comment", function()
expect.equality(views.anchored()[1].comment, "first comment")
end)
it("saves a draft after browsing the source without editing it", function()
vim.cmd.vsplit()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("2Comment")
local card_win = vim.api.nvim_get_current_win()
vim.api.nvim_set_current_win(source_win)
vim.api.nvim_win_set_cursor(source_win, { 4, 0 })
vim.api.nvim_set_current_win(card_win)
helpers.write("on two")
local saved = views.anchored()[1]
expect.equality({ saved.start_line, saved.snippet, saved.comment }, { 2, "two", "on two" })
end)
it("opens an existing comment on the exact range for editing", function()
vim.cmd("Comment")
helpers.write("first comment")
@@ -270,6 +284,21 @@ describe("a rejected comment write", function()
end)
end
it("keeps a draft instead of saving it to a stale source range", function()
local source = vim.api.nvim_get_current_buf()
local source_win = vim.api.nvim_get_current_win()
vim.cmd("2Comment")
local card_win = vim.api.nvim_get_current_win()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "on two" })
vim.api.nvim_set_current_win(source_win)
vim.api.nvim_buf_set_lines(source, 0, 0, false, { "inserted" })
vim.api.nvim_set_current_win(card_win)
expect_rejected_write("wq", "The comment source changed")
expect.equality(#store.all(), 0)
end)
it("keeps an edit when the saved comment is gone", function()
vim.cmd.Comment()
helpers.write("original")