From 8655cf992f6d4a57345fb50b88333c772dd328f1 Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 30 Nov 2026 10:37:30 -0800 Subject: [PATCH] Reattach comments in URI buffers with a blank first line --- CHANGELOG.md | 1 + lua/scratch_comments/comments.lua | 14 +++-------- tests/features/anchoring_spec.lua | 41 ++++++++++++++++++++++--------- 3 files changed, 34 insertions(+), 22 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 867836b..4ffa9c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,7 @@ 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. +- Restore comments when reopening a multi-line URI buffer with a blank first line. ## [0.1.0] - 2026-09-30 diff --git a/lua/scratch_comments/comments.lua b/lua/scratch_comments/comments.lua index accf22f..bdacf7f 100644 --- a/lua/scratch_comments/comments.lua +++ b/lua/scratch_comments/comments.lua @@ -261,16 +261,10 @@ vim.api.nvim_create_autocmd("BufEnter", { end, }) end - -- A previously loaded file can be reentered without a read event. For - -- URI buffers an initial empty pane may still be waiting for BufReadCmd. - local lines = vim.api.nvim_buf_get_lines(bufnr, 0, 1, false) - if not paths.is_uri(name) or lines[1] ~= "" then - vim.schedule(function() - if vim.api.nvim_buf_is_valid(bufnr) then - M.attach_buffer(bufnr) - end - end) - end + -- Reentering a loaded buffer may not fire a read event. + vim.schedule(function() + M.attach_buffer(bufnr) + end) end end, }) diff --git a/tests/features/anchoring_spec.lua b/tests/features/anchoring_spec.lua index dc67df6..f51fd6c 100644 --- a/tests/features/anchoring_spec.lua +++ b/tests/features/anchoring_spec.lua @@ -141,9 +141,17 @@ describe("a file's comments", function() helpers.write("on the first line") end) - it("survive :e!", function() + it("reattach after :e! and follow later edits", function() vim.cmd("edit!") - expect.equality(#views.anchored(), 1) + expect.equality( + vim.wait(1000, function() + return #views.in_buffer(vim.api.nvim_get_current_buf()) == 1 + end), + true + ) + expect.equality(helpers.sign_at(1), "│") + vim.api.nvim_buf_set_lines(0, 0, 0, false, { "inserted" }) + expect.equality(only().start_line, 2) end) it("survive :bd and reattach when reopened", function() @@ -170,11 +178,12 @@ describe("a file's comments", function() expect.equality(store.all()[1].state, "parked") end) - it("keeps a wiped diff-style buffer until the same URI returns", function() + it("restores comments when a wiped URI buffer reopens with a blank first line", function() local url = "codediff:////tmp/repo///HEAD/review.lua" + local lines = { "", "two" } vim.cmd.enew() vim.api.nvim_buf_set_name(0, url) - vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" }) + vim.api.nvim_buf_set_lines(0, 0, -1, false, lines) vim.bo.bufhidden = "wipe" vim.cmd("2Comment") helpers.write("virtual note") @@ -182,14 +191,22 @@ describe("a file's comments", function() vim.cmd("enew!") expect.equality(vim.api.nvim_buf_is_valid(old), false) expect.equality(store.all()[#store.all()].state, "parked") - vim.api.nvim_buf_set_name(0, url) - vim.api.nvim_buf_set_lines(0, 0, -1, false, { "one", "two" }) - vim.api.nvim_exec_autocmds("BufEnter", { buffer = 0 }) - vim.wait(0) - expect.equality(store.all()[#store.all()].state, nil) - expect.equality(helpers.sign_at(2), "│") - expect.equality(store.all()[#store.all()].comment, "virtual note") - expect.equality(#views.anchored(), 2) + + vim.api.nvim_create_autocmd("BufReadCmd", { + pattern = url, + once = true, + callback = function(args) + vim.api.nvim_buf_set_lines(args.buf, 0, -1, false, lines) + end, + }) + vim.cmd.edit(url) + expect.equality( + vim.wait(1000, function() + return helpers.sign_at(2) == "│" + end), + true + ) + expect.equality(views.in_buffer(vim.api.nvim_get_current_buf())[1].comment, "virtual note") end) it("makes a reattached URI comment an orphan when its line is deleted", function() -- 2.54.1