gitsigns.nvim
gitsigns.nvim copied to clipboard
Fallback option for navigation method
Is your feature request related to a problem? Please describe.
I implemented a shortcut to automatically go to the next/previous file when navigating hunks and it reaches end/beginning of the file. To implement this I needed gitsigns to somehow inform me when next_hunk
/prev_hunk
failed. I read the code to find something but I didn't find anything.
Describe the solution you'd like I patched gitsigns.nvim like this:
diff --git a/lua/gitsigns/actions.lua b/lua/gitsigns/actions.lua
index 11e3cb3..536545f 100644
--- a/lua/gitsigns/actions.lua
+++ b/lua/gitsigns/actions.lua
@@ -544,6 +544,9 @@ local nav_hunk = void(function(opts)
if opts.navigation_message then
api.nvim_echo({ { 'No hunks', 'WarningMsg' } }, false, {})
end
+ if opts.fallback then
+ opts.fallback()
+ end
return
end
local line = api.nvim_win_get_cursor(0)[1]
@@ -554,6 +557,9 @@ local nav_hunk = void(function(opts)
if opts.navigation_message then
api.nvim_echo({ { 'No more hunks', 'WarningMsg' } }, false, {})
end
+ if opts.fallback then
+ opts.fallback()
+ end
return
end
Describe alternatives you've considered
I tried hacky solutions like overriding nvim_echo
, or monitoring nvim-notify, etc. But none of them were stable.
Additional context I think my diff is a minimal solution just to resolve my own issue, so I thought I share it here and ask a maintainer who knows the codebase better to kindly add a more sophisticated mechanism to resolve this issue.
I think this as an option is pretty obscure. You're better off setting wrap=false
and detecting if a move was done.
I think this as an option is pretty obscure. You're better off setting
wrap=false
and detecting if a move was done.
Sounds like a hacky solution to me. Specially considering you are using async functions, I may need to use timeouts to make sure no move has happened for a few milliseconds.
I do plan to add callback arguments to all actions. To begin with we can add a callback that passes no arguments?
That would be enough for me, let me know if I can help :-)
I do plan to add callback arguments to all actions. To begin with we can add a callback that passes no arguments?
I can help
@lewis6991 Thanks! Now with this commit I can use the upstream gitsigns.nvim without patching it, now I'm using this callback like this:
local pos = vim.api.nvim_win_get_cursor(0)
gitsigns.next_hunk({
wrap = false,
async = true,
}, function()
local new_pos = vim.api.nvim_win_get_cursor(0)
if new_pos[1] == pos[1] and new_pos[2] == pos[2] then
...
end
end)
I'm checking if it was successful or not by checking if the cursor position has changed, it would be nice if it could report it in the callback.