Reattach comments in URI buffers with a blank first line #10

Merged
eleith merged 1 commits from fix/reattach-blank-first-line into main 2026-11-30 18:50:10 +00:00
3 changed files with 34 additions and 22 deletions

No files matched your search

+1
View File
@@ -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
+4 -10
View File
@@ -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,
})
+29 -12
View File
@@ -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()