11/** @jsxImportSource @opentui /solid */
22import type { TuiPlugin , TuiPluginApi , TuiRouteCurrent } from "@opencode-ai/plugin/tui"
33import type { SnapshotFileDiff , VcsFileDiff } from "@opencode-ai/sdk/v2"
4- import { TextAttributes , type BorderSides , type BoxRenderable , type ScrollBoxRenderable } from "@opentui/core"
4+ import {
5+ TextAttributes ,
6+ type BorderSides ,
7+ type BoxRenderable ,
8+ type DiffRenderable ,
9+ type ScrollBoxRenderable ,
10+ } from "@opentui/core"
511import { LANGUAGE_EXTENSIONS } from "@/lsp/language"
612import { useBindings , useCommandShortcut } from "@tui/keymap"
713import { useTheme } from "@tui/context/theme"
@@ -40,6 +46,7 @@ const KV_VIEW = "diff_viewer_view"
4046type DiffMode = "git" | "last-turn"
4147type DiffViewerFocus = "patches" | "files"
4248type DiffView = "split" | "unified"
49+ type SelectedHunk = { readonly fileIndex : number ; readonly hunkIndex : number ; readonly scrollTop : number }
4350
4451type DiffFile = {
4552 readonly file : string
@@ -143,6 +150,8 @@ function DiffViewer(props: { api: TuiPluginApi }) {
143150 const patchFileIndexes = createMemo ( ( ) => orderedPatchFileIndexes ( flattenFileTree ( fileTree ( ) ) ) )
144151 const focusRunner = ( input : Record < DiffViewerFocus , ( ) => void > ) => ( ) => input [ focus ( ) ] ( )
145152 const switchFocusShortcut = useCommandShortcut ( "diff.switch_focus" )
153+ const nextHunkShortcut = useCommandShortcut ( "diff.next_hunk" )
154+ const previousHunkShortcut = useCommandShortcut ( "diff.previous_hunk" )
146155 const nextFileShortcut = useCommandShortcut ( "diff.next_file" )
147156 const previousFileShortcut = useCommandShortcut ( "diff.previous_file" )
148157 const toggleFileTreeShortcut = useCommandShortcut ( "diff.toggle_file_tree" )
@@ -153,6 +162,8 @@ function DiffViewer(props: { api: TuiPluginApi }) {
153162 const helpShortcut = useCommandShortcut ( "diff.help" )
154163 let scroll : ScrollBoxRenderable | undefined
155164 const patchNodeByFileIndex = new Map < number , BoxRenderable > ( )
165+ const diffNodeByFileIndex = new Map < number , DiffRenderable > ( )
166+ const [ selectedHunk , setSelectedHunk ] = createSignal < SelectedHunk | undefined > ( )
156167 const [ pendingPatchScrollFileIndex , setPendingPatchScrollFileIndex ] = createSignal < number | undefined > ( )
157168 const [ patchFillerHeight , setPatchFillerHeight ] = createSignal ( 0 )
158169
@@ -164,6 +175,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
164175 setLastHighlightedFileNode ( undefined )
165176 setActivePatchFileIndex ( undefined )
166177 setSelectedFileIndex ( undefined )
178+ setSelectedHunk ( undefined )
167179 setReviewedFileNames ( new Set < string > ( ) )
168180 } )
169181
@@ -189,6 +201,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
189201 const clearFileTreePatchState = ( ) => {
190202 setHighlightedFileNode ( undefined )
191203 setActivePatchFileIndex ( undefined )
204+ setSelectedHunk ( undefined )
192205 }
193206
194207 const scrollPatchNodeToTop = ( patchNode : BoxRenderable ) => {
@@ -227,6 +240,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
227240
228241 const jumpToFileIndex = ( fileIndex : number | undefined ) => {
229242 if ( fileIndex === undefined ) return
243+ setSelectedHunk ( undefined )
230244 scrollToFileIndex ( fileIndex )
231245 }
232246
@@ -248,6 +262,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
248262 }
249263
250264 const jumpRelativePatchFile = ( offset : number ) => {
265+ setSelectedHunk ( undefined )
251266 const next = movePatchFileIndex ( patchFileIndexes ( ) , selectedFileIndex ( ) ?? activePatchFileIndex ( ) , offset )
252267 if ( singlePatch ( ) ) {
253268 if ( next === undefined ) return
@@ -258,6 +273,38 @@ function DiffViewer(props: { api: TuiPluginApi }) {
258273 scrollToFileIndex ( next )
259274 }
260275
276+ const jumpRelativeHunk = ( offset : - 1 | 1 ) => {
277+ const patchScroll = scroll
278+ if ( ! patchScroll ) return
279+ const hunks = visiblePatchFiles ( )
280+ . flatMap ( ( entry ) => {
281+ const node = diffNodeByFileIndex . get ( entry . fileIndex )
282+ if ( ! node || node . isDestroyed ) return [ ]
283+ const contentY = patchScroll . scrollTop + node . y - patchScroll . viewport . y
284+ return node . getHunkRowOffsets ( ) . map ( ( row , hunkIndex ) => ( {
285+ fileIndex : entry . fileIndex ,
286+ hunkIndex,
287+ contentY : contentY + row ,
288+ } ) )
289+ } )
290+ . sort ( ( left , right ) => left . contentY - right . contentY )
291+ const selected = selectedHunk ( )
292+ const selectedIndex =
293+ selected ?. scrollTop === patchScroll . scrollTop
294+ ? hunks . findIndex ( ( hunk ) => hunk . fileIndex === selected . fileIndex && hunk . hunkIndex === selected . hunkIndex )
295+ : - 1
296+ const next =
297+ selectedIndex !== - 1
298+ ? hunks [ selectedIndex + offset ]
299+ : offset === 1
300+ ? hunks . find ( ( hunk ) => hunk . contentY > patchScroll . scrollTop )
301+ : hunks . findLast ( ( hunk ) => hunk . contentY < patchScroll . scrollTop )
302+ if ( ! next ) return
303+ selectPatchFile ( next . fileIndex )
304+ patchScroll . scrollTo ( next . contentY )
305+ setSelectedHunk ( { fileIndex : next . fileIndex , hunkIndex : next . hunkIndex , scrollTop : patchScroll . scrollTop } )
306+ }
307+
261308 const highlightedPatchFileIndex = ( ) => fileRows ( ) . find ( ( row ) => row . id === highlightedFileNode ( ) ) ?. fileIndex
262309 const firstPatchFileIndex = ( ) => fileRows ( ) . find ( ( row ) => row . fileIndex !== undefined ) ?. fileIndex
263310 const visiblePatchFiles = createMemo ( ( ) => {
@@ -504,6 +551,22 @@ function DiffViewer(props: { api: TuiPluginApi }) {
504551 patches ( ) { } ,
505552 } ) ,
506553 } ,
554+ {
555+ name : "diff.next_hunk" ,
556+ title : "Jump to next diff hunk" ,
557+ category : "VCS" ,
558+ run ( ) {
559+ jumpRelativeHunk ( 1 )
560+ } ,
561+ } ,
562+ {
563+ name : "diff.previous_hunk" ,
564+ title : "Jump to previous diff hunk" ,
565+ category : "VCS" ,
566+ run ( ) {
567+ jumpRelativeHunk ( - 1 )
568+ } ,
569+ } ,
507570 {
508571 name : "diff.next_file" ,
509572 title : "Jump to next diff file" ,
@@ -557,6 +620,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
557620 title : "Toggle single patch view" ,
558621 category : "VCS" ,
559622 run ( ) {
623+ setSelectedHunk ( undefined )
560624 if ( ! singlePatch ( ) ) {
561625 ensureHighlightedPatchFile ( )
562626 setSinglePatch ( true )
@@ -592,6 +656,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
592656 category : "VCS" ,
593657 run ( ) {
594658 if ( ! splitAvailable ( ) ) return
659+ setSelectedHunk ( undefined )
595660 const next = view ( ) === "split" ? "unified" : "split"
596661 setViewOverride ( next )
597662 props . api . kv . set ( KV_VIEW , next )
@@ -716,6 +781,7 @@ function DiffViewer(props: { api: TuiPluginApi }) {
716781 < Panel flexGrow = { 1 } minHeight = { 0 } border = "none" >
717782 < Separator axis = "x" start = { showFileTree ( ) ? "edge-out" : undefined } />
718783 < scrollbox
784+ id = "diff-viewer-patches"
719785 ref = { ( element : ScrollBoxRenderable ) => ( scroll = element ) }
720786 flexGrow = { 1 }
721787 minHeight = { 0 }
@@ -755,6 +821,8 @@ function DiffViewer(props: { api: TuiPluginApi }) {
755821 { ( patch ) => (
756822 < box border = { patchLeftBorder ( ) } borderColor = { theme ( ) . border } >
757823 < diff
824+ id = { `diff-viewer-patch-${ entry . fileIndex } ` }
825+ ref = { ( element : DiffRenderable ) => diffNodeByFileIndex . set ( entry . fileIndex , element ) }
758826 diff = { patch ( ) }
759827 view = { view ( ) }
760828 filetype = { reviewed ( ) ? PLAIN_TEXT_FILETYPE : filetype ( entry . file . file ) }
@@ -808,6 +876,20 @@ function DiffViewer(props: { api: TuiPluginApi }) {
808876 </ text >
809877 ) }
810878 </ Show >
879+ < Show when = { nextHunkShortcut ( ) } >
880+ { ( shortcut ) => (
881+ < text fg = { theme ( ) . text } >
882+ { shortcut ( ) } < span style = { { fg : theme ( ) . textMuted } } > next hunk</ span >
883+ </ text >
884+ ) }
885+ </ Show >
886+ < Show when = { previousHunkShortcut ( ) } >
887+ { ( shortcut ) => (
888+ < text fg = { theme ( ) . text } >
889+ { shortcut ( ) } < span style = { { fg : theme ( ) . textMuted } } > previous hunk</ span >
890+ </ text >
891+ ) }
892+ </ Show >
811893 < Show when = { previousFileShortcut ( ) } >
812894 { ( shortcut ) => (
813895 < text fg = { theme ( ) . text } >
@@ -855,6 +937,16 @@ function DiffViewerHelpDialog() {
855937 action : "Focus file tree" ,
856938 description : "Move keyboard focus between the file tree and patch pane" ,
857939 } ,
940+ {
941+ shortcut : useCommandShortcut ( "diff.next_hunk" ) ,
942+ action : "Next hunk" ,
943+ description : "Jump to the next diff hunk" ,
944+ } ,
945+ {
946+ shortcut : useCommandShortcut ( "diff.previous_hunk" ) ,
947+ action : "Previous hunk" ,
948+ description : "Jump to the previous diff hunk" ,
949+ } ,
858950 {
859951 shortcut : useCommandShortcut ( "diff.next_file" ) ,
860952 action : "Next file" ,
0 commit comments