diff --git a/lua/gitsigns/hunks.lua b/lua/gitsigns/hunks.lua index e9656f75e..096785fbf 100644 --- a/lua/gitsigns/hunks.lua +++ b/lua/gitsigns/hunks.lua @@ -336,7 +336,7 @@ function M.find_nearest_hunk(lnum, hunks, forwards, wrap) else for i = #hunks, 1, -1 do local hunk = hunks[i] - local dist = lnum - hunk.vend + local dist = lnum - math.max(hunk.vend, 1) if dist > 0 and dist < distance then distance = dist ret = hunk diff --git a/test/actions_spec.lua b/test/actions_spec.lua index e95511633..4debf124d 100644 --- a/test/actions_spec.lua +++ b/test/actions_spec.lua @@ -30,18 +30,20 @@ local function expect_hunks(exp_hunks) #exp_hunks, #hunks ) - msg[#msg + 1] = '' - msg[#msg + 1] = 'Expected hunks:' + + msg[#msg + 1] = '\nExpected hunks:' for _, h in ipairs(exp_hunks) do - msg[#msg + 1] = h.head + msg[#msg + 1] = h end - msg[#msg + 1] = '' - msg[#msg + 1] = 'Passed in hunks:' + + msg[#msg + 1] = '\nPassed in hunks:' for _, h in ipairs(hunks) do msg[#msg + 1] = h.head end + error(table.concat(msg, '\n')) end + for i, hunk in ipairs(hunks) do eq(exp_hunks[i], hunk.head) end @@ -60,10 +62,6 @@ describe('actions', function() setup_gitsigns(config) end) - after_each(function() - -- helpers.cleanup() - end) - it('works with commands', function() setup_test_repo() edit(test_file) @@ -125,16 +123,7 @@ describe('actions', function() end) local function set_lines(start, dend, lines) - exec_lua( - [[ - local start, dend, lines = ... - vim.api.nvim_buf_set_lines(0, start, dend, false, lines) - ]], - start, - dend, - lines - ) - -- command('write') + helpers.api.nvim_buf_set_lines(0, start, dend, false, lines) end describe('can stage add hunks', function() @@ -246,4 +235,39 @@ describe('actions', function() expect_hunks({}) end) end) + + it('can navigate hunks', function() + setup_test_repo() + edit(test_file) + + feed('dd') + feed('4Gx') + feed('6Gx') + + expect_hunks({ + '@@ -1,1 +0 @@', + '@@ -5,1 +4,1 @@', + '@@ -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}) + command('Gitsigns next_hunk') + check_cursor({4,0}) + command('Gitsigns next_hunk') + check_cursor({6,0}) + + command('Gitsigns prev_hunk') + check_cursor({4,0}) + command('Gitsigns prev_hunk') + check_cursor({1,0}) + command('Gitsigns prev_hunk') -- Wrap + check_cursor({6,0}) + + end) end)