@@ -101,7 +101,7 @@ function CollapsedRun({ count, onExpand }: CollapsedRunProps) {
101101}
102102
103103/** Splits a hunk into visible lines and collapsed runs of unchanged lines. */
104- function visibleItems ( lines : DiffLine [ ] , expanded : boolean ) {
104+ function visibleItems ( lines : DiffLine [ ] , isExpanded : ( start : number ) => boolean ) {
105105 const items : Array < { line : DiffLine } | { collapsed : number ; key : number } > = [ ]
106106 for ( let index = 0 ; index < lines . length ; ) {
107107 if ( lines [ index ] . type !== 'context' ) {
@@ -115,7 +115,11 @@ function visibleItems(lines: DiffLine[], expanded: boolean) {
115115 const trailing = index === lines . length
116116 const keepBefore = leading ? 0 : CONTEXT_EDGE
117117 const keepAfter = trailing ? 0 : CONTEXT_EDGE
118- if ( expanded || run . length <= COLLAPSE_AFTER || run . length <= keepBefore + keepAfter + 1 ) {
118+ if (
119+ isExpanded ( start ) ||
120+ run . length <= COLLAPSE_AFTER ||
121+ run . length <= keepBefore + keepAfter + 1
122+ ) {
119123 for ( const line of run ) items . push ( { line } )
120124 continue
121125 }
@@ -147,7 +151,8 @@ function marker(line: DiffLine | undefined) {
147151 * when the hunk headers carry them.
148152 */
149153export function DiffView ( { hunks } : DiffViewProps ) {
150- const [ expanded , setExpanded ] = useState < ReadonlySet < number > > ( ( ) => new Set ( ) )
154+ /** Expanded runs, keyed `hunk:start` so each collapsed run opens on its own. */
155+ const [ expanded , setExpanded ] = useState < ReadonlySet < string > > ( ( ) => new Set ( ) )
151156 const numbered = hunks . some ( ( hunk ) =>
152157 hunk . lines . some ( ( line ) => line . oldLine !== undefined || line . newLine !== undefined )
153158 )
@@ -157,8 +162,7 @@ export function DiffView({ hunks }: DiffViewProps) {
157162 < div className = { cn ( 'grid w-max min-w-full font-mono text-caption leading-[1.6]' , columns ) } >
158163 { hunks . map ( ( hunk , hunkIndex ) => {
159164 const segments = wordSegments ( hunk . lines )
160- const items = visibleItems ( hunk . lines , expanded . has ( hunkIndex ) )
161- const expand = ( ) => setExpanded ( ( current ) => new Set ( current ) . add ( hunkIndex ) )
165+ const items = visibleItems ( hunk . lines , ( start ) => expanded . has ( `${ hunkIndex } :${ start } ` ) )
162166 return (
163167 < Fragment key = { hunkIndex } >
164168 { ( hunk . file || hunk . heading || hunkIndex > 0 ) && (
@@ -181,7 +185,13 @@ export function DiffView({ hunks }: DiffViewProps) {
181185 </ span >
182186 </ div >
183187 ) : (
184- < CollapsedRun key = { `c${ item . key } ` } count = { item . collapsed } onExpand = { expand } />
188+ < CollapsedRun
189+ key = { `c${ item . key } ` }
190+ count = { item . collapsed }
191+ onExpand = { ( ) =>
192+ setExpanded ( ( current ) => new Set ( current ) . add ( `${ hunkIndex } :${ item . key } ` ) )
193+ }
194+ />
185195 )
186196 ) }
187197 </ Fragment >
0 commit comments