Keep comments with renamed source buffers #12

Merged
eleith merged 1 commits from fix/follow-source-renames into main 2026-11-30 19:37:35 +00:00
6 changed files with 112 additions and 10 deletions

No files matched your search

+2
View File
@@ -13,6 +13,8 @@
- Restore comments when reopening a multi-line URI buffer with a blank first line.
- Keep comments orphaned when a live URI edit invalidates their range, even if
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.
## [0.1.0] - 2026-09-30
+7 -6
View File
@@ -94,8 +94,6 @@ end
---@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?
local added
---@type ScratchCommentWindow
@@ -127,12 +125,15 @@ local function edit_new(bufnr, file_path, where)
if vim.api.nvim_buf_get_changedtick(bufnr) ~= source_tick then
return nil, "The comment source changed. Copy draft, :q!, reopen."
end
local name = vim.api.nvim_buf_get_name(bufnr)
if name == "" then
return nil, "The source buffer no longer has a name"
end
added = comments.add({
bufnr = bufnr,
comment = text,
file_path = file_path,
source_name = file_path,
relative_path = relative_path,
file_path = name,
relative_path = paths.display_path(name),
}, where)
record.id = added.id
notify.info("Added comment")
@@ -180,7 +181,7 @@ function M.comment(start_line, end_line, use_selection)
return
end
edit_new(bufnr, paths.is_uri(name) and name or vim.fn.fnamemodify(name, ":p"), where)
edit_new(bufnr, name, where)
end
---@param on? boolean
+23 -3
View File
@@ -105,8 +105,7 @@ local function snapshot(comment, bufnr)
return true
end
-- Snapshot while extmarks and text still exist. BufUnload, BufDelete and
-- BufWipeout can all fire for the same buffer; only the first does work.
-- Snapshot while extmarks and text still exist.
---@param bufnr integer
function M.park_buffer(bufnr)
for _, comment in ipairs(store.in_buffer(bufnr)) do
@@ -188,13 +187,34 @@ vim.api.nvim_create_autocmd({ "TextChanged", "TextChangedI" }, {
end,
})
vim.api.nvim_create_autocmd({ "BufUnload", "BufDelete", "BufWipeout" }, {
-- BufDelete and BufWipeout can also signal a rename, not a loss of text.
vim.api.nvim_create_autocmd("BufUnload", {
group = group,
callback = function(args)
M.park_buffer(args.buf)
end,
})
vim.api.nvim_create_autocmd("BufFilePost", {
group = group,
callback = function(args)
local attached = store.in_buffer(args.buf)
if #attached == 0 then
return
end
local name = vim.api.nvim_buf_get_name(args.buf)
if name == "" then
return -- Keep the live anchors and last known path until the buffer has a name.
end
local relative_path = paths.display_path(name)
for _, comment in ipairs(attached) do
comment.source_name = name
comment.file_path = name
comment.relative_path = relative_path
end
end,
})
-- A read has completed for normal files. BufReadCmd sources may instead fill
-- the buffer later; watch their text changes without depending on the provider.
vim.api.nvim_create_autocmd("BufReadPost", {
+11 -1
View File
@@ -9,7 +9,11 @@ function M.is_uri(name)
end
local function run(args)
local result = vim.system(args, { text = true }):wait()
local ok, process = pcall(vim.system, args, { text = true })
if not ok then
return nil
end
local result = process:wait()
if result.code == 0 then
return vim.trim(result.stdout or "")
end
@@ -70,4 +74,10 @@ function M.relative_path(root, path)
return path
end
---@param path string
---@return string
function M.display_path(path)
return M.is_uri(path) and path or M.relative_path(M.git_root(path), path)
end
return M
+61
View File
@@ -134,6 +134,67 @@ describe("an orphan", function()
end)
end)
describe("renaming a source buffer", function()
local saved_as
after_each(function()
if saved_as then
vim.fn.delete(saved_as)
saved_as = nil
end
end)
it("keeps the comment on the same buffer when its name is cleared and set again", function()
local source = helpers.buffer("before-rename.txt", { "first", "target" })
vim.cmd("2Comment")
helpers.write("on target")
local id = only().id
vim.cmd("0file")
expect.equality(helpers.sign_at(2), "│")
vim.cmd.file("tests/renamed-source.txt")
local exported = vim.json.decode(scratch.render("json")).comments[1]
expect.equality(exported.file, vim.api.nvim_buf_get_name(source))
expect.equality(exported.relative_path, "tests/renamed-source.txt")
expect.equality(only().id, id)
expect.equality(helpers.sign_at(2), "│")
vim.api.nvim_buf_set_lines(source, 0, 0, false, { "inserted" })
expect.equality(only().start_line, 3)
vim.cmd.CommentList()
vim.cmd.cc()
expect.equality(vim.api.nvim_get_current_buf(), source)
expect.equality(vim.api.nvim_win_get_cursor(0)[1], 3)
end)
it("restores a comment under the new name after :saveas and wipe", function()
saved_as = vim.fn.tempname() .. ".txt"
helpers.buffer("before-saveas.txt", { "first", "target" })
vim.cmd("2Comment")
helpers.write("on target")
vim.cmd.saveas({ args = { saved_as }, magic = { file = false, bar = false } })
vim.cmd.bwipeout()
vim.cmd.edit({ args = { saved_as }, magic = { file = false, bar = false } })
expect.equality(
vim.wait(1000, function()
return helpers.sign_at(2) == "│"
end),
true
)
expect.equality(only().comment, "on target")
end)
it("uses the current source name when a draft is first saved", function()
local source = helpers.buffer("before-draft-rename.txt", { "target" })
vim.cmd.Comment()
vim.api.nvim_buf_set_name(source, "after-draft-rename.txt")
helpers.write("on target")
local exported = vim.json.decode(scratch.render("json")).comments[1]
expect.equality(exported.file, vim.api.nvim_buf_get_name(source))
expect.equality(exported.relative_path, "after-draft-rename.txt")
end)
end)
describe("a file's comments", function()
before_each(function()
vim.cmd.edit("tests/fixtures/lifecycle.txt")
+8
View File
@@ -16,6 +16,14 @@ describe("git_root", function()
expect.equality(paths.git_root(root .. "/lua/scratch_comments/paths.lua"), root)
end)
it("finds the repository when Git is not on PATH", function()
local path = vim.env.PATH
vim.env.PATH = vim.fn.tempname()
local ok, root = pcall(paths.git_root, vim.fn.getcwd() .. "/README.md")
vim.env.PATH = path
expect.equality({ ok, root }, { true, vim.fn.getcwd() })
end)
it("uses the working directory for a file with no path", function()
expect.equality(paths.git_root(""), vim.fn.getcwd())
end)