Skip to content

feat: jump to the next / previous hunk with ] and [ - #174

Draft
pablospe wants to merge 29 commits into
dlvhdr:mainfrom
pablospe:hunk-nav
Draft

pablospe wants to merge 29 commits into
dlvhdr:mainfrom
pablospe:hunk-nav

Conversation

@pablospe

@pablospe pablospe commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Part 3 of the OSC 1717 series started in #168. Stacked on #168 (OSC 1717 line metadata): it uses the records that PR parses, so it should be reviewed and merged after it. Until then the diff here includes #168's commits; the last thirteen are this PR. Only this PR's changes: pablospe/diffnav@osc1717...hunk-nav

Closes #176.

] scrolls the next hunk to the top of the diff, [ the previous one (or the start of the current hunk when you are inside it, like vim's [[). In selection mode they move the selection instead. At the last / first hunk they stay put. They work in a directory view across files, and are ignored while searching or filtering, like n/p.

  • Finding hunks: delta tags every hunk-header row with an h record, including the decoration rows around it (a boxed or over/underlined header is three rows with the same h), so a hunk starts at the first row of a run of the same h. But delta prints no header at all for a hunk without a code fragment (nothing after @@) when the hunk-header style shows no line number or file. diffnav's own style is like that, so in themed mode many hunks have no h record. Fixed in delta main (Emit the h record when the hunk header row is skipped dandavison/delta#2334: the h record goes on the blank row above the hunk), so the fallback is only for 0.20.x releases. Checked with a delta built from main: every hunk then starts on an h row and the fallback adds nothing. For those, a hunk starts at the first c/a/d row after a file header, where the new line number jumps by more than one, or where a deleted line skips an old line (adjacent -U0 hunks). Wrapped rows count once. Still missed: an addition-only hunk right before a deletion hunk with -U0 and no header, since added lines carry no old line.
  • After s or a resize: the keys work on the lines on screen, so a jump made before the re-render arrives lands on that hunk in the new render instead of being undone (like feat: keep the scroll position when toggling side-by-side or resizing #168 does for scrolling).
  • Without records (delta < 0.20), the keys do nothing.
  • The keys are in the help and the README.

Known limit: near the end of a diff the viewport can't scroll the last hunks to the top, so ] shows no change there. The next ] or [ still counts from the hunk it went to, so [ steps back through those hunks instead of skipping them.

Possible follow-ups: ] at the last hunk of a file could move on to the next file, and in a directory view the file tree selection doesn't follow yet when ]/[ crosses into another file.

How Did You Test this Change?

  • Unit tests: hunk starts from records (a three-row header, the same line in two files, hunks without a header, a new file without a header, a one-line gap in old lines, a wrapped line), next / previous at the edges and near the end of the diff, no records, scrolling vs. selection mode, ignored while showing matching lines only, and a jump during a pending side-by-side render. One test runs the installed delta on a real diff (-U0, -U1, -U3, unified and side-by-side, 80 and 200 columns) and checks that every hunk is found, also with every h record removed.
  • go test ./..., go vet ./... and golangci-lint v2.12.2 pass.
  • Checked the hunk detection against delta 0.20.1 on a real 120-commit diff of this repo (80 hunks with the default context, 108 with -U0), with hunk-header-style set to diffnav's color-only style, omit, line-number and file line-number syntax, unified and side-by-side, at 50 and 200 columns: every @@ is found exactly once, also with every h record removed, and also for two -U0 deletions one line apart.
  • Manually with delta 0.20.1, in themed mode and ui.useUserDeltaTheme (a boxed file line-number header), unified and side-by-side, and in selection mode. With delta 0.18.2 the keys do nothing.

Images/Videos

Delta built from main, diffnav's own theme, a directory view with three files: main.go (a headerless hunk at the top of the file, then two hunks with a func header) and scores.csv (three headerless hunks, which land on delta's blank row carrying the h record), with weights.csv below so those hunks can reach the top. In side-by-side: ] six times through all of them, [ twice; then s to switch to unified (the position is kept), ] twice and [ once.

PR_hunk_nav

AI disclosure

Claude Code was used to write the code and tests. I reviewed the changes and tested them manually.

Set OSC1717=V1 for delta and, when its output starts with the handshake,
attach each row's line records to the diff line and strip them from the
text. Older delta ignores the variable and renders as before.
Delta writes the handshake in front of its first row, not on a row of its own.
Remember the diff line at the top of the view (or the selected one) and, once the re-rendered diff is shown, scroll back to the row with the same OSC 1717 record. Keeping the row index drifted, since unified and side-by-side lay out a different number of rows. Without records the index is kept as before.
Resizing first let the viewport clamp its top row to the old row count, so a taller pane near the bottom moved up. Also skip the anchor when no content row is visible.
Remember the top row's offset from the first row of its anchor record, so a preamble, delta's decoration rows and wrapped rows stay where they were instead of snapping to the record.
Re-renders show the same patch, so the exact record is always there.
When the new layout wraps the anchor line over fewer rows, the offset spilled onto the next diff line. Rows above the anchor keep their offset.
A scroll or jump made before the re-render arrives is no longer undone by the anchor taken at the s press or resize.
ScrollUp/Down/Top/Bottom dropped the pending anchor even at the edge of the diff, where they don't move. They now share Update's check.
Scrolling right after s or a resize kept the row index on the new layout, so a relative move landed on another line. The viewer now keeps the lines on screen and re-anchors to the line the move lands on; g and G still go to the top and bottom. currentRecord gives the record of the current row from those lines.
Git quotes non-ASCII names with octal escapes and delta copies them into the record, so a.txt with an umlaut came through as \303\244.txt.
Near the end of the diff the view stops short of a hunk, so the next ] aimed at the same hunk again and [ skipped back past it. The next jump now counts from the hunk while the view still shows the same top row.
A hunk start is the first row of its record, so the offset was 0, or wrong when the same line appears earlier in the diff.
Delta prints records that hand-built rows don't: deleted lines with a fixed new line under -U0, and deleted and added lines on one side-by-side row.
@github-actions

This comment has been minimized.

@pablospe
pablospe marked this pull request as draft November 26, 2026 12:37
@pablospe

Copy link
Copy Markdown
Contributor Author

Marked as draft because it depends on #168.
Only this PR's changes: pablospe/diffnav@osc1717...hunk-nav

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.

Jump to the next / previous hunk

1 participant