fix(preview): handle staged hunks
This commit is contained in:
parent
175e74f87d
commit
6ef8c54fb5
|
@ -149,9 +149,10 @@ M.toggle_deleted = function(value)
|
||||||
return config.show_deleted
|
return config.show_deleted
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bufnr? integer
|
--- @param bufnr? integer
|
||||||
---@param hunks? Gitsigns.Hunk.Hunk[]?
|
--- @param hunks? Gitsigns.Hunk.Hunk[]?
|
||||||
---@return Gitsigns.Hunk.Hunk?
|
--- @return Gitsigns.Hunk.Hunk? hunk
|
||||||
|
--- @return integer? index
|
||||||
local function get_cursor_hunk(bufnr, hunks)
|
local function get_cursor_hunk(bufnr, hunks)
|
||||||
bufnr = bufnr or current_buf()
|
bufnr = bufnr or current_buf()
|
||||||
|
|
||||||
|
@ -185,13 +186,14 @@ local function get_range(params)
|
||||||
return range
|
return range
|
||||||
end
|
end
|
||||||
|
|
||||||
|
--- @async
|
||||||
--- @param bufnr integer
|
--- @param bufnr integer
|
||||||
--- @param bcache Gitsigns.CacheEntry
|
--- @param bcache Gitsigns.CacheEntry
|
||||||
--- @param greedy? boolean
|
--- @param greedy? boolean
|
||||||
--- @param staged? boolean
|
--- @param staged? boolean
|
||||||
--- @return Gitsigns.Hunk.Hunk[]? hunks
|
--- @return Gitsigns.Hunk.Hunk[]? hunks
|
||||||
local function get_hunks(bufnr, bcache, greedy, staged)
|
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
|
-- Re-run the diff without linematch
|
||||||
local buftext = util.buf_lines(bufnr)
|
local buftext = util.buf_lines(bufnr)
|
||||||
local text --- @type string[]?
|
local text --- @type string[]?
|
||||||
|
@ -225,7 +227,8 @@ local function get_hunk(bufnr, range, greedy, staged)
|
||||||
local hunks = get_hunks(bufnr, bcache, greedy, staged)
|
local hunks = get_hunks(bufnr, bcache, greedy, staged)
|
||||||
|
|
||||||
if not range then
|
if not range then
|
||||||
return get_cursor_hunk(bufnr, hunks)
|
local hunk = get_cursor_hunk(bufnr, hunks)
|
||||||
|
return hunk
|
||||||
end
|
end
|
||||||
|
|
||||||
table.sort(range)
|
table.sort(range)
|
||||||
|
@ -298,7 +301,7 @@ end
|
||||||
--- @param bufnr integer
|
--- @param bufnr integer
|
||||||
--- @param hunk Gitsigns.Hunk.Hunk
|
--- @param hunk Gitsigns.Hunk.Hunk
|
||||||
local function reset_hunk(bufnr, hunk)
|
local function reset_hunk(bufnr, hunk)
|
||||||
local lstart, lend ---@type integer, integer
|
local lstart, lend --- @type integer, integer
|
||||||
if hunk.type == 'delete' then
|
if hunk.type == 'delete' then
|
||||||
lstart = hunk.added.start
|
lstart = hunk.added.start
|
||||||
lend = hunk.added.start
|
lend = hunk.added.start
|
||||||
|
@ -732,11 +735,27 @@ local function feedkeys(keys)
|
||||||
api.nvim_feedkeys(cy, 'n', false)
|
api.nvim_feedkeys(cy, 'n', false)
|
||||||
end
|
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.
|
--- 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 bufnr = current_buf()
|
||||||
|
|
||||||
local hunk = get_cursor_hunk(bufnr)
|
local hunk, staged = get_hunk_with_staged(bufnr, true)
|
||||||
|
|
||||||
if not hunk then
|
if not hunk then
|
||||||
return
|
return
|
||||||
|
@ -744,11 +763,11 @@ M.preview_hunk_inline = function()
|
||||||
|
|
||||||
clear_preview_inline(bufnr)
|
clear_preview_inline(bufnr)
|
||||||
|
|
||||||
local winid ---@type integer
|
local winid --- @type integer
|
||||||
manager.show_added(bufnr, ns_inline, hunk)
|
manager.show_added(bufnr, ns_inline, hunk)
|
||||||
if config._inline2 then
|
if config._inline2 then
|
||||||
if hunk.removed.count > 0 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
|
end
|
||||||
else
|
else
|
||||||
manager.show_deleted(bufnr, ns_inline, hunk)
|
manager.show_deleted(bufnr, ns_inline, hunk)
|
||||||
|
@ -771,7 +790,7 @@ M.preview_hunk_inline = function()
|
||||||
if hunk.added.start <= 1 then
|
if hunk.added.start <= 1 then
|
||||||
feedkeys(hunk.removed.count .. '<C-y>')
|
feedkeys(hunk.removed.count .. '<C-y>')
|
||||||
end
|
end
|
||||||
end
|
end)
|
||||||
|
|
||||||
--- Select the hunk under the cursor.
|
--- Select the hunk under the cursor.
|
||||||
M.select_hunk = function()
|
M.select_hunk = function()
|
||||||
|
@ -929,8 +948,8 @@ C.blame_line = function(args, _)
|
||||||
M.blame_line(args)
|
M.blame_line(args)
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param bcache Gitsigns.CacheEntry
|
--- @param bcache Gitsigns.CacheEntry
|
||||||
---@param base string?
|
--- @param base string?
|
||||||
local function update_buf_base(bcache, base)
|
local function update_buf_base(bcache, base)
|
||||||
bcache.base = base
|
bcache.base = base
|
||||||
bcache:invalidate(true)
|
bcache:invalidate(true)
|
||||||
|
@ -1121,14 +1140,14 @@ local function hunks_to_qflist(buf_or_filename, hunks, qflist)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
---@param target 'all'|'attached'|integer|nil
|
--- @param target 'all'|'attached'|integer|nil
|
||||||
---@return table[]?
|
--- @return table[]?
|
||||||
local function buildqflist(target)
|
local function buildqflist(target)
|
||||||
target = target or current_buf()
|
target = target or current_buf()
|
||||||
if target == 0 then
|
if target == 0 then
|
||||||
target = current_buf()
|
target = current_buf()
|
||||||
end
|
end
|
||||||
local qflist = {} ---@type table[]
|
local qflist = {} --- @type table[]
|
||||||
|
|
||||||
if type(target) == 'number' then
|
if type(target) == 'number' then
|
||||||
local bufnr = target
|
local bufnr = target
|
||||||
|
|
|
@ -30,7 +30,6 @@ function M.dump_cache()
|
||||||
--- @type string
|
--- @type string
|
||||||
local text = vim.inspect(cache, { process = process })
|
local text = vim.inspect(cache, { process = process })
|
||||||
vim.api.nvim_echo({ { text } }, false, {})
|
vim.api.nvim_echo({ { text } }, false, {})
|
||||||
return cache
|
|
||||||
end
|
end
|
||||||
|
|
||||||
--- @param noecho boolean
|
--- @param noecho boolean
|
||||||
|
|
|
@ -294,8 +294,9 @@ end
|
||||||
--- @param bufnr integer
|
--- @param bufnr integer
|
||||||
--- @param nsd integer
|
--- @param nsd integer
|
||||||
--- @param hunk Gitsigns.Hunk.Hunk
|
--- @param hunk Gitsigns.Hunk.Hunk
|
||||||
|
--- @param staged boolean?
|
||||||
--- @return integer winid
|
--- @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 cwin = api.nvim_get_current_win()
|
||||||
local virt_lines = {} --- @type {[1]: string, [2]: string}[][]
|
local virt_lines = {} --- @type {[1]: string, [2]: string}[][]
|
||||||
local textoff = vim.fn.getwininfo(cwin)[1].textoff --[[@as integer]]
|
local textoff = vim.fn.getwininfo(cwin)[1].textoff --[[@as integer]]
|
||||||
|
@ -317,7 +318,8 @@ function M.show_deleted_in_float(bufnr, nsd, hunk)
|
||||||
|
|
||||||
local bcache = cache[bufnr]
|
local bcache = cache[bufnr]
|
||||||
local pbufnr = api.nvim_create_buf(false, true)
|
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)
|
local width = api.nvim_win_get_width(0)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue