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

Ability to jump to the first and the last hunk #800

Closed
dtcyganok opened this issue May 25, 2023 · 7 comments
Closed

Ability to jump to the first and the last hunk #800

dtcyganok opened this issue May 25, 2023 · 7 comments
Labels
enhancement New feature or request

Comments

@dtcyganok
Copy link

Is your feature request related to a problem? Please describe.
It's pretty common to have the comprehensive navigation through a set of items. Usually it includes:

  • prev item
  • next item
  • first item
  • last item

Many plugins' authors follow that pattern. For instance unimpaired.vim and ale.

Describe the solution you'd like
My suggestion is to add the api functions first_hunk and last_hunk.

Describe alternatives you've considered
To write my own functions first_hunk and last_hunk.

@dtcyganok dtcyganok added the enhancement New feature or request label May 25, 2023
@lewis6991
Copy link
Owner

I would do gg]c for the first hunk and G[c for the last

@dtcyganok
Copy link
Author

Thanks, i already tried that. And it doesn't work correctly, if the first hunk starts from the first line. Similarly, the situation with the last hunk is the same

@dtcyganok
Copy link
Author

dtcyganok commented May 25, 2023

I wrote those functions, but i'm not sure that's correct:

local gitsigns = require('gitsigns')

local function opts()
  return {
    buffer = bufnr,
    unique = true,
  }
end

vim.keymap.set('n', '[G', function()
  local hunks = gitsigns.get_hunks()
  local hunk = hunks[1]
  local line = math.min(hunk.added.start, hunk.removed.start)
  vim.api.nvim_win_set_cursor(0, { line, 0 })
end, opts())

vim.keymap.set('n', ']G', function()
  local hunks = gitsigns.get_hunks()
  local hunk = hunks[#hunks]
  local line = math.max(hunk.added.start, hunk.removed.start)
  vim.api.nvim_win_set_cursor(0, { line, 0 })
end, opts())

@lewis6991
Copy link
Owner

lewis6991 commented May 25, 2023

It's close. The buffer indices are in hunk.added. hunk.removed is what you're comparing too.

Try hunk.added.start + hunk.added.count for the end of the hunk.

@ribru17
Copy link

ribru17 commented Jun 5, 2023

What about G]c for first hunk and gg[c for last hunk? I tested this and it works when hunks are at the first and last lines thanks to the wrap-around functionality. For the last hunk if you want your cursor to be at the beginning of the changes then it would be gg[c[c]c.

@dtcyganok
Copy link
Author

@ribru17 gg[c doesn't work properly if the first line has been changed, as i mentioned before. There is the bug with wrap option. I've already created the related issue

@lewis6991
Copy link
Owner

Done in #976

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

No branches or pull requests

3 participants