Skip to content

Commit 6ef8c54

Browse files
committed
fix(preview): handle staged hunks
1 parent 175e74f commit 6ef8c54

File tree

3 files changed

+39
-19
lines changed

3 files changed

+39
-19
lines changed

lua/gitsigns/actions.lua

+35-16
Original file line numberDiff line numberDiff line change
@@ -149,9 +149,10 @@ M.toggle_deleted = function(value)
149149
return config.show_deleted
150150
end
151151

152-
---@param bufnr? integer
153-
---@param hunks? Gitsigns.Hunk.Hunk[]?
154-
---@return Gitsigns.Hunk.Hunk?
152+
--- @param bufnr? integer
153+
--- @param hunks? Gitsigns.Hunk.Hunk[]?
154+
--- @return Gitsigns.Hunk.Hunk? hunk
155+
--- @return integer? index
155156
local function get_cursor_hunk(bufnr, hunks)
156157
bufnr = bufnr or current_buf()
157158

@@ -185,13 +186,14 @@ local function get_range(params)
185186
return range
186187
end
187188

189+
--- @async
188190
--- @param bufnr integer
189191
--- @param bcache Gitsigns.CacheEntry
190192
--- @param greedy? boolean
191193
--- @param staged? boolean
192194
--- @return Gitsigns.Hunk.Hunk[]? hunks
193195
local function get_hunks(bufnr, bcache, greedy, staged)
194-
if greedy then
196+
if greedy and config.diff_opts.linematch then
195197
-- Re-run the diff without linematch
196198
local buftext = util.buf_lines(bufnr)
197199
local text --- @type string[]?
@@ -225,7 +227,8 @@ local function get_hunk(bufnr, range, greedy, staged)
225227
local hunks = get_hunks(bufnr, bcache, greedy, staged)
226228

227229
if not range then
228-
return get_cursor_hunk(bufnr, hunks)
230+
local hunk = get_cursor_hunk(bufnr, hunks)
231+
return hunk
229232
end
230233

231234
table.sort(range)
@@ -298,7 +301,7 @@ end
298301
--- @param bufnr integer
299302
--- @param hunk Gitsigns.Hunk.Hunk
300303
local function reset_hunk(bufnr, hunk)
301-
local lstart, lend ---@type integer, integer
304+
local lstart, lend --- @type integer, integer
302305
if hunk.type == 'delete' then
303306
lstart = hunk.added.start
304307
lend = hunk.added.start
@@ -732,23 +735,39 @@ local function feedkeys(keys)
732735
api.nvim_feedkeys(cy, 'n', false)
733736
end
734737

738+
--- @param bufnr integer
739+
--- @param greedy? boolean
740+
--- @return Gitsigns.Hunk.Hunk? hunk
741+
--- @return boolean? staged
742+
local function get_hunk_with_staged(bufnr, greedy)
743+
local hunk = get_hunk(bufnr, nil, greedy, false)
744+
if hunk then
745+
return hunk, false
746+
end
747+
748+
hunk = get_hunk(bufnr, nil, greedy, true)
749+
if hunk then
750+
return hunk, true
751+
end
752+
end
753+
735754
--- Preview the hunk at the cursor position inline in the buffer.
736-
M.preview_hunk_inline = function()
755+
M.preview_hunk_inline = async.void(function()
737756
local bufnr = current_buf()
738757

739-
local hunk = get_cursor_hunk(bufnr)
758+
local hunk, staged = get_hunk_with_staged(bufnr, true)
740759

741760
if not hunk then
742761
return
743762
end
744763

745764
clear_preview_inline(bufnr)
746765

747-
local winid ---@type integer
766+
local winid --- @type integer
748767
manager.show_added(bufnr, ns_inline, hunk)
749768
if config._inline2 then
750769
if hunk.removed.count > 0 then
751-
winid = manager.show_deleted_in_float(bufnr, ns_inline, hunk)
770+
winid = manager.show_deleted_in_float(bufnr, ns_inline, hunk, staged)
752771
end
753772
else
754773
manager.show_deleted(bufnr, ns_inline, hunk)
@@ -771,7 +790,7 @@ M.preview_hunk_inline = function()
771790
if hunk.added.start <= 1 then
772791
feedkeys(hunk.removed.count .. '<C-y>')
773792
end
774-
end
793+
end)
775794

776795
--- Select the hunk under the cursor.
777796
M.select_hunk = function()
@@ -929,8 +948,8 @@ C.blame_line = function(args, _)
929948
M.blame_line(args)
930949
end
931950

932-
---@param bcache Gitsigns.CacheEntry
933-
---@param base string?
951+
--- @param bcache Gitsigns.CacheEntry
952+
--- @param base string?
934953
local function update_buf_base(bcache, base)
935954
bcache.base = base
936955
bcache:invalidate(true)
@@ -1121,14 +1140,14 @@ local function hunks_to_qflist(buf_or_filename, hunks, qflist)
11211140
end
11221141
end
11231142

1124-
---@param target 'all'|'attached'|integer|nil
1125-
---@return table[]?
1143+
--- @param target 'all'|'attached'|integer|nil
1144+
--- @return table[]?
11261145
local function buildqflist(target)
11271146
target = target or current_buf()
11281147
if target == 0 then
11291148
target = current_buf()
11301149
end
1131-
local qflist = {} ---@type table[]
1150+
local qflist = {} --- @type table[]
11321151

11331152
if type(target) == 'number' then
11341153
local bufnr = target

lua/gitsigns/debug.lua

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ function M.dump_cache()
3030
--- @type string
3131
local text = vim.inspect(cache, { process = process })
3232
vim.api.nvim_echo({ { text } }, false, {})
33-
return cache
3433
end
3534

3635
--- @param noecho boolean

lua/gitsigns/manager.lua

+4-2
Original file line numberDiff line numberDiff line change
@@ -294,8 +294,9 @@ end
294294
--- @param bufnr integer
295295
--- @param nsd integer
296296
--- @param hunk Gitsigns.Hunk.Hunk
297+
--- @param staged boolean?
297298
--- @return integer winid
298-
function M.show_deleted_in_float(bufnr, nsd, hunk)
299+
function M.show_deleted_in_float(bufnr, nsd, hunk, staged)
299300
local cwin = api.nvim_get_current_win()
300301
local virt_lines = {} --- @type {[1]: string, [2]: string}[][]
301302
local textoff = vim.fn.getwininfo(cwin)[1].textoff --[[@as integer]]
@@ -317,7 +318,8 @@ function M.show_deleted_in_float(bufnr, nsd, hunk)
317318

318319
local bcache = cache[bufnr]
319320
local pbufnr = api.nvim_create_buf(false, true)
320-
api.nvim_buf_set_lines(pbufnr, 0, -1, false, bcache.compare_text)
321+
local text = staged and bcache.compare_text_head or bcache.compare_text
322+
api.nvim_buf_set_lines(pbufnr, 0, -1, false, assert(text))
321323

322324
local width = api.nvim_win_get_width(0)
323325

0 commit comments

Comments
 (0)