@@ -19,7 +19,6 @@ import type {
1919 Hunk ,
2020 HunkData ,
2121 RenderDiffFilesResult ,
22- RenderDiffHunksResult ,
2322 RenderDiffOptions ,
2423 RenderDiffResult ,
2524 RenderRange ,
@@ -75,7 +74,7 @@ interface PushLineWithAnnotation {
7574}
7675
7776interface RenderCollapsedHunksProps {
78- ast : RenderDiffFilesResult | RenderDiffHunksResult ;
77+ ast : RenderDiffFilesResult ;
7978 hunk : Hunk ;
8079 hunkData : HunkData [ ] ;
8180 hunkSpecs : string | undefined ;
@@ -87,6 +86,7 @@ interface RenderCollapsedHunksProps {
8786 deletionsAST : ElementContent [ ] ;
8887 unifiedAST : ElementContent [ ] ;
8988 state : DiffRenderState ;
89+ isPartial : boolean ;
9090}
9191
9292const DEFAULT_RENDER_RANGE : RenderRange = {
@@ -100,13 +100,14 @@ interface RenderHunkProps {
100100 hunk : Hunk ;
101101 hunkData : HunkData [ ] ;
102102
103- ast : RenderDiffFilesResult | RenderDiffHunksResult ;
103+ ast : RenderDiffFilesResult ;
104104 unifiedAST : ElementContent [ ] ;
105105 deletionsAST : ElementContent [ ] ;
106106 additionsAST : ElementContent [ ] ;
107107 isLastHunk : boolean ;
108108
109109 state : DiffRenderState ;
110+ isPartial : boolean ;
110111}
111112
112113interface DiffRenderState {
@@ -540,6 +541,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
540541 deletionsAST,
541542 unifiedAST,
542543 hunkData,
544+ isPartial : fileDiff . isPartial ,
543545 } ) ;
544546 state . hunkIndex ++ ;
545547 state . prevHunk = hunk ;
@@ -552,13 +554,9 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
552554 ) ;
553555
554556 additionsAST =
555- ! unified && ( code . hunks != null || code . newLines . length > 0 )
556- ? additionsAST
557- : undefined ;
557+ ! unified && code . newLines . length > 0 ? additionsAST : undefined ;
558558 deletionsAST =
559- ! unified && ( code . hunks != null || code . oldLines . length > 0 )
560- ? deletionsAST
561- : undefined ;
559+ ! unified && code . oldLines . length > 0 ? deletionsAST : undefined ;
562560 unifiedAST = unifiedAST . length > 0 ? unifiedAST : undefined ;
563561
564562 // FIXME(amadeus): this version of virtualization is probably more
@@ -694,14 +692,13 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
694692 deletionsAST,
695693 additionsAST,
696694 state,
695+ isPartial,
697696 } : RenderCollapsedHunksProps ) {
698697 if ( rangeSize <= 0 ) {
699698 return ;
700699 }
701700 const { hunkSeparators, expandUnchanged, diffStyle, expansionLineCount } =
702701 this . getOptionsWithDefaults ( ) ;
703- const expandable =
704- ast . hunks == null && ast . newLines . length > 0 && ast . oldLines . length > 0 ;
705702 const expandedRegion =
706703 this . expandedHunks . get ( state . hunkIndex ) ?? EXPANDED_REGION ;
707704 const chunked = rangeSize > expansionLineCount ;
@@ -719,7 +716,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
719716 createSeparator ( {
720717 type : hunkSeparators ,
721718 content : getModifiedLinesString ( collapsedLines ) ,
722- expandIndex : expandable ? state . hunkIndex : undefined ,
719+ expandIndex : ! isPartial ? state . hunkIndex : undefined ,
723720 chunked,
724721 slotName,
725722 isFirstHunk,
@@ -731,10 +728,10 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
731728 hunkIndex : state . hunkIndex ,
732729 lines : collapsedLines ,
733730 type,
734- expandable : expandable
731+ expandable : ! isPartial
735732 ? {
736- up : expandable && ! isFirstHunk ,
737- down : expandable ,
733+ up : ! isFirstHunk ,
734+ down : true ,
738735 chunked,
739736 }
740737 : undefined ,
@@ -821,7 +818,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
821818 }
822819 } ;
823820
824- if ( expandable ) {
821+ if ( ! isPartial ) {
825822 const { additionLineNumber, deletionLineNumber } = ( ( ) => {
826823 if ( isLastHunk ) {
827824 return {
@@ -880,6 +877,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
880877 additionsAST,
881878 unifiedAST,
882879 state,
880+ isPartial,
883881 } : RenderHunkProps ) {
884882 const { diffStyle } = this . getOptionsWithDefaults ( ) ;
885883 const unified = diffStyle === 'unified' ;
@@ -897,25 +895,19 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
897895 isLastHunk : false ,
898896 rangeSize : Math . max ( hunk . collapsedBefore , 0 ) ,
899897 unifiedAST,
898+ isPartial,
900899 } ) ;
901900 state . lineIndex = startingLineIndex + hunk . collapsedBefore ;
902901
903902 let additionLineNumber = hunk . additionStart - 1 ;
904903 let deletionLineNumber = hunk . deletionStart - 1 ;
905904 let { oldLines, newLines, oldIndex, newIndex } = ( ( ) => {
906- if ( ast . hunks != null ) {
907- const lineHunk = ast . hunks [ state . hunkIndex ] ;
908- if ( lineHunk == null ) {
909- console . error ( { ast, hunkIndex : state . hunkIndex } ) ;
910- throw new Error (
911- `DiffHunksRenderer.renderHunks: lineHunk doesn't exist`
912- ) ;
913- }
905+ if ( isPartial ) {
914906 return {
915- oldLines : lineHunk . oldLines ,
916- newLines : lineHunk . newLines ,
917- oldIndex : 0 ,
918- newIndex : 0 ,
907+ oldLines : ast . oldLines ,
908+ newLines : ast . newLines ,
909+ oldIndex : hunk . oldLinesIndex ,
910+ newIndex : hunk . newLinesIndex ,
919911 } ;
920912 }
921913 return {
@@ -933,8 +925,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
933925 }
934926 let brokeEarly = false ;
935927 if ( hunkContent . type === 'context' ) {
936- const { length : len } = hunkContent . lines ;
937- for ( let i = 0 ; i < len ; i ++ ) {
928+ for ( let i = 0 ; i < hunkContent . lines ; i ++ ) {
938929 if ( state . renderedLineCount >= state . renderRange . totalLines ) {
939930 brokeEarly = true ;
940931 break ;
@@ -1001,8 +992,8 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
1001992 }
1002993 }
1003994 } else {
1004- const { length : dLen } = hunkContent . deletions ;
1005- const { length : aLen } = hunkContent . additions ;
995+ const dLen = hunkContent . deletions ;
996+ const aLen = hunkContent . additions ;
1006997 const len = unified ? dLen + aLen : Math . max ( dLen , aLen ) ;
1007998 let spanSize = 0 ;
1008999 for ( let i = 0 ; i < len ; i ++ ) {
@@ -1145,6 +1136,7 @@ export class DiffHunksRenderer<LAnnotation = undefined> {
11451136 0
11461137 ) ,
11471138 unifiedAST,
1139+ isPartial,
11481140 } ) ;
11491141 }
11501142 }
0 commit comments