refactor(diffsplit): clarify diff side API
This commit is contained in:
@@ -0,0 +1,129 @@
|
||||
local Revision = require("git.core.revision")
|
||||
local h = require("test.git.helpers")
|
||||
local t = require("test")
|
||||
|
||||
---@return integer[]
|
||||
local function diff_wins()
|
||||
local wins = {}
|
||||
for _, win in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
||||
if vim.wo[win].diff then
|
||||
table.insert(wins, win)
|
||||
end
|
||||
end
|
||||
return wins
|
||||
end
|
||||
|
||||
---@param role "left"|"right"
|
||||
---@return integer
|
||||
local function vertical_diff_win(role)
|
||||
local wins = diff_wins()
|
||||
table.sort(wins, function(a, b)
|
||||
return vim.api.nvim_win_get_position(a)[2]
|
||||
< vim.api.nvim_win_get_position(b)[2]
|
||||
end)
|
||||
local win = role == "left" and wins[1] or wins[#wins]
|
||||
if not win then
|
||||
error("diff window not found")
|
||||
end
|
||||
return win
|
||||
end
|
||||
|
||||
---@param dir string
|
||||
local function cleanup_dir_buffers(dir)
|
||||
for _, buf in ipairs(vim.api.nvim_list_bufs()) do
|
||||
local name = vim.api.nvim_buf_get_name(buf)
|
||||
if name:find(dir, 1, true) then
|
||||
pcall(vim.api.nvim_buf_delete, buf, { force = true })
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
t.test("open from worktree focuses the index pane", function()
|
||||
local dir = h.make_repo({ ["a.txt"] = "old\n" })
|
||||
t.write(dir, "a.txt", "new\n")
|
||||
vim.cmd.edit(vim.fs.joinpath(dir, "a.txt"))
|
||||
|
||||
local current_buf = vim.api.nvim_get_current_buf()
|
||||
require("git.diffsplit").open()
|
||||
|
||||
t.wait_for(function()
|
||||
return #diff_wins() == 2
|
||||
end, "diff windows to open")
|
||||
|
||||
t.eq(vim.api.nvim_get_current_win(), vertical_diff_win("left"))
|
||||
t.truthy(
|
||||
vim.api.nvim_get_current_buf() ~= current_buf,
|
||||
"worktree buffer should lose focus"
|
||||
)
|
||||
end)
|
||||
|
||||
t.test("open from index focuses the worktree pane", function()
|
||||
local dir = h.make_repo({ ["a.txt"] = "old\n" })
|
||||
t.write(dir, "a.txt", "new\n")
|
||||
local r = assert(require("git.core.repo").resolve(dir))
|
||||
local index_buf = require("git.object").buf_for(
|
||||
r,
|
||||
Revision.new({ stage = 0, path = "a.txt" })
|
||||
)
|
||||
vim.api.nvim_set_current_buf(index_buf)
|
||||
|
||||
require("git.diffsplit").open({ layout = "vertical" })
|
||||
|
||||
t.wait_for(function()
|
||||
return #diff_wins() == 2
|
||||
end, "diff windows to open")
|
||||
|
||||
local right = vertical_diff_win("right")
|
||||
t.eq(
|
||||
vim.api.nvim_get_current_win(),
|
||||
right,
|
||||
"worktree buffer should be focused"
|
||||
)
|
||||
t.truthy(
|
||||
vim.api.nvim_get_current_buf() ~= index_buf,
|
||||
"index buffer should lose focus"
|
||||
)
|
||||
end)
|
||||
|
||||
t.test("open_pair keeps old on the left when splitright is set", function()
|
||||
local dir = vim.fn.tempname()
|
||||
vim.fn.mkdir(dir, "p")
|
||||
t.defer(function()
|
||||
cleanup_dir_buffers(dir)
|
||||
vim.fn.delete(dir, "rf")
|
||||
end)
|
||||
|
||||
local old = vim.fs.joinpath(dir, "old.txt")
|
||||
local new = vim.fs.joinpath(dir, "new.txt")
|
||||
vim.fn.writefile({ "old" }, old)
|
||||
vim.fn.writefile({ "new" }, new)
|
||||
|
||||
local prev_splitright = vim.o.splitright
|
||||
vim.o.splitright = true
|
||||
t.defer(function()
|
||||
vim.o.splitright = prev_splitright
|
||||
end)
|
||||
|
||||
local old_buf = vim.fn.bufadd(old)
|
||||
local new_buf = vim.fn.bufadd(new)
|
||||
vim.fn.bufload(old_buf)
|
||||
vim.fn.bufload(new_buf)
|
||||
|
||||
require("git.diffsplit").open_pair(old_buf, new_buf, {
|
||||
layout = "vertical",
|
||||
})
|
||||
|
||||
t.wait_for(function()
|
||||
return #diff_wins() == 2
|
||||
end, "diff windows to open")
|
||||
|
||||
local left = vertical_diff_win("left")
|
||||
local right = vertical_diff_win("right")
|
||||
t.eq(vim.api.nvim_win_get_buf(left), old_buf, "old pane should be left")
|
||||
t.eq(vim.api.nvim_win_get_buf(right), new_buf, "new pane should be right")
|
||||
t.eq(
|
||||
vim.api.nvim_get_current_win(),
|
||||
right,
|
||||
"new pane should be focused by default"
|
||||
)
|
||||
end)
|
||||
@@ -341,7 +341,7 @@ t.test(
|
||||
error("a non-sidebar window should remain after close")
|
||||
end
|
||||
vim.api.nvim_set_current_win(remaining)
|
||||
require("git.diffsplit").open({ mods = { vertical = true } })
|
||||
require("git.diffsplit").open({ layout = "vertical" })
|
||||
t.wait_for(function()
|
||||
local count = 0
|
||||
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
||||
|
||||
Reference in New Issue
Block a user