diff --git a/lua/gitsigns/hunks.lua b/lua/gitsigns/hunks.lua index f383fa2f7..3b79dccaa 100644 --- a/lua/gitsigns/hunks.lua +++ b/lua/gitsigns/hunks.lua @@ -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 diff --git a/test/actions_spec.lua b/test/actions_spec.lua index e7b98cc6d..c0c75c86b 100644 --- a/test/actions_spec.lua +++ b/test/actions_spec.lua @@ -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)