@@ -24,21 +24,7 @@ export default class MultiFilePatch {
2424 this . filePatchesByPath . set ( filePatch . getPath ( ) , filePatch ) ;
2525 this . filePatchesByMarker . set ( filePatch . getMarker ( ) , filePatch ) ;
2626
27- let diffRow = 1 ;
28- const index = new RBTree ( ( a , b ) => a . diffRow - b . diffRow ) ;
29- this . diffRowOffsetIndices . set ( filePatch . getPath ( ) , { startBufferRow : filePatch . getStartRange ( ) . start . row , index} ) ;
30-
31- for ( let hunkIndex = 0 ; hunkIndex < filePatch . getHunks ( ) . length ; hunkIndex ++ ) {
32- const hunk = filePatch . getHunks ( ) [ hunkIndex ] ;
33- this . hunksByMarker . set ( hunk . getMarker ( ) , hunk ) ;
34-
35- // Advance past the hunk body
36- diffRow += hunk . bufferRowCount ( ) ;
37- index . insert ( { diffRow, offset : hunkIndex + 1 } ) ;
38-
39- // Advance past the next hunk header
40- diffRow ++ ;
41- }
27+ this . populateDiffRowOffsetIndices ( filePatch ) ;
4228 }
4329 }
4430
@@ -210,6 +196,28 @@ export default class MultiFilePatch {
210196 return Range . fromObject ( [ [ selectionRow , 0 ] , [ selectionRow , Infinity ] ] ) ;
211197 }
212198
199+ isDiffRowOffsetIndexEmpty ( filePatchPath ) {
200+ const diffRowOffsetIndex = this . diffRowOffsetIndices . get ( filePatchPath ) ;
201+ return diffRowOffsetIndex . index . size === 0 ;
202+ }
203+ populateDiffRowOffsetIndices ( filePatch ) {
204+ let diffRow = 1 ;
205+ const index = new RBTree ( ( a , b ) => a . diffRow - b . diffRow ) ;
206+ this . diffRowOffsetIndices . set ( filePatch . getPath ( ) , { startBufferRow : filePatch . getStartRange ( ) . start . row , index} ) ;
207+
208+ for ( let hunkIndex = 0 ; hunkIndex < filePatch . getHunks ( ) . length ; hunkIndex ++ ) {
209+ const hunk = filePatch . getHunks ( ) [ hunkIndex ] ;
210+ this . hunksByMarker . set ( hunk . getMarker ( ) , hunk ) ;
211+
212+ // Advance past the hunk body
213+ diffRow += hunk . bufferRowCount ( ) ;
214+ index . insert ( { diffRow, offset : hunkIndex + 1 } ) ;
215+
216+ // Advance past the next hunk header
217+ diffRow ++ ;
218+ }
219+ }
220+
213221 adoptBuffer ( nextPatchBuffer ) {
214222 nextPatchBuffer . clearAllLayers ( ) ;
215223
@@ -332,6 +340,12 @@ export default class MultiFilePatch {
332340 for ( const hunk of filePatch . getHunks ( ) ) {
333341 this . hunksByMarker . set ( hunk . getMarker ( ) , hunk ) ;
334342 }
343+
344+ // if the patch was initially collapsed, we need to calculate
345+ // the diffRowOffsetIndices to calculate comment position.
346+ if ( this . isDiffRowOffsetIndexEmpty ( filePatch . getPath ( ) ) ) {
347+ this . populateDiffRowOffsetIndices ( filePatch ) ;
348+ }
335349 }
336350
337351 getMarkersBefore ( filePatchIndex ) {
0 commit comments