Skip to content

Commit

Permalink
fix(actions): set less lines when reseting hunks
Browse files Browse the repository at this point in the history
Fixes #637
  • Loading branch information
lewis6991 committed Oct 2, 2023
1 parent 609fffa commit 61e0130
Showing 1 changed file with 26 additions and 10 deletions.
36 changes: 26 additions & 10 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -281,6 +281,7 @@ M.stage_hunk = mk_repeatable(async.void(function(range, opts)
end

if not hunk then
api.nvim_echo({ { 'No hunk to stage', 'WarningMsg' } }, false, {})
return
end

Expand All @@ -296,6 +297,20 @@ C.stage_hunk = function(_, params)
M.stage_hunk(get_range(params))
end

--- @param bufnr integer
--- @param hunk Gitsigns.Hunk.Hunk
local function reset_hunk(bufnr, hunk)
local lstart, lend ---@type integer, integer
if hunk.type == 'delete' then
lstart = hunk.added.start
lend = hunk.added.start
else
lstart = hunk.added.start - 1
lend = hunk.added.start - 1 + hunk.added.count
end
util.set_lines(bufnr, lstart, lend, hunk.removed.lines)
end

--- Reset the lines of the hunk at the cursor position, or all
--- lines in the given range. If {range} is provided, all lines in
--- the given range are reset. This supports partial-hunks,
Expand All @@ -321,18 +336,11 @@ M.reset_hunk = mk_repeatable(async.void(function(range, opts)
local hunk = get_hunk(bufnr, range, opts.greedy ~= false, false)

if not hunk then
api.nvim_echo({ { 'No hunk to reset', 'WarningMsg' } }, false, {})
return
end

local lstart, lend ---@type integer, integer
if hunk.type == 'delete' then
lstart = hunk.added.start
lend = hunk.added.start
else
lstart = hunk.added.start - 1
lend = hunk.added.start - 1 + hunk.added.count
end
util.set_lines(bufnr, lstart, lend, hunk.removed.lines)
reset_hunk(bufnr, hunk)
end))

C.reset_hunk = function(_, params)
Expand All @@ -347,7 +355,15 @@ M.reset_buffer = function()
return
end

util.set_lines(bufnr, 0, -1, bcache.compare_text)
local hunks = bcache.hunks
if not hunks or #hunks == 0 then
api.nvim_echo({ { 'No unstaged changes in the buffer to reset', 'WarningMsg' } }, false, {})
return
end

for i = #hunks, 1, -1 do
reset_hunk(bufnr, hunks[i])
end
end

--- Undo the last call of stage_hunk().
Expand Down

0 comments on commit 61e0130

Please sign in to comment.