feat(git): trim hunk preview header, focus float on re-invoke
This commit is contained in:
@@ -52,6 +52,15 @@ local function detailed_marks(buf, ns_name)
|
||||
return vim.api.nvim_buf_get_extmarks(buf, ns, 0, -1, { details = true })
|
||||
end
|
||||
|
||||
---@return integer?
|
||||
local function find_float()
|
||||
for _, w in ipairs(vim.api.nvim_tabpage_list_wins(0)) do
|
||||
if vim.api.nvim_win_get_config(w).relative ~= "" then
|
||||
return w
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
t.test("pure add: hunk shape and add signs", function()
|
||||
local _, buf, state = setup("a\nd\n", "a\nb\nc\nd\n")
|
||||
t.eq(#state.hunks, 1, "one hunk for a pure addition")
|
||||
@@ -296,6 +305,52 @@ t.test("git_hunk_signs falls back to the default for unset kinds", function()
|
||||
})
|
||||
end)
|
||||
|
||||
t.test("preview_hunk shows the hunk body without file headers", function()
|
||||
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
|
||||
vim.api.nvim_set_current_buf(buf)
|
||||
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
||||
hunks.preview_hunk(buf)
|
||||
local float = assert(find_float(), "preview float should open")
|
||||
t.defer(function()
|
||||
pcall(vim.api.nvim_win_close, float, true)
|
||||
end)
|
||||
local lines = vim.api.nvim_buf_get_lines(
|
||||
vim.api.nvim_win_get_buf(float),
|
||||
0,
|
||||
-1,
|
||||
false
|
||||
)
|
||||
t.truthy(
|
||||
vim.startswith(lines[1] or "", "@@ "),
|
||||
"first line is the @@ header"
|
||||
)
|
||||
for _, l in ipairs(lines) do
|
||||
t.falsy(vim.startswith(l, "--- "), "no --- file header line")
|
||||
t.falsy(vim.startswith(l, "+++ "), "no +++ file header line")
|
||||
end
|
||||
end)
|
||||
|
||||
t.test("preview_hunk re-invocation focuses the open float", function()
|
||||
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
|
||||
vim.api.nvim_set_current_buf(buf)
|
||||
vim.api.nvim_win_set_cursor(0, { 2, 0 })
|
||||
hunks.preview_hunk(buf)
|
||||
local float = assert(find_float(), "preview float should open")
|
||||
t.defer(function()
|
||||
pcall(vim.api.nvim_win_close, float, true)
|
||||
end)
|
||||
t.truthy(
|
||||
vim.api.nvim_get_current_win() ~= float,
|
||||
"the float opens unfocused"
|
||||
)
|
||||
hunks.preview_hunk(buf)
|
||||
t.eq(
|
||||
vim.api.nvim_get_current_win(),
|
||||
float,
|
||||
"re-invoking focuses the existing float"
|
||||
)
|
||||
end)
|
||||
|
||||
t.test("nav jumps to next and previous hunks with wrap", function()
|
||||
local _, buf = setup("a\nb\nc\nd\ne\n", "A\nb\nc\nd\nE\n")
|
||||
vim.api.nvim_set_current_buf(buf)
|
||||
|
||||
Reference in New Issue
Block a user