Find the comment before reading its snippet #20
No files matched your search
@@ -26,6 +26,7 @@
|
||||
- Simplify delayed quickfix jumps by using the existing source reattachment callback.
|
||||
- `:Comment` ranges use whole lines. For exact Visual selections, use
|
||||
`<Cmd>Comment<CR>` instead of `:Comment<CR>` in your Visual mapping.
|
||||
- Find the comment to open before reading its source text.
|
||||
|
||||
## [0.1.0] - 2026-09-30
|
||||
|
||||
|
||||
@@ -10,21 +10,6 @@ local window = require("scratch_comments.ui.window")
|
||||
|
||||
local M = {}
|
||||
|
||||
---@param bufnr integer
|
||||
---@param start_line integer
|
||||
---@param end_line integer
|
||||
---@param start_col? integer
|
||||
---@param end_col? integer
|
||||
---@return ScratchCommentView?
|
||||
local function find_anchor(bufnr, start_line, end_line, start_col, end_col)
|
||||
return vim.iter(views.in_buffer(bufnr)):find(function(view)
|
||||
return view.start_line == start_line
|
||||
and view.end_line == end_line
|
||||
and view.start_col == start_col
|
||||
and view.end_col == end_col
|
||||
end)
|
||||
end
|
||||
|
||||
---@param first integer
|
||||
---@param last integer
|
||||
---@return ScratchLocation
|
||||
@@ -174,8 +159,7 @@ local function comment_at(where)
|
||||
return
|
||||
end
|
||||
|
||||
local existing =
|
||||
find_anchor(bufnr, where.start_line, where.end_line, where.start_col, where.end_col)
|
||||
local existing = views.find_at(bufnr, where)
|
||||
if existing then
|
||||
M.show(existing, vim.api.nvim_get_current_win(), nil, function()
|
||||
require("scratch_comments.list").refresh_comment(existing.id)
|
||||
|
||||
@@ -284,15 +284,8 @@ function M.edit_selected()
|
||||
if not index then
|
||||
return false
|
||||
end
|
||||
if not id or not by_id(id) then
|
||||
notify.warn("This comment is gone")
|
||||
return true
|
||||
end
|
||||
local entry = vim.iter(views.anchored()):find(function(view)
|
||||
return view.id == id
|
||||
end) or vim.iter(views.orphans()):find(function(view)
|
||||
return view.id == id
|
||||
end)
|
||||
local comment = id and by_id(id)
|
||||
local entry = comment and views.for_card(comment)
|
||||
if not entry then
|
||||
notify.warn("This comment is gone")
|
||||
return true
|
||||
@@ -313,7 +306,7 @@ function M.edit_selected()
|
||||
end
|
||||
end)
|
||||
end, function()
|
||||
refresh(id, index, expected_id)
|
||||
refresh(entry.id, index, expected_id)
|
||||
end)
|
||||
if comment_win then
|
||||
parent_win = vim.api.nvim_win_get_config(comment_win).win
|
||||
|
||||
@@ -42,55 +42,113 @@ function M.text(bufnr, start_line, end_line, start_col, end_col)
|
||||
return vim.api.nvim_buf_get_lines(bufnr, start_line - 1, end_line, false)
|
||||
end
|
||||
|
||||
-- Resolve coordinates without reading source text.
|
||||
---@param comment ScratchComment
|
||||
---@return ScratchCommentView?
|
||||
local function locate(comment)
|
||||
local first, last, start_col, end_col
|
||||
if comment.state == "parked" then
|
||||
local where = comment.last_location
|
||||
first, last = where.start_line, where.end_line
|
||||
start_col, end_col = where.start_col, where.end_col
|
||||
elseif comment.state ~= "inactive" then
|
||||
first, last, start_col, end_col = anchors.range(comment)
|
||||
end
|
||||
if not first or not last then
|
||||
return nil
|
||||
end
|
||||
local view = vim.deepcopy(comment) --[[@as ScratchCommentView]]
|
||||
view.start_line, view.end_line = first, last
|
||||
view.start_col, view.end_col = start_col, end_col
|
||||
return view
|
||||
end
|
||||
|
||||
---@param view ScratchCommentView
|
||||
---@return ScratchCommentView
|
||||
local function with_snippet(view)
|
||||
if view.state ~= "parked" then
|
||||
view.snippet = table.concat(
|
||||
M.text(view.bufnr, view.start_line, view.end_line, view.start_col, view.end_col),
|
||||
"\n"
|
||||
)
|
||||
end
|
||||
return view
|
||||
end
|
||||
|
||||
---@param comments ScratchComment[]
|
||||
---@return ScratchCommentView[]
|
||||
local function sorted_positions(comments)
|
||||
local found = vim.iter(comments):map(locate):totable()
|
||||
table.sort(found, M.by_position)
|
||||
return found
|
||||
end
|
||||
|
||||
---@param comment ScratchComment
|
||||
local function orphaned(comment)
|
||||
return comment.state == "inactive" or (comment.state == nil and anchors.is_orphaned(comment))
|
||||
end
|
||||
|
||||
---@return ScratchCommentView[]
|
||||
function M.anchored()
|
||||
local views = {}
|
||||
for _, comment in ipairs(store.all()) do
|
||||
local start_line, end_line, start_col, end_col
|
||||
if comment.state == "parked" then
|
||||
local where = comment.last_location
|
||||
start_line, end_line = where.start_line, where.end_line
|
||||
start_col, end_col = where.start_col, where.end_col
|
||||
elseif comment.state ~= "inactive" then
|
||||
start_line, end_line, start_col, end_col = anchors.range(comment)
|
||||
end
|
||||
if start_line and end_line then
|
||||
local view = vim.deepcopy(comment) --[[@as ScratchCommentView]]
|
||||
view.start_line, view.end_line = start_line, end_line
|
||||
view.start_col, view.end_col = start_col, end_col
|
||||
view.snippet = comment.state == "parked" and comment.snippet
|
||||
or table.concat(M.text(comment.bufnr, start_line, end_line, start_col, end_col), "\n")
|
||||
table.insert(views, view)
|
||||
end
|
||||
end
|
||||
table.sort(views, M.by_position)
|
||||
return views
|
||||
return vim.tbl_map(with_snippet, sorted_positions(store.all()))
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@return ScratchCommentView[]
|
||||
function M.in_buffer(bufnr)
|
||||
return vim.tbl_filter(function(view)
|
||||
return view.bufnr == bufnr
|
||||
end, M.anchored())
|
||||
return vim.tbl_map(with_snippet, sorted_positions(store.in_buffer(bufnr)))
|
||||
end
|
||||
|
||||
---@param list ScratchCommentView[]
|
||||
---@param id? string
|
||||
---@return integer?
|
||||
function M.index_of(list, id)
|
||||
for i, view in ipairs(list) do
|
||||
if view.id == id then
|
||||
return i
|
||||
---@param a ScratchLocation
|
||||
---@param b ScratchLocation
|
||||
local function same_location(a, b)
|
||||
return a.start_line == b.start_line
|
||||
and a.end_line == b.end_line
|
||||
and a.start_col == b.start_col
|
||||
and a.end_col == b.end_col
|
||||
end
|
||||
|
||||
---@param bufnr integer
|
||||
---@param where ScratchLocation
|
||||
---@return ScratchCommentView?
|
||||
function M.find_at(bufnr, where)
|
||||
for _, view in ipairs(sorted_positions(store.in_buffer(bufnr))) do
|
||||
if same_location(view, where) then
|
||||
return with_snippet(view)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
---@param comment ScratchComment
|
||||
---@return ScratchCommentView|ScratchComment|nil
|
||||
function M.for_card(comment)
|
||||
local view = locate(comment)
|
||||
if view then
|
||||
return with_snippet(view)
|
||||
end
|
||||
if orphaned(comment) then
|
||||
return comment
|
||||
end
|
||||
end
|
||||
|
||||
---@param view ScratchCommentView
|
||||
---@return integer index
|
||||
---@return integer total
|
||||
function M.number_in_source(view)
|
||||
local in_source = sorted_positions(vim.tbl_filter(function(comment)
|
||||
return comment.source_name == view.source_name
|
||||
end, store.all()))
|
||||
for index, candidate in ipairs(in_source) do
|
||||
if candidate.id == view.id then
|
||||
return index, #in_source
|
||||
end
|
||||
end
|
||||
return 0, #in_source
|
||||
end
|
||||
|
||||
---@return ScratchComment[]
|
||||
function M.orphans()
|
||||
return vim.tbl_filter(function(comment)
|
||||
return comment.state == "inactive" or (comment.state == nil and anchors.is_orphaned(comment))
|
||||
end, store.all())
|
||||
return vim.tbl_filter(orphaned, store.all())
|
||||
end
|
||||
|
||||
return M
|
||||
@@ -6,16 +6,14 @@ local M = {}
|
||||
---@param view ScratchCommentView
|
||||
---@return ScratchFrame
|
||||
function M.of(view)
|
||||
local in_file = vim.tbl_filter(function(candidate)
|
||||
return candidate.source_name == view.source_name
|
||||
end, views.anchored())
|
||||
local index, total = views.number_in_source(view)
|
||||
return {
|
||||
title = location.title(view.file_path, view),
|
||||
context = vim.split(view.snippet, "\n"),
|
||||
filetype = view.bufnr and vim.api.nvim_buf_is_valid(view.bufnr) and vim.bo[view.bufnr].filetype
|
||||
or "",
|
||||
comment = view.comment,
|
||||
comment_title = ("comment (%d of %d)"):format(views.index_of(in_file, view.id) or 0, #in_file),
|
||||
comment_title = ("comment (%d of %d)"):format(index, total),
|
||||
}
|
||||
end
|
||||
|
||||
|
||||
@@ -45,6 +45,37 @@ describe(":Comment", function()
|
||||
expect.equality(views.anchored()[1].comment, "edited comment")
|
||||
end)
|
||||
|
||||
it("reads only the live snippet for the card being reopened", function()
|
||||
local source = vim.api.nvim_get_current_buf()
|
||||
vim.cmd("1Comment")
|
||||
helpers.write("first comment")
|
||||
vim.cmd("2Comment")
|
||||
helpers.write("second comment")
|
||||
helpers.buffer("other.md", { "another source" })
|
||||
vim.cmd.Comment()
|
||||
helpers.write("other comment")
|
||||
vim.cmd.buffer(source)
|
||||
vim.api.nvim_buf_set_text(source, 0, 0, 0, 3, { "updated" })
|
||||
|
||||
local read_text = views.text
|
||||
local reads = {}
|
||||
---@diagnostic disable-next-line: duplicate-set-field -- record snippet reads during open
|
||||
views.text = function(bufnr, first, last, ...)
|
||||
table.insert(reads, { bufnr, first, last })
|
||||
return read_text(bufnr, first, last, ...)
|
||||
end
|
||||
local ok, err = pcall(function()
|
||||
vim.cmd("1Comment")
|
||||
end)
|
||||
views.text = read_text
|
||||
assert(ok, err)
|
||||
|
||||
local context_win, comment_win = helpers.panes()
|
||||
expect.equality(reads, { { source, 1, 1 } })
|
||||
expect.equality(helpers.text(vim.api.nvim_win_get_buf(context_win)), "updated")
|
||||
expect.equality(helpers.title(comment_win), " comment (1 of 2) [NORMAL] ")
|
||||
end)
|
||||
|
||||
it("on a range adds a separate comment over those lines", function()
|
||||
vim.cmd("1Comment")
|
||||
helpers.write("on one")
|
||||
|
||||
Reference in new issue
Block a user