Files

264 lines
9.8 KiB
Lua
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
local helpers = require("helpers")
local expect = MiniTest.expect
local size
local source
before_each(function()
helpers.reset()
size = { vim.o.columns, vim.o.lines }
source = helpers.buffer("notes.md", { "one", "two", "three", "four" })
vim.cmd("1,3Comment")
helpers.write("on one to three")
vim.api.nvim_win_set_cursor(0, { 1, 0 })
vim.cmd.CommentList()
vim.cmd("normal \r")
vim.cmd("1,3Comment")
end)
after_each(function()
vim.o.columns, vim.o.lines = size[1], size[2]
end)
describe("the comment window", function()
it("shows the commented lines above the comment", function()
local lines_win, comment_win = helpers.panes()
expect.equality(#helpers.floats(), 2)
expect.equality(helpers.text(vim.api.nvim_win_get_buf(comment_win)), "on one to three")
expect.equality(helpers.text(vim.api.nvim_win_get_buf(lines_win)), "one\ntwo\nthree")
expect.equality(helpers.text(source), "one\ntwo\nthree\nfour")
end)
it("titles the lines pane with the file and lines", function()
expect.equality(helpers.title((helpers.panes())), " notes.md [lines 1–3] ")
end)
it("names an existing comment after its source file and line", function()
local bufnr = vim.api.nvim_get_current_buf()
expect.equality(
vim.api.nvim_buf_get_name(bufnr),
"scratch-comments://" .. bufnr .. "/notes.md:1 [comment]"
)
end)
it("titles the comment pane with the count and mode", function()
local _, comment_win = helpers.panes()
expect.equality(helpers.title(comment_win), " comment (1 of 1) [NORMAL] ")
end)
it("shows the mode as it changes", function()
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 (1 of 1) [INSERT] , comment (1 of 1) [NORMAL] "
)
end)
it("can edit the comment, but not the lines", function()
local lines_win = helpers.panes()
expect.equality(vim.bo.modifiable, true)
expect.equality(vim.bo[vim.api.nvim_win_get_buf(lines_win)].modifiable, false)
end)
it("uses the editor background, padding included", function()
local lines_win, comment_win = helpers.panes()
for _, win in ipairs({ lines_win, comment_win }) do
local groups = vim.split(vim.wo[win].winhighlight, ",")
expect.equality(groups[1], "NormalFloat:Normal")
for _, group in ipairs(groups) do
expect.equality(vim.endswith(group, ":Normal"), true)
end
expect.equality(#groups, 7)
end
end)
it("follows the editor's size", function()
vim.o.columns, vim.o.lines = 60, 20
vim.api.nvim_exec_autocmds("VimResized", {})
local lines_win, comment_win = helpers.panes()
for _, win in ipairs({ lines_win, comment_win }) do
local config = vim.api.nvim_win_get_config(win)
expect.equality(config.width, 56)
expect.equality(config.col, 2)
end
local top = vim.api.nvim_win_get_config(lines_win)
local bottom = vim.api.nvim_win_get_config(comment_win)
expect.equality(bottom.row, top.row + top.height + 2)
expect.equality(top.height <= math.floor(20 * 0.4), true)
expect.equality(helpers.title(lines_win), " notes.md [lines 1–3] ")
end)
it("resizes with its source window when quickfix closes", function()
local source_win = require("scratch_comments.ui.window").current().source_win
local dim = vim.iter(helpers.every_float()):find(function(win)
return vim.w[win].scratch_comments_backdrop
end)
local before = vim.api.nvim_win_get_config(dim).height
vim.cmd.cclose()
vim.api.nvim_exec_autocmds("WinResized", {})
expect.equality(vim.api.nvim_win_get_height(source_win) > before, true)
expect.equality(
vim.api.nvim_win_get_config(dim).height,
vim.api.nvim_win_get_height(source_win)
)
end)
it("keeps a small card inside a very short source window", function()
local record = assert(require("scratch_comments.ui.window").current())
---@cast record { comment_win: integer, source_win: integer }
local comment_buf = vim.api.nvim_win_get_buf(record.comment_win)
vim.api.nvim_buf_set_lines(comment_buf, 0, -1, false, { "unsaved" })
vim.api.nvim_set_current_win(record.source_win)
vim.cmd("resize 3")
vim.api.nvim_exec_autocmds("WinResized", {})
local height = vim.api.nvim_win_get_height(record.source_win)
local config = vim.api.nvim_win_get_config(record.comment_win)
expect.equality(height < 6, true)
expect.equality(config.row + config.height <= height, true)
expect.equality(vim.bo[comment_buf].modified, true)
expect.equality(helpers.text(comment_buf), "unsaved")
end)
it("keeps a card in a narrow editing split", function()
local record = assert(require("scratch_comments.ui.window").current())
---@cast record { comment_win: integer, source_win: integer }
vim.api.nvim_set_current_win(record.source_win)
vim.cmd.vsplit()
vim.api.nvim_set_current_win(record.source_win)
vim.cmd("vertical resize 4")
vim.api.nvim_exec_autocmds("WinResized", {})
local config = vim.api.nvim_win_get_config(record.comment_win)
expect.equality(vim.api.nvim_win_get_width(record.source_win) <= 5, true)
expect.equality(config.width <= vim.api.nvim_win_get_width(record.source_win), true)
end)
it("keeps unsaved edits when the source window closes", function()
local record = assert(require("scratch_comments.ui.window").current())
---@cast record { comment_win: integer, source_win: integer }
local comment_buf = vim.api.nvim_win_get_buf(record.comment_win)
vim.api.nvim_buf_set_lines(comment_buf, 0, -1, false, { "unsaved" })
vim.api.nvim_set_current_win(record.source_win)
vim.api.nvim_win_close(record.source_win, true)
vim.wait(0)
expect.equality(vim.api.nvim_win_is_valid(record.comment_win), true)
expect.equality(helpers.text(comment_buf), "unsaved")
expect.equality(vim.bo[comment_buf].modified, true)
local parent = assert(vim.api.nvim_win_get_config(record.comment_win).win)
expect.equality(vim.api.nvim_win_is_valid(parent), true)
expect.no_equality(vim.bo[vim.api.nvim_win_get_buf(parent)].buftype, "quickfix")
end)
it("closes a clean card if its source window was the last file pane", function()
local record = assert(require("scratch_comments.ui.window").current())
---@cast record { comment_win: integer, source_win: integer }
vim.api.nvim_set_current_win(record.source_win)
vim.api.nvim_win_close(record.source_win, true)
vim.wait(0)
expect.equality(vim.api.nvim_win_is_valid(record.comment_win), false)
expect.equality(vim.bo.buftype, "quickfix")
end)
it("closes on <Esc> in normal mode", function()
vim.cmd([[execute "normal \<Esc>"]])
expect.equality(#helpers.floats(), 0)
end)
it("stays open on <Esc> with unsaved changes", function()
vim.api.nvim_buf_set_lines(0, 0, -1, false, { "unsaved" })
vim.cmd([[execute "normal \<Esc>"]])
expect.equality(#helpers.floats(), 2)
expect.equality(helpers.notified()[#helpers.notified()], "Save with :w, or discard with :q!")
end)
it("closes on <Esc> from the lines pane too", function()
vim.cmd("wincmd w")
vim.cmd([[execute "normal \<Esc>"]])
expect.equality(#helpers.floats(), 0)
end)
it("dims the editor behind it, and stops when it closes", function()
local dim = vim.tbl_filter(function(win)
return vim.w[win].scratch_comments_backdrop
end, helpers.every_float())
expect.equality(#dim, 1)
local config = vim.api.nvim_win_get_config(dim[1])
local source_win = require("scratch_comments.ui.window").current().source_win
expect.equality(config.relative, "win")
expect.equality(config.win, source_win)
expect.equality(
{ config.width, config.height },
{ vim.api.nvim_win_get_width(source_win), vim.api.nvim_win_get_height(source_win) }
)
expect.equality(config.zindex < vim.api.nvim_win_get_config(0).zindex, true)
expect.equality(vim.wo[dim[1]].winblend, 60)
vim.cmd("quit")
expect.equality(#helpers.every_float(), 0)
end)
it("takes its resize handler with it when it closes", function()
local function handlers()
return #vim.api.nvim_get_autocmds({ event = "VimResized" })
end
vim.cmd("quit")
local baseline = handlers()
vim.cmd.CommentList()
vim.cmd.Comment()
expect.equality(handlers() > baseline, true)
vim.cmd("quit")
expect.equality(handlers(), baseline)
end)
it("closes both panes when one closes", function()
vim.cmd("close")
expect.equality(#helpers.floats(), 0)
end)
end)
describe("the border", function()
local winborder
before_each(function()
winborder = vim.o.winborder
vim.cmd("quit")
end)
after_each(function()
vim.o.winborder = winborder
end)
it("is rounded when 'winborder' is not set", function()
vim.o.winborder = ""
vim.cmd.CommentList()
vim.cmd.Comment()
expect.equality(vim.api.nvim_win_get_config(0).border[1], "╭")
end)
it("follows 'winborder' when it is set", function()
vim.o.winborder = "single"
vim.cmd.CommentList()
vim.cmd.Comment()
expect.equality(vim.api.nvim_win_get_config(0).border[1], "┌")
end)
it("restores a custom border when a short source window grows again", function()
vim.o.winborder = "single"
vim.cmd.CommentList()
vim.cmd.Comment()
local record = assert(require("scratch_comments.ui.window").current())
---@cast record { comment_win: integer, source_win: integer }
vim.api.nvim_set_current_win(record.source_win)
vim.cmd("resize 3")
vim.api.nvim_exec_autocmds("WinResized", {})
vim.cmd("resize 12")
vim.api.nvim_exec_autocmds("WinResized", {})
expect.equality(vim.api.nvim_win_get_config(record.comment_win).border[1], "┌")
end)
end)