152 lines
4.9 KiB
Lua
152 lines
4.9 KiB
Lua
local helpers = require("helpers")
|
|
local scratch = require("scratch_comments")
|
|
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", "five", "six" })
|
|
end)
|
|
|
|
describe(":Comment", function()
|
|
it("comments on the cursor line", function()
|
|
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
|
vim.cmd("Comment")
|
|
helpers.write("first comment")
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(views.anchored()[1].start_line, 2)
|
|
expect.equality(views.anchored()[1].comment, "first comment")
|
|
end)
|
|
|
|
it("opens an existing comment on the exact range for editing", function()
|
|
vim.cmd("Comment")
|
|
helpers.write("first comment")
|
|
vim.cmd("Comment")
|
|
expect.equality(helpers.text(), "first comment")
|
|
expect.equality(vim.bo.modifiable, true)
|
|
helpers.write("edited comment")
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(views.anchored()[1].comment, "edited comment")
|
|
end)
|
|
|
|
it("on a range adds a separate comment over those lines", function()
|
|
vim.cmd("1Comment")
|
|
helpers.write("on one")
|
|
vim.cmd("1,3Comment")
|
|
helpers.write("on one to three")
|
|
expect.equality(#store.all(), 2)
|
|
expect.equality(views.anchored()[2].end_line, 3)
|
|
end)
|
|
end)
|
|
|
|
describe("the comment window", function()
|
|
it("names the buffer after the source file and line", function()
|
|
vim.api.nvim_win_set_cursor(0, { 3, 0 })
|
|
vim.cmd("Comment")
|
|
local bufnr = vim.api.nvim_get_current_buf()
|
|
local name = vim.api.nvim_buf_get_name(bufnr)
|
|
expect.equality(name, "scratch-comments://" .. bufnr .. "/notes.md:3 [comment]")
|
|
expect.equality(vim.fn.fnamemodify(name, ":t"), "notes.md:3 [comment]")
|
|
end)
|
|
|
|
it("titles a new comment with the mode as it changes", function()
|
|
vim.cmd("Comment")
|
|
local titles = {}
|
|
vim.api.nvim_create_autocmd("ModeChanged", {
|
|
buffer = 0,
|
|
callback = function()
|
|
table.insert(titles, helpers.title(0))
|
|
end,
|
|
})
|
|
vim.cmd("normal! ihello")
|
|
expect.equality(table.concat(titles, ","), " comment [INSERT] , comment [NORMAL] ")
|
|
end)
|
|
|
|
it("keeps one comment when saved twice", function()
|
|
vim.cmd("Comment")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "first line", "second line" })
|
|
vim.cmd("w")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "first line", "second line, revised" })
|
|
vim.cmd("wq")
|
|
expect.equality(#store.all(), 1)
|
|
expect.equality(store.all()[1].comment, "first line\nsecond line, revised")
|
|
end)
|
|
|
|
it("adds nothing when a new empty comment is saved", function()
|
|
vim.cmd("Comment")
|
|
vim.cmd.stopinsert()
|
|
vim.cmd.write()
|
|
vim.cmd.quit()
|
|
expect.equality(#store.all(), 0)
|
|
end)
|
|
|
|
it("deletes a comment when saved empty after its first write", function()
|
|
vim.cmd("Comment")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "first" })
|
|
vim.cmd.write()
|
|
expect.equality(#store.all(), 1)
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "" })
|
|
vim.cmd.write()
|
|
vim.wait(0)
|
|
expect.equality(#store.all(), 0)
|
|
expect.equality(#helpers.floats(), 0)
|
|
end)
|
|
|
|
it("adds nothing when discarded with :q!", function()
|
|
vim.cmd("Comment")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "hello" })
|
|
vim.cmd("q!")
|
|
expect.equality(#store.all(), 0)
|
|
expect.equality(#helpers.floats(), 0)
|
|
end)
|
|
|
|
it("closes with :q when untouched", function()
|
|
vim.cmd("Comment")
|
|
vim.cmd("quit")
|
|
expect.equality(#helpers.floats(), 0)
|
|
end)
|
|
|
|
it("stays open on :q with unsaved text", function()
|
|
vim.cmd("Comment")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
|
|
pcall(vim.cmd.quit)
|
|
expect.equality(#helpers.floats(), 2)
|
|
end)
|
|
|
|
it("keeps an unsaved comment open when the lines pane is closed", function()
|
|
vim.cmd("Comment")
|
|
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
|
|
vim.cmd("wincmd w")
|
|
pcall(vim.cmd.quit)
|
|
expect.equality(#helpers.floats(), 1)
|
|
vim.api.nvim_set_current_win(helpers.floats()[1])
|
|
vim.cmd("q!")
|
|
expect.equality(#helpers.floats(), 0)
|
|
expect.equality(#store.all(), 0)
|
|
end)
|
|
end)
|
|
|
|
describe("scratch.add()", function()
|
|
it("defaults to the cursor line, or takes a range", function()
|
|
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
|
scratch.add()
|
|
helpers.write("on the cursor line")
|
|
scratch.add(4, 5)
|
|
helpers.write("on lines 4-5")
|
|
local ranges = vim.tbl_map(function(view)
|
|
return view.start_line .. "-" .. view.end_line
|
|
end, views.anchored())
|
|
expect.equality(table.concat(ranges, ","), "2-2,4-5")
|
|
end)
|
|
end)
|
|
|
|
describe("in a buffer with no name", function()
|
|
it("warns and opens nothing", function()
|
|
vim.cmd.enew()
|
|
vim.cmd("Comment")
|
|
expect.equality(#helpers.floats(), 0)
|
|
expect.equality(helpers.notified(), { "Save the buffer before commenting on it" })
|
|
end)
|
|
end)
|