From 3c553de320710f1f46da6f3a637847ed9eeb80b8 Mon Sep 17 00:00:00 2001 From: eleith Date: Mon, 30 Nov 2026 15:40:58 -0800 Subject: [PATCH] Use the active Visual selection for comments --- CHANGELOG.md | 2 + README.md | 13 ++---- doc/scratch-comments.txt | 19 ++++----- lua/scratch_comments/actions.lua | 70 ++++++++++++++++++++------------ plugin/scratch-comments.lua | 8 +++- tests/features/spans_spec.lua | 60 ++++++++++++++++++--------- 6 files changed, 106 insertions(+), 66 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2a73707..e4748b4 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -24,6 +24,8 @@ - Simplify quickfix setup and remove unused test helpers and redundant tests. - Share card layout code between resizing and moving to another split. - Simplify delayed quickfix jumps by using the existing source reattachment callback. +- `:Comment` ranges use whole lines. For exact Visual selections, use + `Comment` instead of `:Comment` in your Visual mapping. ## [0.1.0] - 2026-09-30 diff --git a/README.md b/README.md index d51b279..e701cd9 100644 --- a/README.md +++ b/README.md @@ -46,13 +46,8 @@ comment on the current line: :Comment ``` -comment on a range, or select text and type `:Comment`. select whole lines to -comment on them, or just a word or phrase to comment on only that: - -```vim -:12,16Comment -:'<,'>Comment -``` +use `:12,16Comment` for a range of whole lines. to comment on a word or +phrase, select it and use the [visual mapping](#mappings) below. a window opens over the file, with the lines you're commenting on at the top and your comment below. a new comment opens in insert mode, and the @@ -83,7 +78,7 @@ it, pipe it to a command, or edit it: | Command | Does | | --- | --- | -| `:Comment` | Add or edit a comment in a file; in the built-in comment list, open the cursor row's card | +| `:[range]Comment` | Comment on whole lines or the active selection; in the built-in comment list, open the cursor row's card | | `:CommentList [filter]` | Put comments in the quickfix list and open it, fuzzy matching `filter` | | `:CommentDelete` | Delete the cursor row in the built-in quickfix list, or the open card elsewhere | | `:CommentExport[!] [format]` | Copy all comments to the clipboard as `markdown` (default) or `json`. `!` opens them in a scratch buffer | @@ -96,7 +91,7 @@ add your own, for example: ```lua vim.keymap.set("n", "ca", "Comment", { desc = "Comment on line" }) -vim.keymap.set("x", "ca", ":Comment", { desc = "Comment on selection" }) +vim.keymap.set("x", "ca", "Comment", { desc = "Comment on selection" }) vim.keymap.set("n", "cl", "CommentList", { desc = "List comments" }) vim.keymap.set("n", "cx", "CommentExport", { desc = "Copy comments" }) ``` diff --git a/doc/scratch-comments.txt b/doc/scratch-comments.txt index d47afaf..ae767e4 100644 --- a/doc/scratch-comments.txt +++ b/doc/scratch-comments.txt @@ -29,15 +29,14 @@ Requires Neovim 0.12 or newer. 2. COMMANDS *scratch-comments-commands* *:Comment* -:[range]Comment Comment on the current line, or on [range]. To comment - on a selection, select it and type ":Comment", which - gives ":'<,'>Comment". A characterwise selection - (|v|) comments on just the selected text. A linewise - one (|V|) comments on whole lines. If that exact text - already has a comment, open it for editing instead of - adding a duplicate. In the built-in quickfix list - opened by |:CommentList|, open the comment under the - cursor for editing without jumping to its source. +:[range]Comment Comment on the current line or a range of whole lines. + A Visual mapping using Comment comments on + the active selection. Characterwise (|v|) selections + use just the selected text; linewise (|V|) and + blockwise (|CTRL-V|) selections use whole lines. + See |scratch-comments-api| for a mapping example. + In the built-in quickfix list opened by |:CommentList|, + edit the comment under the cursor without jumping. A window opens over the file: the lines you are commenting on at the top, your comment below. @@ -172,7 +171,7 @@ needs 'termguicolors'. For a different dim, or none: >lua < Example mappings: >lua vim.keymap.set("n", "ca", "Comment") - vim.keymap.set("x", "ca", ":Comment") + vim.keymap.set("x", "ca", "Comment") vim.keymap.set("n", "cl", "CommentList") vim.keymap.set("n", "cx", "CommentExport") < diff --git a/lua/scratch_comments/actions.lua b/lua/scratch_comments/actions.lua index 8fbeef5..9ca04c9 100644 --- a/lua/scratch_comments/actions.lua +++ b/lua/scratch_comments/actions.lua @@ -25,19 +25,32 @@ local function find_anchor(bufnr, start_line, end_line, start_col, end_col) end) end ----@param start_line integer ----@param end_line integer ----@return integer? start_col ----@return integer? end_col -local function selected_columns(start_line, end_line) - local from, to = vim.fn.getpos("'<"), vim.fn.getpos("'>") - if vim.fn.visualmode() ~= "v" or from[2] ~= start_line or to[2] ~= end_line then - return nil, nil +---@param first integer +---@param last integer +---@return ScratchLocation +local function line_range(first, last) + return { start_line = math.min(first, last), end_line = math.max(first, last) } +end + +---@param mode string active Visual mode +---@return ScratchLocation +local function visual_range(mode) + local from, to = vim.fn.getpos("v"), vim.fn.getpos(".") + if mode ~= "v" then + return line_range(from[2], to[2]) -- Linewise and blockwise selections use whole lines. end - local last_line = vim.fn.getline(end_line) - local last = math.min(to[3], #last_line) - local end_col = last == 0 and 0 or last + vim.str_utf_end(last_line, last) - return from[3] - 1, end_col + + -- Respect 'selection' and distinguish end-of-line from column zero. + local region = vim.fn.getregionpos(from, to, { eol = true }) + local first, last = region[1][1], region[#region][2] + local last_line = vim.fn.getline(last[2]) + local col = math.min(last[3], #last_line) + return { + start_line = first[2], + end_line = last[2], + start_col = math.max(0, first[3] - 1), + end_col = col == 0 and 0 or col + vim.str_utf_end(last_line, col), + } end ---@param id string @@ -142,10 +155,8 @@ local function edit_new(bufnr, file_path, where) }, record) end ----@param start_line integer ----@param end_line integer ----@param use_selection? boolean -function M.comment(start_line, end_line, use_selection) +---@param where ScratchLocation +local function comment_at(where) local bufnr = vim.api.nvim_get_current_buf() if window.is_card_buffer(bufnr) then notify.warn("Open comments from a source buffer or the comment list") @@ -163,15 +174,6 @@ function M.comment(start_line, end_line, use_selection) return end - ---@type ScratchLocation - local where = { - start_line = math.min(start_line, end_line), - end_line = math.max(start_line, end_line), - } - if use_selection then - where.start_col, where.end_col = selected_columns(where.start_line, where.end_line) - end - local existing = find_anchor(bufnr, where.start_line, where.end_line, where.start_col, where.end_col) if existing then @@ -184,6 +186,24 @@ function M.comment(start_line, end_line, use_selection) edit_new(bufnr, name, where) end +---@param start_line integer +---@param end_line integer +function M.comment(start_line, end_line) + comment_at(line_range(start_line, end_line)) +end + +---@return boolean handled whether there was an active Visual selection +function M.comment_selection() + local mode = vim.fn.mode() + if mode ~= "v" and mode ~= "V" and mode ~= "\22" then + return false + end + local where = visual_range(mode) + vim.cmd("normal! \27") + comment_at(where) + return true +end + ---@param on? boolean function M.toggle(on) if on == nil then diff --git a/plugin/scratch-comments.lua b/plugin/scratch-comments.lua index 8fc41b6..04766cb 100644 --- a/plugin/scratch-comments.lua +++ b/plugin/scratch-comments.lua @@ -10,8 +10,12 @@ end local command = vim.api.nvim_create_user_command command("Comment", function(ctx) - require("scratch_comments.actions").comment(ctx.line1, ctx.line2, ctx.range == 2) -end, { range = true, desc = "Add or edit a source comment, or edit the selected quickfix comment" }) + local actions = require("scratch_comments.actions") + if ctx.range == 0 and actions.comment_selection() then + return + end + actions.comment(ctx.line1, ctx.line2) +end, { range = true, desc = "Add or edit a Scratch Comment" }) command("CommentDelete", function() scratch().delete() diff --git a/tests/features/spans_spec.lua b/tests/features/spans_spec.lua index 5ac73e4..793cd2d 100644 --- a/tests/features/spans_spec.lua +++ b/tests/features/spans_spec.lua @@ -4,8 +4,8 @@ local views = require("scratch_comments.model.views") local expect = MiniTest.expect ---@param keys string a visual selection, such as "viw" -local function select(keys) - vim.cmd("normal! " .. keys .. "\27") +local function comment_selection(keys) + vim.api.nvim_feedkeys(vim.keycode(keys .. ""), "xt", false) end -- Only the text decorations, not the signs or tracking anchors. @@ -28,13 +28,13 @@ end before_each(function() helpers.reset() helpers.buffer("spans.md", { "the quick brown fox", "café → bar", "one two", "three four" }) + vim.keymap.set("x", "", "Comment", { buffer = true }) end) describe("a charwise selection", function() before_each(function() vim.api.nvim_win_set_cursor(0, { 1, 4 }) - select("viw") - vim.cmd("'<,'>Comment") + comment_selection("viw") end) it("names its columns in the title", function() @@ -57,17 +57,18 @@ describe("a charwise selection", function() it("opens an existing comment on the exact selection for editing", function() helpers.write("on quick") - select("viw") - vim.cmd("'<,'>Comment") + comment_selection("viw") expect.equality(helpers.text(), "on quick") helpers.write("edited quick") expect.equality(#views.anchored(), 1) expect.equality(views.anchored()[1].comment, "edited quick") end) - it("is separate from a comment on the whole line", function() + it("is separate from a numeric range on the same line", function() helpers.write("on quick") - vim.cmd("Comment") + expect.equality({ vim.fn.line("'<"), vim.fn.line("'>") }, { 1, 1 }) + vim.cmd("1,1Comment") + expect.equality(helpers.text(), "") helpers.write("on the line") vim.cmd("1Comment") expect.equality(helpers.text(), "on the line") @@ -81,8 +82,7 @@ describe("a span", function() before_each(function() vim.api.nvim_win_set_cursor(0, { 1, 4 }) - select("viw") - vim.cmd("'<,'>Comment") + comment_selection("viw") helpers.write("on quick") quick = views.anchored()[1] end) @@ -154,27 +154,49 @@ describe("a span", function() end) describe("a selection", function() + local selection + + before_each(function() + selection = vim.o.selection + end) + + after_each(function() + vim.o.selection = selection + end) + it("keeps a multibyte character whole", function() vim.api.nvim_win_set_cursor(0, { 2, 0 }) - select("v3l") - vim.cmd("'<,'>Comment") + comment_selection("v3l") helpers.write("on café") expect.equality(views.anchored()[1].snippet, "café") expect.equality(highlights(), { { 1, 0, 1, 5, "ScratchCommentHighlight" } }) end) + it("keeps the exact text in a reversed exclusive selection", function() + vim.o.selection = "exclusive" + vim.api.nvim_win_set_cursor(0, { 2, 5 }) + comment_selection("v4h") + helpers.write("on café") + expect.equality(views.anchored()[1].snippet, "café") + end) + it("to $ ends at the end of the line", function() vim.api.nvim_win_set_cursor(0, { 3, 4 }) - select("v$") - vim.cmd("'<,'>Comment") + comment_selection("v$") helpers.write("to the end") expect.equality(views.anchored()[1].snippet, "two") end) + it("keeps a selection that starts just after the last character", function() + vim.api.nvim_win_set_cursor(0, { 3, 4 }) + comment_selection("v$oj0") + helpers.write("on the newline and t") + expect.equality(views.anchored()[1].snippet, "\nt") + end) + it("can cross lines, titled with line:col", function() vim.api.nvim_win_set_cursor(0, { 3, 4 }) - select("vj") - vim.cmd("'<,'>Comment") + comment_selection("vj") expect.equality(lines_title(), " spans.md [lines 3:5–4:5] ") helpers.write("across lines") expect.equality(views.anchored()[1].snippet, "two\nthree") @@ -185,8 +207,7 @@ describe("a selection", function() it("that is linewise makes a line comment", function() vim.api.nvim_win_set_cursor(0, { 3, 4 }) - select("V") - vim.cmd("'<,'>Comment") + comment_selection("V") expect.equality(lines_title(), " spans.md [line 3] ") helpers.write("whole line") expect.equality(views.anchored()[1].start_col, nil) @@ -203,9 +224,8 @@ describe("a long title", function() columns = vim.o.columns vim.cmd.file(("a-very-long-name"):rep(6) .. ".md") vim.api.nvim_win_set_cursor(0, { 1, 4 }) - select("viw") vim.o.columns = 60 - vim.cmd("'<,'>Comment") + comment_selection("viw") end) after_each(function() -- 2.54.1