Skip to content

Commit

Permalink
feat(nav): add target option
Browse files Browse the repository at this point in the history
Allows `nav_hunk` to target staged, unstaged or all hunks.
  • Loading branch information
lewis6991 committed Jun 19, 2024
1 parent 7516bac commit 9291836
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 5 deletions.
2 changes: 2 additions & 0 deletions doc/gitsigns.txt
Original file line number Diff line number Diff line change
Expand Up @@ -403,6 +403,8 @@ nav_hunk({direction}, {opts}, {callback?}) *gitsigns.nav_hunk()*
{greedy}: (boolean)
Only navigate between non-contiguous hunks. Only useful if
'diff_opts' contains `linematch`. Defaults to `true`.
{target}: (`'unstaged'|'staged'|'all'`)
Which kinds of hunks to target. Defaults to `'unstaged'`.
{count}: (integer)
Number of times to advance. Defaults to |v:count1|.

Expand Down
39 changes: 34 additions & 5 deletions lua/gitsigns/actions.lua
Original file line number Diff line number Diff line change
Expand Up @@ -215,10 +215,10 @@ local function get_hunks(bufnr, bcache, greedy, staged)
end

if staged then
return vim.deepcopy(bcache.hunks_staged, true)
return vim.deepcopy(bcache.hunks_staged)
end

return vim.deepcopy(bcache.hunks, true)
return vim.deepcopy(bcache.hunks)
end

--- @param bufnr integer
Expand Down Expand Up @@ -478,6 +478,7 @@ end)
--- @field greedy boolean
--- @field preview boolean
--- @field count integer
--- @field target 'unstaged'|'staged'|'all'

--- @param x string
--- @param word string
Expand Down Expand Up @@ -513,6 +514,10 @@ local function process_nav_opts(opts)
opts.count = vim.v.count1
end

if opts.target == nil then
opts.target = 'unstaged'
end

return opts
end

Expand All @@ -532,6 +537,30 @@ local function has_preview_inline(bufnr)
return #api.nvim_buf_get_extmarks(bufnr, ns_inline, 0, -1, { limit = 1 }) > 0
end

--- @param bufnr integer
--- @param target 'unstaged'|'staged'|'all'
--- @param greedy boolean
--- @return Gitsigns.Hunk.Hunk[]
local function get_nav_hunks(bufnr, target, greedy)
local bcache = assert(cache[bufnr])
local hunks_main = get_hunks(bufnr, bcache, greedy, false) or {}

local hunks --- @type Gitsigns.Hunk.Hunk[]
if target == 'unstaged' then
hunks = hunks_main
else
local hunks_head = get_hunks(bufnr, bcache, greedy, true) or {}
hunks_head = Hunks.filter_common(hunks_head, hunks_main) or {}
if target == 'all' then
hunks = hunks_main
vim.list_extend(hunks, hunks_head)
elseif target == 'staged' then
hunks = hunks_head
end
end
return hunks
end

--- @async
--- @param direction 'first'|'last'|'next'|'prev'
--- @param opts? Gitsigns.NavOpts
Expand All @@ -543,9 +572,7 @@ local function nav_hunk(direction, opts)
return
end

local hunks = get_hunks(bufnr, bcache, opts.greedy, false) or {}
local hunks_head = get_hunks(bufnr, bcache, opts.greedy, true) or {}
vim.list_extend(hunks, Hunks.filter_common(hunks_head, hunks) or {})
local hunks = get_nav_hunks(bufnr, opts.target, opts.greedy)

if not hunks or vim.tbl_isempty(hunks) then
if opts.navigation_message then
Expand Down Expand Up @@ -629,6 +656,8 @@ end
--- • {greedy}: (boolean)
--- Only navigate between non-contiguous hunks. Only useful if
--- 'diff_opts' contains `linematch`. Defaults to `true`.
--- • {target}: (`'unstaged'|'staged'|'all'`)
--- Which kinds of hunks to target. Defaults to `'unstaged'`.
--- • {count}: (integer)
--- Number of times to advance. Defaults to |v:count1|.
M.nav_hunk = async.create(2, function(direction, opts)
Expand Down

3 comments on commit 9291836

@w-tr
Copy link

@w-tr w-tr commented on 9291836 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know how to trigger this ...

I tried
:Gitsigns nav_hunk('next')
:Gitsigns nav_hunk('next', {targeted} = 'staged')

When i just do
:Gitsigns nav_hunk It says no more hunks

Am i making a fundamental error in thinking this is available in the nvim command line in progress options?

@lewis6991
Copy link
Owner Author

@lewis6991 lewis6991 commented on 9291836 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

:Gitsigns nav_hunk next target=staged

or

:lua require('gitsigns').nav_huk('next', { target = 'staged' })

@w-tr
Copy link

@w-tr w-tr commented on 9291836 Jul 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the key here is removing the brackets and the quotations when in commandlinemode

Please sign in to comment.