Skip to content

Commit

Permalink
feat: staged signs support for preview and nav
Browse files Browse the repository at this point in the history
  • Loading branch information
lewis6991 committed Dec 16, 2022
1 parent 947811c commit 4010092
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 24 deletions.
24 changes: 17 additions & 7 deletions lua/gitsigns/actions.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 7 additions & 4 deletions lua/gitsigns/hunks.lua

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

24 changes: 17 additions & 7 deletions teal/gitsigns/actions.tl
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,12 @@ end

local function get_cursor_hunk(bufnr: integer, hunks: {Hunk}): Hunk, integer
bufnr = bufnr or current_buf()
hunks = hunks or cache[bufnr].hunks

if not hunks then
hunks = {}
vim.list_extend(hunks, cache[bufnr].hunks or {})
vim.list_extend(hunks, cache[bufnr].hunks_staged or {})
end

local lnum = api.nvim_win_get_cursor(0)[1]
return gs_hunks.find_hunk(lnum, hunks)
Expand Down Expand Up @@ -530,7 +535,9 @@ local nav_hunk = void(function(opts: NavHunkOpts)
return
end

local hunks = get_hunks(bufnr, bcache, opts.greedy)
local hunks: {Hunk} = {}
vim.list_extend(hunks, get_hunks(bufnr, bcache, opts.greedy, false) or {})
vim.list_extend(hunks, get_hunks(bufnr, bcache, opts.greedy, true) or {})

if not hunks or vim.tbl_isempty(hunks) then
if opts.navigation_message then
Expand Down Expand Up @@ -714,7 +721,11 @@ M.preview_hunk = noautocmd(function()
return
end

local hunk, index = get_cursor_hunk(bufnr, bcache.hunks)
local hunks: {Hunk} = {}
vim.list_extend(hunks, bcache.hunks or {})
vim.list_extend(hunks, bcache.hunks_staged or {})

local hunk, index = get_cursor_hunk(bufnr, hunks)

if not hunk then return end

Expand All @@ -736,14 +747,13 @@ end)

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

local hunk = get_cursor_hunk(bufnr)
local hunk = get_cursor_hunk()

if not hunk then
return
end

local bufnr = current_buf()
manager.show_added(bufnr, ns_inline, hunk)
manager.show_deleted(bufnr, ns_inline, hunk)

Expand Down Expand Up @@ -1223,7 +1233,7 @@ M.get_actions = function(): {string:function}
if not bcache then
return
end
local hunk = get_cursor_hunk(bufnr, bcache.hunks)
local hunk = get_cursor_hunk()

local actions_l: {string} = {}

Expand Down
15 changes: 9 additions & 6 deletions teal/gitsigns/hunks.tl
Original file line number Diff line number Diff line change
Expand Up @@ -248,7 +248,7 @@ function M.get_summary(hunks: {Hunk}): StatusObj
return status
end

function M.find_hunk(lnum: number, hunks: {Hunk}): Hunk, integer
function M.find_hunk(lnum: integer, hunks: {Hunk}): Hunk, integer
for i, hunk in ipairs(hunks or {}) do
if lnum == 1 and hunk.added.start == 0 and hunk.vend == 0 then
return hunk, i
Expand All @@ -260,25 +260,28 @@ function M.find_hunk(lnum: number, hunks: {Hunk}): Hunk, integer
end
end

function M.find_nearest_hunk(lnum: number, hunks: {Hunk}, forwards: boolean, wrap: boolean): Hunk, integer
function M.find_nearest_hunk(lnum: integer, hunks: {Hunk}, forwards: boolean, wrap: boolean): Hunk, integer
local ret: Hunk
local index: integer
local distance: integer = math.huge as integer
if forwards then
for i = 1, #hunks do
local hunk = hunks[i]
if hunk.added.start > lnum then
local dist = hunk.added.start - lnum
if dist > 0 and dist < distance then
distance = dist
ret = hunk
index = i
break
end
end
else
for i = #hunks, 1, -1 do
local hunk = hunks[i]
if hunk.vend < lnum then
local dist = lnum - hunk.vend
if dist > 0 and dist < distance then
distance = dist
ret = hunk
index = i
break
end
end
end
Expand Down

0 comments on commit 4010092

Please sign in to comment.