Skip to content

Commit

Permalink
fix(nav): followup for #976
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Apr 5, 2024
1 parent 59bdc18 commit ee5b6ba
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 4 deletions.
6 changes: 6 additions & 0 deletions lua/gitsigns/hunks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
43 changes: 39 additions & 4 deletions test/actions_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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 })
Expand All @@ -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)

0 comments on commit ee5b6ba

Please sign in to comment.