163 lines
5.9 KiB
Lua
163 lines
5.9 KiB
Lua
local helpers = require("helpers")
|
|
local views = require("scratch_comments.model.views")
|
|
local store = require("scratch_comments.model.store")
|
|
local expect = MiniTest.expect
|
|
|
|
before_each(function()
|
|
helpers.reset()
|
|
helpers.buffer("notes.md", { "one", "two", "three", "four" })
|
|
vim.cmd("1Comment")
|
|
helpers.write("on one")
|
|
vim.cmd("1,3Comment")
|
|
helpers.write("on one to three")
|
|
vim.cmd.CommentList()
|
|
end)
|
|
|
|
describe(":CommentDelete", function()
|
|
it("deletes the cursor row without opening a card or jumping", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_win_set_cursor(qf_win, { 2, 0 })
|
|
expect.equality(vim.fn.getqflist({ idx = 0 }).idx, 1)
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(vim.api.nvim_get_current_win(), qf_win)
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(views.anchored()[1].comment, "on one")
|
|
expect.equality(#vim.fn.getqflist(), 1)
|
|
end)
|
|
|
|
it("closes quickfix after deleting its last row", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_win_set_cursor(qf_win, { 2, 0 })
|
|
vim.cmd.CommentDelete()
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 0)
|
|
expect.equality(vim.api.nvim_win_is_valid(qf_win), false)
|
|
end)
|
|
|
|
it("deletes the focused card by its ID", function()
|
|
vim.cmd.Comment()
|
|
vim.cmd.CommentDelete()
|
|
vim.wait(0)
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(views.anchored()[1].comment, "on one to three")
|
|
end)
|
|
|
|
it("deletes the cursor row rather than a different open card", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.cmd.Comment()
|
|
local open = assert(require("scratch_comments.ui.window").current())
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.api.nvim_win_set_cursor(qf_win, { 2, 0 })
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(vim.api.nvim_get_current_win(), qf_win)
|
|
expect.equality(views.anchored()[1].comment, "on one")
|
|
expect.equality(require("scratch_comments.ui.window").current(), open)
|
|
end)
|
|
|
|
it("keeps another open card's unsaved edits when deleting a quickfix row", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.cmd.Comment()
|
|
local card_win = vim.api.nvim_get_current_win()
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.api.nvim_win_set_cursor(qf_win, { 2, 0 })
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(views.anchored()[1].comment, "on one")
|
|
expect.equality(vim.bo[vim.api.nvim_win_get_buf(card_win)].modified, true)
|
|
vim.api.nvim_set_current_win(card_win)
|
|
vim.cmd("q!")
|
|
end)
|
|
|
|
it("keeps quickfix focus when invoked there while an editor is open", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.cmd.Comment()
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.cmd.CommentDelete()
|
|
vim.wait(0)
|
|
expect.equality(vim.api.nvim_get_current_win(), qf_win)
|
|
expect.equality(#store.all(), 1)
|
|
end)
|
|
|
|
it("does not use the open card when another quickfix list replaces ours", function()
|
|
vim.cmd.Comment()
|
|
local qf_win = vim.fn.getqflist({ winid = 0 }).winid
|
|
vim.fn.setqflist({}, " ", { items = { { filename = "other.lua", lnum = 1, text = "other" } } })
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(helpers.notified()[#helpers.notified()], "Not a Scratch Comments list")
|
|
end)
|
|
|
|
it("does not use an open card when the quickfix row is stale", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.cmd.Comment()
|
|
local items = vim.fn.getqflist()
|
|
items[2].user_data = {}
|
|
vim.fn.setqflist({}, "r", { items = items })
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.api.nvim_win_set_cursor(qf_win, { 2, 0 })
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(helpers.notified()[#helpers.notified()], "This comment is gone")
|
|
end)
|
|
|
|
it("does nothing after a card has closed", function()
|
|
vim.cmd.Comment()
|
|
vim.cmd.quit()
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(helpers.notified()[#helpers.notified()], "No comment card open")
|
|
end)
|
|
|
|
it("protects unsaved changes when its quickfix row is selected", function()
|
|
local qf_win = vim.api.nvim_get_current_win()
|
|
vim.cmd.Comment()
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
|
|
vim.api.nvim_set_current_win(qf_win)
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(helpers.notified()[#helpers.notified()], "Save or discard the comment first")
|
|
vim.api.nvim_set_current_win(
|
|
assert(require("scratch_comments.ui.window").current()).comment_win
|
|
)
|
|
vim.cmd("q!")
|
|
end)
|
|
|
|
it("protects unsaved changes", function()
|
|
vim.cmd.Comment()
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
|
|
vim.cmd.CommentDelete()
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(helpers.notified()[#helpers.notified()], "Save or discard the comment first")
|
|
end)
|
|
end)
|
|
|
|
describe("saving an empty comment", function()
|
|
it("deletes the edited comment and closes its card", function()
|
|
vim.cmd.Comment()
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "" })
|
|
vim.cmd.write()
|
|
vim.wait(0)
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(views.anchored()[1].comment, "on one to three")
|
|
expect.equality(#vim.fn.getqflist(), 1)
|
|
end)
|
|
|
|
it("does not delete it when empty changes are discarded", function()
|
|
vim.cmd.Comment()
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "" })
|
|
vim.cmd("q!")
|
|
expect.equality(#store.all(), 2)
|
|
end)
|
|
end)
|
|
|
|
describe(":CommentClear", function()
|
|
it("removes every comment and its marks", function()
|
|
local source = assert(views.anchored()[1].bufnr)
|
|
vim.cmd("CommentClear")
|
|
local namespace = vim.api.nvim_get_namespaces().scratch_comments
|
|
expect.equality(#store.all(), 0)
|
|
expect.equality(#vim.api.nvim_buf_get_extmarks(source, namespace, 0, -1, {}), 0)
|
|
end)
|
|
end)
|