feat(git): syntax-highlight deleted lines in the diff overlay

This commit is contained in:
2026-05-20 06:42:41 +02:00
parent aaef6621dd
commit 2064c629ed
2 changed files with 149 additions and 12 deletions
+44 -6
View File
@@ -82,7 +82,7 @@ t.test("pure delete (middle): hunk shape and delete sign", function()
t.eq(hk.new_count, 0)
t.eq(hk.old_lines, { "b" })
t.eq(sign_marks(buf), {
{ row = 0, sign = "", hl = "GitHunkDelete" },
{ row = 0, sign = "", hl = "GitHunkDelete" },
})
end)
@@ -94,7 +94,7 @@ t.test("top-of-file delete: sign anchors on line 1", function()
t.eq(hk.new_start, 0)
t.eq(hk.old_lines, { "a" })
t.eq(sign_marks(buf), {
{ row = 0, sign = "", hl = "GitHunkDelete" },
{ row = 0, sign = "", hl = "GitHunkDelete" },
})
end)
@@ -143,7 +143,7 @@ t.test("editing the buffer refreshes signs", function()
t.eq(hk.type, "change")
end)
t.test("overlay:change hunk shows deletion and addition", function()
t.test("overlay: change hunk shows deletion and addition", function()
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
hunks.toggle_overlay(buf)
---@type integer?
@@ -169,7 +169,7 @@ t.test("overlay:change hunk shows deletion and addition", function()
t.eq(piece[2], "GitHunkDeleteLine")
end)
t.test("overlay:delete hunk shows only deletion lines", function()
t.test("overlay: delete hunk shows only deletion lines", function()
local _, buf = setup("a\nb\nc\n", "a\nc\n")
hunks.toggle_overlay(buf)
local marks = detailed_marks(buf, "ow.git.hunks.overlay")
@@ -180,7 +180,7 @@ t.test("overlay:delete hunk shows only deletion lines", function()
t.eq(piece[2], "GitHunkDeleteLine")
end)
t.test("overlay:add hunk highlights the added lines", function()
t.test("overlay: add hunk highlights the added lines", function()
local _, buf = setup("a\nd\n", "a\nb\nc\nd\n")
hunks.toggle_overlay(buf)
local rows = {}
@@ -194,7 +194,45 @@ t.test("overlay:add hunk highlights the added lines", function()
t.eq(rows, { 1, 2 }, "both added lines highlighted")
end)
t.test("overlay:toggling swaps gutter signs for the overlay", function()
t.test("overlay: deleted lines are treesitter-highlighted", function()
local _, buf = setup(
"-- a note\nlocal x = 1\nlocal y = 2\n",
"local y = 2\n",
"a.lua"
)
t.truthy(
pcall(vim.treesitter.start, buf, "lua"),
"the lua parser should be available"
)
hunks.toggle_overlay(buf)
---@type table[]?
local virt
for _, m in ipairs(detailed_marks(buf, "ow.git.hunks.overlay")) do
local d = assert(m[4])
if d.virt_lines then
virt = d.virt_lines
end
end
virt = assert(virt, "a deletion virtual line should render")
---@type table<string, boolean>
local seen = {}
for _, line in ipairs(virt) do
for _, c in ipairs(line) do
local hl = c[2]
if
type(hl) == "table"
and hl[1] == "GitHunkDeleteLine"
and hl[2]
then
seen[hl[2]] = true
end
end
end
t.truthy(seen["@comment"], "the deleted comment keeps its @comment group")
t.truthy(seen["@keyword"], "deleted code keeps its syntax groups")
end)
t.test("overlay: toggling swaps gutter signs for the overlay", function()
local _, buf = setup("a\nb\nc\n", "a\nB\nc\n")
t.truthy(
#detailed_marks(buf, "ow.git.hunks") > 0,