@@ -56,6 +56,11 @@ interface NativeReviewDiffLineRow extends NativeReviewDiffRow {
5656 readonly content : string ;
5757}
5858
59+ interface IndexedNativeReviewDiffLineRow {
60+ readonly row : NativeReviewDiffLineRow ;
61+ readonly rowIndex : number ;
62+ }
63+
5964export interface NativeReviewDiffTokenChunk {
6065 readonly chunkIndex : number ;
6166 readonly fileId : string ;
@@ -308,6 +313,41 @@ function isHighlightableLineRow(row: NativeReviewDiffRow): row is NativeReviewDi
308313 return row . kind === "line" && typeof row . fileId === "string" && typeof row . content === "string" ;
309314}
310315
316+ function hasConsecutiveLineNumbers (
317+ previous : number | null | undefined ,
318+ next : number | null | undefined ,
319+ ) : boolean {
320+ return typeof previous === "number" && typeof next === "number" && next === previous + 1 ;
321+ }
322+
323+ function canShareGrammarContext (
324+ previous : IndexedNativeReviewDiffLineRow ,
325+ next : IndexedNativeReviewDiffLineRow ,
326+ ) : boolean {
327+ if ( next . rowIndex !== previous . rowIndex + 1 || next . row . fileId !== previous . row . fileId ) {
328+ return false ;
329+ }
330+
331+ if ( previous . row . change === "delete" || next . row . change === "delete" ) {
332+ return (
333+ previous . row . change !== "add" &&
334+ next . row . change !== "add" &&
335+ hasConsecutiveLineNumbers ( previous . row . oldLineNumber , next . row . oldLineNumber )
336+ ) ;
337+ }
338+
339+ if ( previous . row . change === "add" || next . row . change === "add" ) {
340+ return hasConsecutiveLineNumbers ( previous . row . newLineNumber , next . row . newLineNumber ) ;
341+ }
342+
343+ return (
344+ previous . row . change === "context" &&
345+ next . row . change === "context" &&
346+ hasConsecutiveLineNumbers ( previous . row . oldLineNumber , next . row . oldLineNumber ) &&
347+ hasConsecutiveLineNumbers ( previous . row . newLineNumber , next . row . newLineNumber )
348+ ) ;
349+ }
350+
311351function groupLineRowsByFileId ( rows : ReadonlyArray < NativeReviewDiffRow > ) {
312352 const rowsByFileId = new Map < string , NativeReviewDiffLineRow [ ] > ( ) ;
313353 for ( const row of rows ) {
@@ -360,7 +400,7 @@ export async function highlightNativeReviewDiffVisibleRows(
360400 const maxRows = input . maxRows ?? NATIVE_REVIEW_DIFF_VISIBLE_MAX_ROWS ;
361401 const startIndex = clampRowIndex ( input . firstRowIndex - overscanRows , input . rows ) ;
362402 const endIndex = clampRowIndex ( input . lastRowIndex + overscanRows , input . rows ) ;
363- const selectedRows : NativeReviewDiffLineRow [ ] = [ ] ;
403+ const selectedRows : IndexedNativeReviewDiffLineRow [ ] = [ ] ;
364404
365405 for (
366406 let rowIndex = startIndex ;
@@ -374,12 +414,12 @@ export async function highlightNativeReviewDiffVisibleRows(
374414 ! input . alreadyHighlightedRowIds ?. has ( row . id ) &&
375415 fileMap . has ( row . fileId )
376416 ) {
377- selectedRows . push ( row ) ;
417+ selectedRows . push ( { row, rowIndex } ) ;
378418 }
379419 }
380420
381421 const tokensByRowId : Record < string , ReadonlyArray < NativeReviewDiffToken > > = { } ;
382- let segmentRows : NativeReviewDiffLineRow [ ] = [ ] ;
422+ let segmentRows : IndexedNativeReviewDiffLineRow [ ] = [ ] ;
383423 let segmentFile : NativeReviewDiffFile | undefined ;
384424
385425 const flushSegment = ( ) => {
@@ -389,27 +429,33 @@ export async function highlightNativeReviewDiffVisibleRows(
389429 return ;
390430 }
391431
392- const code = segmentRows . map ( ( row ) => row . content ) . join ( "\n" ) ;
432+ const code = segmentRows . map ( ( { row } ) => row . content ) . join ( "\n" ) ;
393433 const tokenLines = highlighter . tokenize ( code , { lang : segmentFile . language , theme } ) ;
394- segmentRows . forEach ( ( row , rowIndex ) => {
434+ segmentRows . forEach ( ( { row } , rowIndex ) => {
395435 tokensByRowId [ row . id ] = tokenLines [ rowIndex ] ?? makePlainTokenFallback ( row ) ;
396436 } ) ;
397437 segmentRows = [ ] ;
398438 segmentFile = undefined ;
399439 } ;
400440
401- for ( const row of selectedRows ) {
441+ for ( const selectedRow of selectedRows ) {
442+ const { row } = selectedRow ;
402443 const file = fileMap . get ( row . fileId ) ;
403444 if ( ! file ) {
404445 continue ;
405446 }
406447
407- if ( segmentFile && segmentFile . id !== file . id ) {
448+ const previousRow = segmentRows . at ( - 1 ) ;
449+ if (
450+ segmentFile &&
451+ ( segmentFile . id !== file . id ||
452+ ( previousRow !== undefined && ! canShareGrammarContext ( previousRow , selectedRow ) ) )
453+ ) {
408454 flushSegment ( ) ;
409455 }
410456
411457 segmentFile = file ;
412- segmentRows . push ( row ) ;
458+ segmentRows . push ( selectedRow ) ;
413459 }
414460 flushSegment ( ) ;
415461
0 commit comments