Skip to content

Commit

Permalink
fix(actions): prev_hunk works with wrap on line 1
Browse files Browse the repository at this point in the history
Fixes #806
  • Loading branch information
lewis6991 committed Apr 4, 2024
1 parent 224a319 commit 2b96835
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 20 deletions.
2 changes: 1 addition & 1 deletion lua/gitsigns/hunks.lua
Expand Up @@ -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
Expand Down
62 changes: 43 additions & 19 deletions test/actions_spec.lua
Expand Up @@ -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
Expand All @@ -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)
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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)

0 comments on commit 2b96835

Please sign in to comment.