Preserve comment drafts when a save fails #7
No files matched your search
@@ -0,0 +1,14 @@
|
||||
# Changelog
|
||||
|
||||
## Unreleased
|
||||
|
||||
### Fixed
|
||||
|
||||
- Keep unsaved comment drafts open when saving fails because the source buffer
|
||||
was unloaded or wiped, or because the comment being edited was deleted.
|
||||
|
||||
## [0.1.0] - 2026-09-30
|
||||
|
||||
- First tagged release.
|
||||
|
||||
[0.1.0]: https://git.eleith.com/eleith/scratch-comments.nvim/src/tag/v0.1.0
|
||||
@@ -42,14 +42,14 @@ end
|
||||
|
||||
---@param id string
|
||||
---@param text string
|
||||
---@return boolean|string? "deleted" when saving removes the comment
|
||||
---@return ScratchSaveResult? result
|
||||
---@return string? reason
|
||||
local function save_existing(id, text)
|
||||
local comment = vim.iter(store.all()):find(function(candidate)
|
||||
return candidate.id == id
|
||||
end)
|
||||
if not comment then
|
||||
notify.warn("This comment is gone; nothing was saved")
|
||||
return false
|
||||
return nil, "This comment is gone; nothing was saved"
|
||||
end
|
||||
if text == "" then
|
||||
comments.delete(comment)
|
||||
@@ -58,6 +58,7 @@ local function save_existing(id, text)
|
||||
end
|
||||
comments.edit(id, text)
|
||||
notify.info("Updated comment")
|
||||
return "saved"
|
||||
end
|
||||
|
||||
---@param view ScratchComment|ScratchCommentView
|
||||
@@ -115,8 +116,11 @@ local function edit_new(bufnr, file_path, where)
|
||||
if added then
|
||||
return save_existing(added.id, text)
|
||||
end
|
||||
if text == "" or not vim.api.nvim_buf_is_valid(bufnr) then
|
||||
return
|
||||
if text == "" then
|
||||
return "unchanged"
|
||||
end
|
||||
if not vim.api.nvim_buf_is_loaded(bufnr) then
|
||||
return nil, "The comment source is no longer loaded"
|
||||
end
|
||||
added = comments.add({
|
||||
bufnr = bufnr,
|
||||
@@ -127,6 +131,7 @@ local function edit_new(bufnr, file_path, where)
|
||||
}, where)
|
||||
record.id = added.id
|
||||
notify.info("Added comment")
|
||||
return "saved"
|
||||
end,
|
||||
}, record)
|
||||
end
|
||||
|
||||
@@ -16,13 +16,15 @@ local function frame_buffer(lines, filetype)
|
||||
return bufnr
|
||||
end
|
||||
|
||||
---@alias ScratchSaveResult "saved"|"unchanged"|"deleted"
|
||||
|
||||
---@class ScratchFrame
|
||||
---@field title string
|
||||
---@field context string[]
|
||||
---@field filetype string
|
||||
---@field comment string
|
||||
---@field comment_name? string name shown for the editable comment buffer
|
||||
---@field on_save? fun(text: string): boolean|string? false keeps the text unsaved; "deleted" closes the editor
|
||||
---@field on_save? fun(text: string): ScratchSaveResult?, string? nil result rejects the write
|
||||
---@field comment_title? string
|
||||
---@field on_close? fun()
|
||||
|
||||
|
||||
@@ -69,6 +69,7 @@ end
|
||||
---@param spec ScratchFrame
|
||||
---@param record ScratchCommentWindow
|
||||
function M.open(spec, record)
|
||||
local save = assert(spec.on_save)
|
||||
spec.on_close = function()
|
||||
if current and current.comment_win == record.comment_win then
|
||||
current = nil
|
||||
@@ -124,12 +125,12 @@ function M.open(spec, record)
|
||||
callback = function()
|
||||
local lines = vim.api.nvim_buf_get_lines(comment_buf, 0, -1, false)
|
||||
local text = vim.trim(table.concat(lines, "\n"))
|
||||
local result = spec.on_save and spec.on_save(text)
|
||||
if result == false then
|
||||
return
|
||||
local result, reason = save(text)
|
||||
if not result then
|
||||
error(reason or "Could not save the comment", 0)
|
||||
end
|
||||
vim.bo[comment_buf].modified = false
|
||||
if record.on_saved then
|
||||
if result ~= "unchanged" and record.on_saved then
|
||||
record.on_saved()
|
||||
end
|
||||
if result == "deleted" then
|
||||
|
||||
@@ -127,6 +127,55 @@ describe("the comment window", function()
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("a rejected comment write", function()
|
||||
local function expect_rejected_write(command, reason)
|
||||
local win = vim.api.nvim_get_current_win()
|
||||
local buf = vim.api.nvim_get_current_buf()
|
||||
local text = helpers.text()
|
||||
local ok, err = pcall(function()
|
||||
vim.cmd(command)
|
||||
end)
|
||||
expect.equality(ok, false)
|
||||
expect.equality(tostring(err):find(reason, 1, true) ~= nil, true)
|
||||
expect.equality(vim.api.nvim_win_is_valid(win), true)
|
||||
expect.equality(vim.bo[buf].modified, true)
|
||||
expect.equality(helpers.text(buf), text)
|
||||
end
|
||||
|
||||
for _, unload in ipairs({ true, false }) do
|
||||
it("keeps a draft after its source is " .. (unload and "unloaded" or "wiped"), function()
|
||||
local source = vim.api.nvim_get_current_buf()
|
||||
vim.cmd("new")
|
||||
local other_win = vim.api.nvim_get_current_win()
|
||||
vim.cmd.wincmd("p")
|
||||
vim.cmd("2Comment")
|
||||
local card_win = vim.api.nvim_get_current_win()
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "keep this", "draft" })
|
||||
vim.api.nvim_set_current_win(other_win)
|
||||
vim.api.nvim_buf_delete(source, { unload = unload, force = true })
|
||||
vim.api.nvim_set_current_win(card_win)
|
||||
|
||||
for _, command in ipairs({ "w", "wq", "wq!" }) do
|
||||
expect_rejected_write(command, "The comment source is no longer loaded")
|
||||
end
|
||||
expect.equality(#store.all(), 0)
|
||||
vim.cmd("q!")
|
||||
expect.equality(vim.api.nvim_win_is_valid(card_win), false)
|
||||
end)
|
||||
end
|
||||
|
||||
it("keeps an edit when the saved comment is gone", function()
|
||||
vim.cmd.Comment()
|
||||
helpers.write("original")
|
||||
vim.cmd.Comment()
|
||||
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "keep this edit" })
|
||||
require("scratch_comments.comments").delete(store.all()[1])
|
||||
|
||||
expect_rejected_write("wq", "This comment is gone; nothing was saved")
|
||||
expect.equality(#store.all(), 0)
|
||||
end)
|
||||
end)
|
||||
|
||||
describe("scratch.add()", function()
|
||||
it("defaults to the cursor line, or takes a range", function()
|
||||
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
||||
|
||||
Reference in new issue
Block a user