Skip to content

fix(preview): make preview_hunk and preview_hunk_inline show consistent diff - #1480

Open
letstakeawalk wants to merge 4 commits into
lewis6991:mainfrom
letstakeawalk:fix/preview
Open

fix(preview): make preview_hunk and preview_hunk_inline show consistent diff#1480
letstakeawalk wants to merge 4 commits into
lewis6991:mainfrom
letstakeawalk:fix/preview

Conversation

@letstakeawalk

Copy link
Copy Markdown

Summary

preview_hunk was the only hunk command that did not support greedy. When navigating with nav_hunk (which defaults to greedy=true) and diff_opts.linematch enabled, preview_hunk shows linematch-split hunks individually while preview_hunk_inline correctly shows the merged diff. This causes inconsistent preview behavior between the two commands.

Steps to reproduce

Given a file with the following content committed:

- [ ] lorem ipsum
- [ ] add line below and change me
- [ ] change me
- [ ] lorem ipsum
- [ ] lorem ipsum
- [ ] delete me
- [ ] change me
- [ ] delete me
- [ ] change me
- [ ] lorem ipsum

Then changed to:

- [ ] lorem ipsum
- [x] add line below and change me
- [ ] new line
- [x] change me
- [ ] lorem ipsum
- [ ] lorem ipsum
- [x] change me
- [x] change me
- [ ] lorem ipsum

Before

  1. Cursor at line 1
  2. nav_hunk("next") → cursor at line 2
  3. preview_hunk()
    gitsigns-preview-before-1
    Note: preview title shows Hunk 1 of 7
  4. preview_hunk_inline()
    gitsigns-preview-inline-1
    Note the difference
  5. nav_hunk("next") → cursor at line 7
  6. preview_hunk()
gitsigns-preview-before-2 Note: preview title shows `Hunk 5 of 7` 7. `preview_hunk_inline()` gitsigns-preview-inline-2 Note the difference

preview_hunk_inline shows the diff as expected according to greedy nav_hunk, but preview_hunk only shows the individual linematch-split hunk.

After (with this PR)

preview_hunk now shows the same merged result as preview_hunk_inline, with the correct greedy hunk count in the title (Hunk 1 of 2 and Hunk 2 of 2).

gitsigns-preview-after-1 gitsigns-preview-after-2

Changes

  • Refactor get_hunk_with_staged to return hunk index and count
  • Add greedy option to preview_hunk — make it async, use get_hunk_with_staged
    and Util.noautocmd
  • Add greedy option to preview_hunk_inline — parameterize the hardcoded true
  • Pass greedy from nav_hunk to both preview functions
  • Update generated docs

- Accept greedy parameter instead of hardcoding true
- Use `get_hunks` and `get_cursor_hunk` directly instead of `get_hunk`
- Return hunk index and total count for use by callers
- Make `preview_hunk` async to support greedy diff re-computation
- Use `get_hunk_with_staged` for consistent hunk resolution
- Replace local `noautocmd` wrapper with `Util.noautocmd`
- Add `opts` and `callback` parameter docs for `preview_hunk`
- Add `opts` parameter docs for `preview_hunk_inline`
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant