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

Fallback option for navigation method #766

Closed
sassanh opened this issue Mar 10, 2023 · 6 comments · Fixed by #967
Closed

Fallback option for navigation method #766

sassanh opened this issue Mar 10, 2023 · 6 comments · Fixed by #967
Labels
enhancement New feature or request

Comments

@sassanh
Copy link

sassanh commented Mar 10, 2023

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 sassanh added the enhancement New feature or request label Mar 10, 2023
@lewis6991
Copy link
Owner

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

@lewis6991 lewis6991 closed this as not planned Won't fix, can't repro, duplicate, stale Mar 10, 2023
@sassanh
Copy link
Author

sassanh commented Mar 10, 2023

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.

@lewis6991
Copy link
Owner

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

@sassanh
Copy link
Author

sassanh commented Mar 10, 2023

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

@lewis6991 lewis6991 reopened this Mar 10, 2023
@Jaakkko
Copy link

Jaakkko commented Jun 11, 2023

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 added a commit that referenced this issue Apr 3, 2024
@lewis6991 lewis6991 linked a pull request Apr 3, 2024 that will close this issue
lewis6991 added a commit that referenced this issue Apr 3, 2024
lewis6991 added a commit that referenced this issue Apr 3, 2024
@sassanh
Copy link
Author

sassanh commented Apr 4, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants