Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(actions): set less lines when reseting hunks #897

Merged
merged 1 commit into from
Oct 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading