Skip to content

Commit

Permalink
fix(preview): handle staged hunks
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Nov 29, 2023
1 parent 175e74f commit 6ef8c54
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 19 deletions.
51 changes: 35 additions & 16 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,10 @@ M.toggle_deleted = function(value)
return config.show_deleted
end

---@param bufnr? integer
---@param hunks? Gitsigns.Hunk.Hunk[]?
---@return Gitsigns.Hunk.Hunk?
--- @param bufnr? integer
--- @param hunks? Gitsigns.Hunk.Hunk[]?
--- @return Gitsigns.Hunk.Hunk? hunk
--- @return integer? index
local function get_cursor_hunk(bufnr, hunks)
bufnr = bufnr or current_buf()

Expand Down Expand Up @@ -185,13 +186,14 @@ local function get_range(params)
return range
end

--- @async
--- @param bufnr integer
--- @param bcache Gitsigns.CacheEntry
--- @param greedy? boolean
--- @param staged? boolean
--- @return Gitsigns.Hunk.Hunk[]? hunks
local function get_hunks(bufnr, bcache, greedy, staged)
if greedy then
if greedy and config.diff_opts.linematch then
-- Re-run the diff without linematch
local buftext = util.buf_lines(bufnr)
local text --- @type string[]?
Expand Down Expand Up @@ -225,7 +227,8 @@ local function get_hunk(bufnr, range, greedy, staged)
local hunks = get_hunks(bufnr, bcache, greedy, staged)

if not range then
return get_cursor_hunk(bufnr, hunks)
local hunk = get_cursor_hunk(bufnr, hunks)
return hunk
end

table.sort(range)
Expand Down Expand Up @@ -298,7 +301,7 @@ end
--- @param bufnr integer
--- @param hunk Gitsigns.Hunk.Hunk
local function reset_hunk(bufnr, hunk)
local lstart, lend ---@type integer, integer
local lstart, lend --- @type integer, integer
if hunk.type == 'delete' then
lstart = hunk.added.start
lend = hunk.added.start
Expand Down Expand Up @@ -732,23 +735,39 @@ local function feedkeys(keys)
api.nvim_feedkeys(cy, 'n', false)
end

--- @param bufnr integer
--- @param greedy? boolean
--- @return Gitsigns.Hunk.Hunk? hunk
--- @return boolean? staged
local function get_hunk_with_staged(bufnr, greedy)
local hunk = get_hunk(bufnr, nil, greedy, false)
if hunk then
return hunk, false
end

hunk = get_hunk(bufnr, nil, greedy, true)
if hunk then
return hunk, true
end
end

--- Preview the hunk at the cursor position inline in the buffer.
M.preview_hunk_inline = function()
M.preview_hunk_inline = async.void(function()
local bufnr = current_buf()

local hunk = get_cursor_hunk(bufnr)
local hunk, staged = get_hunk_with_staged(bufnr, true)

if not hunk then
return
end

clear_preview_inline(bufnr)

local winid ---@type integer
local winid --- @type integer
manager.show_added(bufnr, ns_inline, hunk)
if config._inline2 then
if hunk.removed.count > 0 then
winid = manager.show_deleted_in_float(bufnr, ns_inline, hunk)
winid = manager.show_deleted_in_float(bufnr, ns_inline, hunk, staged)
end
else
manager.show_deleted(bufnr, ns_inline, hunk)
Expand All @@ -771,7 +790,7 @@ M.preview_hunk_inline = function()
if hunk.added.start <= 1 then
feedkeys(hunk.removed.count .. '<C-y>')
end
end
end)

--- Select the hunk under the cursor.
M.select_hunk = function()
Expand Down Expand Up @@ -929,8 +948,8 @@ C.blame_line = function(args, _)
M.blame_line(args)
end

---@param bcache Gitsigns.CacheEntry
---@param base string?
--- @param bcache Gitsigns.CacheEntry
--- @param base string?
local function update_buf_base(bcache, base)
bcache.base = base
bcache:invalidate(true)
Expand Down Expand Up @@ -1121,14 +1140,14 @@ local function hunks_to_qflist(buf_or_filename, hunks, qflist)
end
end

---@param target 'all'|'attached'|integer|nil
---@return table[]?
--- @param target 'all'|'attached'|integer|nil
--- @return table[]?
local function buildqflist(target)
target = target or current_buf()
if target == 0 then
target = current_buf()
end
local qflist = {} ---@type table[]
local qflist = {} --- @type table[]

if type(target) == 'number' then
local bufnr = target
Expand Down
1 change: 0 additions & 1 deletion lua/gitsigns/debug.lua
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ function M.dump_cache()
--- @type string
local text = vim.inspect(cache, { process = process })
vim.api.nvim_echo({ { text } }, false, {})
return cache
end

--- @param noecho boolean
Expand Down
6 changes: 4 additions & 2 deletions lua/gitsigns/manager.lua
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,9 @@ end
--- @param bufnr integer
--- @param nsd integer
--- @param hunk Gitsigns.Hunk.Hunk
--- @param staged boolean?
--- @return integer winid
function M.show_deleted_in_float(bufnr, nsd, hunk)
function M.show_deleted_in_float(bufnr, nsd, hunk, staged)
local cwin = api.nvim_get_current_win()
local virt_lines = {} --- @type {[1]: string, [2]: string}[][]
local textoff = vim.fn.getwininfo(cwin)[1].textoff --[[@as integer]]
Expand All @@ -317,7 +318,8 @@ function M.show_deleted_in_float(bufnr, nsd, hunk)

local bcache = cache[bufnr]
local pbufnr = api.nvim_create_buf(false, true)
api.nvim_buf_set_lines(pbufnr, 0, -1, false, bcache.compare_text)
local text = staged and bcache.compare_text_head or bcache.compare_text
api.nvim_buf_set_lines(pbufnr, 0, -1, false, assert(text))

local width = api.nvim_win_get_width(0)

Expand Down

0 comments on commit 6ef8c54

Please sign in to comment.