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

feat: staged signs support for preview and nav #695

Merged
merged 1 commit into from
Dec 16, 2022
Merged
Show file tree
Hide file tree
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
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