From 4010092d31cb673370d80c43b116ad1e73b4d38f Mon Sep 17 00:00:00 2001 From: Lewis Russell Date: Fri, 16 Dec 2022 12:56:23 +0000 Subject: [PATCH] feat: staged signs support for preview and nav --- lua/gitsigns/actions.lua | 24 +++++++++++++++++------- lua/gitsigns/hunks.lua | 11 +++++++---- teal/gitsigns/actions.tl | 24 +++++++++++++++++------- teal/gitsigns/hunks.tl | 15 +++++++++------ 4 files changed, 50 insertions(+), 24 deletions(-) diff --git a/lua/gitsigns/actions.lua b/lua/gitsigns/actions.lua index 4bf95ad0..ce93bf1a 100644 --- a/lua/gitsigns/actions.lua +++ b/lua/gitsigns/actions.lua @@ -222,7 +222,12 @@ end local function get_cursor_hunk(bufnr, hunks) 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) @@ -530,7 +535,9 @@ local nav_hunk = void(function(opts) return end - local hunks = get_hunks(bufnr, bcache, opts.greedy) + local hunks = {} + 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 @@ -714,7 +721,11 @@ M.preview_hunk = noautocmd(function() return end - local hunk, index = get_cursor_hunk(bufnr, bcache.hunks) + local hunks = {} + 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 @@ -736,14 +747,13 @@ end) 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) @@ -1223,7 +1233,7 @@ M.get_actions = function() if not bcache then return end - local hunk = get_cursor_hunk(bufnr, bcache.hunks) + local hunk = get_cursor_hunk() local actions_l = {} diff --git a/lua/gitsigns/hunks.lua b/lua/gitsigns/hunks.lua index 56c56fe4..8ebf0d5f 100644 --- a/lua/gitsigns/hunks.lua +++ b/lua/gitsigns/hunks.lua @@ -263,22 +263,25 @@ end function M.find_nearest_hunk(lnum, hunks, forwards, wrap) local ret local index + local distance = math.huge 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 diff --git a/teal/gitsigns/actions.tl b/teal/gitsigns/actions.tl index 696cff74..180bf74b 100644 --- a/teal/gitsigns/actions.tl +++ b/teal/gitsigns/actions.tl @@ -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) @@ -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 @@ -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 @@ -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) @@ -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} = {} diff --git a/teal/gitsigns/hunks.tl b/teal/gitsigns/hunks.tl index 9a3742a1..ff8f1673 100644 --- a/teal/gitsigns/hunks.tl +++ b/teal/gitsigns/hunks.tl @@ -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 @@ -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