Some hunk related refactoring

This commit is contained in:
Lewis Russell 2021-04-13 09:44:14 +00:00
parent 7c493175f3
commit 7ee92f88bf
4 changed files with 20 additions and 52 deletions

View File

@ -591,7 +591,7 @@ local function add_debug_functions()
local item = raw_item
return { '...', length = #item, head = item[1] }
elseif not vim.tbl_isempty(raw_item) and vim.tbl_contains({
'hunks', 'staged_diffs', }, key) then
'staged_diffs', }, key) then
return { '...', length = #vim.tbl_keys(raw_item) }
elseif key == 'pending_signs' then
local keys = vim.tbl_keys(raw_item)
@ -748,9 +748,7 @@ local function select_hunk()
return
end
local start, dend = gs_hunks.get_range(hunk)
vim.cmd('normal! ' .. start .. 'GV' .. dend .. 'G')
vim.cmd('normal! ' .. hunk.start .. 'GV' .. hunk.dend .. 'G')
end
local blame_line = void_async(function()

View File

@ -73,26 +73,15 @@ function M.parse_diff_line(line)
return hunk
end
function M.get_range(hunk)
local dend = hunk.dend
if hunk.type == "change" then
local add, remove = hunk.added.count, hunk.removed.count
if add > remove then
dend = dend + add - remove
end
end
return hunk.start, dend
end
function M.process_hunks(hunks)
local signs = {}
for _, hunk in ipairs(hunks) do
local count = hunk.type == 'add' and hunk.added.count or hunk.removed.count
for i = hunk.start, hunk.dend do
local topdelete = hunk.type == 'delete' and i == 0
local changedelete = hunk.type == 'change' and hunk.removed.count > hunk.added.count and i == hunk.dend
local count = hunk.type == 'add' and hunk.added.count or hunk.removed.count
local lnum = topdelete and 1 or i
signs[lnum] = {
signs[topdelete and 1 or i] = {
type = topdelete and 'topdelete' or changedelete and 'changedelete' or hunk.type,
count = i == hunk.start and count,
}
@ -100,12 +89,11 @@ function M.process_hunks(hunks)
if hunk.type == "change" then
local add, remove = hunk.added.count, hunk.removed.count
if add > remove then
local count = add - remove
for i = 1, count do
local lnum = hunk.dend + i
signs[lnum] = {
local count_diff = add - remove
for i = 1, count_diff do
signs[hunk.dend + i] = {
type = 'add',
count = i == 1 and count,
count = i == 1 and count_diff,
}
end
end
@ -183,9 +171,7 @@ function M.find_hunk(lnum, hunks)
return hunk
end
local start, dend = M.get_range(hunk)
if start <= lnum and dend >= lnum then
if hunk.start <= lnum and hunk.dend >= lnum then
return hunk
end
end

View File

@ -591,7 +591,7 @@ local function add_debug_functions()
local item <const> = raw_item as {string}
return { '...', length=#item, head=item[1] }
elseif not vim.tbl_isempty(raw_item) and vim.tbl_contains({
'hunks', 'staged_diffs' }, key) then
'staged_diffs' }, key) then
return { '...', length=#vim.tbl_keys(raw_item) }
elseif key == 'pending_signs' then
local keys = vim.tbl_keys(raw_item)
@ -748,9 +748,7 @@ local function select_hunk()
return
end
local start, dend = gs_hunks.get_range(hunk)
vim.cmd('normal! '..start..'GV'..dend..'G')
vim.cmd('normal! '..hunk.start..'GV'..hunk.dend..'G')
end
local blame_line = void_async(function()

View File

@ -73,26 +73,15 @@ function M.parse_diff_line(line: string): Hunk
return hunk
end
function M.get_range(hunk: Hunk): number, number
local dend = hunk.dend
if hunk.type == "change" then
local add, remove = hunk.added.count, hunk.removed.count
if add > remove then
dend = dend + add - remove
end
end
return hunk.start, dend
end
function M.process_hunks(hunks: {Hunk}): {integer:Sign}
local signs = {}
for _, hunk in ipairs(hunks) do
local count = hunk.type == 'add' and hunk.added.count or hunk.removed.count
for i = hunk.start, hunk.dend do
local topdelete = hunk.type == 'delete' and i == 0
local changedelete = hunk.type == 'change' and hunk.removed.count > hunk.added.count and i == hunk.dend
local count = hunk.type == 'add' and hunk.added.count or hunk.removed.count
local lnum = topdelete and 1 or i
signs[lnum] = {
-- topdelete signs get placed one row lower
signs[topdelete and 1 or i] = {
type = topdelete and 'topdelete' or changedelete and 'changedelete' or hunk.type,
count = i == hunk.start and count
}
@ -100,12 +89,11 @@ function M.process_hunks(hunks: {Hunk}): {integer:Sign}
if hunk.type == "change" then
local add, remove = hunk.added.count, hunk.removed.count
if add > remove then
local count = add - remove
for i = 1, count do
local lnum = hunk.dend + i
signs[lnum] = {
local count_diff = add - remove
for i = 1, count_diff do
signs[hunk.dend + i] = {
type = 'add',
count = i == 1 and count
count = i == 1 and count_diff
}
end
end
@ -183,9 +171,7 @@ function M.find_hunk(lnum: number, hunks: {Hunk}): Hunk
return hunk
end
local start, dend = M.get_range(hunk)
if start <= lnum and dend >= lnum then
if hunk.start <= lnum and hunk.dend >= lnum then
return hunk
end
end