gitsigns.nvim icon indicating copy to clipboard operation
gitsigns.nvim copied to clipboard

Fallback option for navigation method

Open sassanh opened this issue 1 year ago • 5 comments

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.

sassanh avatar Mar 10 '23 16:03 sassanh

I think this as an option is pretty obscure. You're better off setting wrap=false and detecting if a move was done.

lewis6991 avatar Mar 10 '23 16:03 lewis6991

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.

sassanh avatar Mar 10 '23 16:03 sassanh

I do plan to add callback arguments to all actions. To begin with we can add a callback that passes no arguments?

lewis6991 avatar Mar 10 '23 21:03 lewis6991

That would be enough for me, let me know if I can help :-)

sassanh avatar Mar 10 '23 22:03 sassanh

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

Jaakkko avatar Jun 11 '23 06:06 Jaakkko

@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.

sassanh avatar Apr 04 '24 10:04 sassanh