fix(nav): followup for #976

This commit is contained in:
Lewis Russell 2024-04-05 13:52:40 +01:00
parent 59bdc1851c
commit ee5b6ba0b5
2 changed files with 45 additions and 4 deletions

View file

@ -325,6 +325,9 @@ function M.find_nearest_hunk(lnum, hunks, direction, wrap)
elseif direction == 'last' then
return #hunks
elseif direction == 'next' then
if hunks[1].added.start > lnum then
return 1
end
for i = #hunks, 1, -1 do
if hunks[i].added.start <= lnum then
if i + 1 <= #hunks and hunks[i + 1].added.start > lnum then
@ -335,6 +338,9 @@ function M.find_nearest_hunk(lnum, hunks, direction, wrap)
end
end
elseif direction == 'prev' then
if math.max(hunks[#hunks].vend) < lnum then
return #hunks
end
for i = 1, #hunks do
if lnum <= math.max(hunks[i].vend, 1) then
if i > 1 and math.max(hunks[i - 1].vend, 1) < lnum then

View file

@ -236,6 +236,10 @@ describe('actions', function()
end)
end)
local function check_cursor(pos)
eq(pos, helpers.api.nvim_win_get_cursor(0))
end
it('can navigate hunks', function()
setup_test_repo()
edit(test_file)
@ -250,10 +254,6 @@ describe('actions', function()
'@@ -7,1 +6,1 @@',
})
local function check_cursor(pos)
eq(pos, helpers.api.nvim_win_get_cursor(0))
end
check_cursor({ 6, 0 })
command('Gitsigns next_hunk') -- Wrap
check_cursor({ 1, 0 })
@ -268,5 +268,40 @@ describe('actions', function()
check_cursor({ 1, 0 })
command('Gitsigns prev_hunk') -- Wrap
check_cursor({ 6, 0 })
end)
it('can navigate hunks (nowrap)', function()
setup_test_repo()
edit(test_file)
feed('4Gx')
feed('6Gx')
feed('gg')
expect_hunks({
'@@ -4,1 +4,1 @@',
'@@ -6,1 +6,1 @@',
})
command('set nowrapscan')
check_cursor({ 1, 0 })
command('Gitsigns next_hunk')
check_cursor({ 4, 0 })
command('Gitsigns next_hunk')
check_cursor({ 6, 0 })
command('Gitsigns next_hunk')
check_cursor({ 6, 0 })
feed('G')
check_cursor({ 18, 0 })
command('Gitsigns prev_hunk')
check_cursor({ 6, 0 })
command('Gitsigns prev_hunk')
check_cursor({ 4, 0 })
command('Gitsigns prev_hunk')
check_cursor({ 4, 0 })
end)
end)