diff --git a/CHANGELOG.md b/CHANGELOG.md index 5641ceb..2a73707 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - Fall back to filesystem lookup when Git cannot start. - Remove stale quickfix entries when clearing comments without touching other lists. - Leave the quickfix list alone if it contains rows from another plugin. +- Export to write-only clipboards without trying to read the clipboard back. ### Changed diff --git a/lua/scratch_comments/export/clipboard.lua b/lua/scratch_comments/export/clipboard.lua index 8e95cce..a4ceb71 100644 --- a/lua/scratch_comments/export/clipboard.lua +++ b/lua/scratch_comments/export/clipboard.lua @@ -3,15 +3,10 @@ local M = {} ---@param text string ---@return boolean function M.copy(text) - local ok = pcall(vim.fn.setreg, "+", text) - if not ok then - return false - end - - -- setreg never errors when no provider is configured, so confirm the - -- register actually holds what we put in it. - local ok_read, stored = pcall(vim.fn.getreg, "+") - return ok_read and stored == text + local ok, written = pcall(function() + return vim.fn.has("clipboard") == 1 and vim.fn.setreg("+", text) == 0 + end) + return ok and written == true end return M diff --git a/lua/scratch_comments/export/init.lua b/lua/scratch_comments/export/init.lua index 806316f..9226b48 100644 --- a/lua/scratch_comments/export/init.lua +++ b/lua/scratch_comments/export/init.lua @@ -43,8 +43,7 @@ function M.export(format, in_buffer) end local anchored, orphans = views.anchored(), views.orphans() - local count = #anchored + #orphans - if count == 0 then + if #anchored + #orphans == 0 then notify.info("No comments to export") return end @@ -60,11 +59,11 @@ function M.export(format, in_buffer) end if not clipboard.copy(text) then - notify.error("Could not copy to the clipboard; no provider configured?") + notify.error("Could not copy to the clipboard; try :CommentExport!") return end - notify.info("Copied " .. count .. " comment(s)") + notify.info("Clipboard write requested") end return M diff --git a/tests/features/list_export_spec.lua b/tests/features/list_export_spec.lua index fa69aea..22eb198 100644 --- a/tests/features/list_export_spec.lua +++ b/tests/features/list_export_spec.lua @@ -1,21 +1,12 @@ -local clipboard = require("scratch_comments.export.clipboard") local helpers = require("helpers") local scratch = require("scratch_comments") local store = require("scratch_comments.model.store") local expect = MiniTest.expect -local copy = clipboard.copy -local copied local source before_each(function() helpers.reset() - copied = nil - ---@diagnostic disable-next-line: duplicate-set-field -- test fake - clipboard.copy = function(text) - copied = text - return true - end source = helpers.buffer("export-fixture.lua", { "local a = 1", "local b = 2", "local c = 3" }) vim.cmd("3Comment") helpers.write("on c") @@ -23,10 +14,6 @@ before_each(function() helpers.write("on a") end) -after_each(function() - clipboard.copy = copy -end) - describe("json", function() it("lists every comment in position order, with its lines and text", function() local data = vim.json.decode(scratch.render("json")) @@ -43,11 +30,42 @@ describe("json", function() end) describe(":CommentExport", function() - it("copies markdown by default, keeping the comments", function() + local original_provider, copied, paste_calls + + local function reload_clipboard_provider(provider) + vim.g.clipboard = provider + vim.g.loaded_clipboard_provider = nil + vim.cmd.runtime("autoload/provider/clipboard.vim") + end + + before_each(function() + original_provider = vim.g.clipboard + copied, paste_calls = nil, 0 + local function copy(lines) + copied = table.concat(lines, "\n") + end + local function paste() + paste_calls = paste_calls + 1 + return { { "old clipboard contents" }, "v" } + end + reload_clipboard_provider({ + copy = { ["+"] = copy, ["*"] = copy }, + paste = { ["+"] = paste, ["*"] = paste }, + }) + end) + + after_each(function() + reload_clipboard_provider(original_provider) + end) + + it("copies Markdown without reading the clipboard back", function() vim.cmd("CommentExport") + expect.equality(paste_calls, 0) expect.no_equality(copied:find("## export-fixture.lua", 1, true), nil) expect.no_equality(copied:find("on a", 1, true), nil) expect.equality(#vim.json.decode(scratch.render("json")).comments, 2) + local messages = helpers.notified() + expect.equality(messages[#messages], "Clipboard write requested") end) it("copies json", function() @@ -55,16 +73,11 @@ describe(":CommentExport", function() expect.equality(vim.json.decode(copied).comments[2].comment, "on c") end) - it("says so when the clipboard can't be written", function() - ---@diagnostic disable-next-line: duplicate-set-field -- test fake - clipboard.copy = function() - return false - end + it("suggests scratch export when the clipboard provider is unavailable", function() + reload_clipboard_provider({}) vim.cmd("CommentExport") - expect.equality( - helpers.notified()[#helpers.notified()], - "Could not copy to the clipboard; no provider configured?" - ) + local messages = helpers.notified() + expect.equality(messages[#messages], "Could not copy to the clipboard; try :CommentExport!") end) it("copies nothing for an unknown format", function()