-
Notifications
You must be signed in to change notification settings - Fork 87
Expand file tree
/
Copy pathVirtualizedFileDiff.ts
More file actions
762 lines (691 loc) · 23.3 KB
/
VirtualizedFileDiff.ts
File metadata and controls
762 lines (691 loc) · 23.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
import { DEFAULT_COLLAPSED_CONTEXT_THRESHOLD } from '../constants';
import type {
ExpansionDirections,
FileDiffMetadata,
RenderRange,
RenderWindow,
VirtualFileMetrics,
} from '../types';
import { iterateOverDiff } from '../utils/iterateOverDiff';
import { parseDiffFromFile } from '../utils/parseDiffFromFile';
import { resolveVirtualFileMetrics } from '../utils/resolveVirtualFileMetrics';
import type { WorkerPoolManager } from '../worker';
import {
FileDiff,
type FileDiffOptions,
type FileDiffRenderProps,
} from './FileDiff';
import type { Virtualizer } from './Virtualizer';
interface ExpandedRegionSpecs {
fromStart: number;
fromEnd: number;
collapsedLines: number;
renderAll: boolean;
}
let instanceId = -1;
export class VirtualizedFileDiff<
LAnnotation = undefined,
> extends FileDiff<LAnnotation> {
override readonly __id: string = `little-virtualized-file-diff:${++instanceId}`;
public top: number | undefined;
public height: number = 0;
private metrics: VirtualFileMetrics;
// Sparse map: view-specific line index -> measured height
// Only stores lines that differ what is returned from `getLineHeight`
private heightCache: Map<number, number> = new Map();
private isVisible: boolean = false;
private virtualizer: Virtualizer;
constructor(
options: FileDiffOptions<LAnnotation> | undefined,
virtualizer: Virtualizer,
metrics?: Partial<VirtualFileMetrics>,
workerManager?: WorkerPoolManager,
isContainerManaged = false
) {
super(options, workerManager, isContainerManaged);
const { hunkSeparators = 'line-info' } = this.options;
this.virtualizer = virtualizer;
this.metrics = resolveVirtualFileMetrics(
typeof hunkSeparators === 'function' ? 'custom' : hunkSeparators,
metrics
);
}
// Get the height for a line, using cached value if available.
// If not cached and hasMetadataLine is true, adds lineHeight for the metadata.
private getLineHeight(lineIndex: number, hasMetadataLine = false): number {
const cached = this.heightCache.get(lineIndex);
if (cached != null) {
return cached;
}
const multiplier = hasMetadataLine ? 2 : 1;
return this.metrics.lineHeight * multiplier;
}
// Override setOptions to clear height cache when diffStyle changes
override setOptions(options: FileDiffOptions<LAnnotation> | undefined): void {
if (options == null) return;
const previousDiffStyle = this.options.diffStyle;
const previousOverflow = this.options.overflow;
const previousCollapsed = this.options.collapsed;
super.setOptions(options);
if (
previousDiffStyle !== this.options.diffStyle ||
previousOverflow !== this.options.overflow ||
previousCollapsed !== this.options.collapsed
) {
this.heightCache.clear();
this.computeApproximateSize();
this.renderRange = undefined;
}
this.virtualizer.instanceChanged(this);
}
// Measure rendered lines and update height cache.
// Called after render to reconcile estimated vs actual heights.
// Definitely need to optimize this in cases where there aren't any custom
// line heights or in cases of extremely large files...
public reconcileHeights(): void {
const { overflow = 'scroll' } = this.options;
if (this.fileContainer != null) {
this.top = this.virtualizer.getOffsetInScrollContainer(
this.fileContainer
);
}
if (this.fileContainer == null || this.fileDiff == null) {
this.height = 0;
return;
}
// NOTE(amadeus): We can probably be a lot smarter about this, and we
// should be thinking about ways to improve this
// If the file has no annotations and we are using the scroll variant, then
// we can probably skip everything
if (
overflow === 'scroll' &&
this.lineAnnotations.length === 0 &&
!this.virtualizer.config.resizeDebugging
) {
return;
}
const diffStyle = this.getDiffStyle();
let hasLineHeightChange = false;
const codeGroups =
diffStyle === 'split'
? [this.codeDeletions, this.codeAdditions]
: [this.codeUnified];
for (const codeGroup of codeGroups) {
if (codeGroup == null) continue;
const content = codeGroup.children[1];
if (!(content instanceof HTMLElement)) continue;
for (const line of content.children) {
if (!(line instanceof HTMLElement)) continue;
const lineIndexAttr = line.dataset.lineIndex;
if (lineIndexAttr == null) continue;
const lineIndex = parseLineIndex(lineIndexAttr, diffStyle);
let measuredHeight = line.getBoundingClientRect().height;
let hasMetadata = false;
// Annotations or noNewline metadata increase the size of the their
// attached line
if (
line.nextElementSibling instanceof HTMLElement &&
('lineAnnotation' in line.nextElementSibling.dataset ||
'noNewline' in line.nextElementSibling.dataset)
) {
if ('noNewline' in line.nextElementSibling.dataset) {
hasMetadata = true;
}
measuredHeight +=
line.nextElementSibling.getBoundingClientRect().height;
}
const expectedHeight = this.getLineHeight(lineIndex, hasMetadata);
if (measuredHeight === expectedHeight) {
continue;
}
hasLineHeightChange = true;
// Line is back to standard height (e.g., after window resize)
// Remove from cache
if (
measuredHeight ===
this.metrics.lineHeight * (hasMetadata ? 2 : 1)
) {
this.heightCache.delete(lineIndex);
}
// Non-standard height, cache it
else {
this.heightCache.set(lineIndex, measuredHeight);
}
}
}
if (hasLineHeightChange || this.virtualizer.config.resizeDebugging) {
this.computeApproximateSize();
}
}
public onRender = (dirty: boolean): boolean => {
if (this.fileContainer == null) {
return false;
}
if (dirty) {
this.top = this.virtualizer.getOffsetInScrollContainer(
this.fileContainer
);
}
return this.render();
};
override cleanUp(): void {
if (this.fileContainer != null) {
this.virtualizer.disconnect(this.fileContainer);
}
super.cleanUp();
}
override expandHunk(hunkIndex: number, direction: ExpansionDirections): void {
this.hunksRenderer.expandHunk(hunkIndex, direction);
this.computeApproximateSize();
this.renderRange = undefined;
this.virtualizer.instanceChanged(this);
// NOTE(amadeus): We should probably defer to the virtualizer to re-render
// this.rerender();
}
override expandAll(): void {
this.hunksRenderer.expandAll();
this.computeApproximateSize();
this.renderRange = undefined;
this.virtualizer.instanceChanged(this);
}
override collapseAll(): void {
this.hunksRenderer.collapseAll();
this.computeApproximateSize();
this.renderRange = undefined;
this.virtualizer.instanceChanged(this);
}
public setVisibility(visible: boolean): void {
if (this.fileContainer == null) {
return;
}
this.renderRange = undefined;
if (visible && !this.isVisible) {
this.top = this.virtualizer.getOffsetInScrollContainer(
this.fileContainer
);
this.isVisible = true;
} else if (!visible && this.isVisible) {
this.isVisible = false;
this.rerender();
}
}
// Compute the approximate size of the file using cached line heights.
// Uses lineHeight for lines without cached measurements.
// We should probably optimize this if there are no custom line heights...
// The reason we refer to this as `approximate size` is because heights my
// dynamically change for a number of reasons so we can never be fully sure
// if the height is 100% accurate
private computeApproximateSize(): void {
const isFirstCompute = this.height === 0;
this.height = 0;
if (this.fileDiff == null) {
return;
}
const {
disableFileHeader = false,
expandUnchanged = false,
collapsed = false,
collapsedContextThreshold = DEFAULT_COLLAPSED_CONTEXT_THRESHOLD,
hunkSeparators = 'line-info',
} = this.options;
const { diffHeaderHeight, fileGap, hunkSeparatorHeight } = this.metrics;
const diffStyle = this.getDiffStyle();
const separatorGap =
hunkSeparators !== 'simple' &&
hunkSeparators !== 'metadata' &&
hunkSeparators !== 'line-info-basic'
? fileGap
: 0;
// Header or initial padding
if (!disableFileHeader) {
this.height += diffHeaderHeight;
} else if (hunkSeparators !== 'simple' && hunkSeparators !== 'metadata') {
this.height += fileGap;
}
if (collapsed) {
return;
}
iterateOverDiff({
diff: this.fileDiff,
diffStyle,
expandedHunks:
expandUnchanged || this.hunksRenderer.isAllExpanded()
? true
: this.hunksRenderer.getExpandedHunksMap(),
collapsedContextThreshold,
callback: ({
hunkIndex,
collapsedBefore,
collapsedAfter,
deletionLine,
additionLine,
}) => {
const splitLineIndex =
additionLine != null
? additionLine.splitLineIndex
: deletionLine.splitLineIndex;
const unifiedLineIndex =
additionLine != null
? additionLine.unifiedLineIndex
: deletionLine.unifiedLineIndex;
const hasMetadata =
(additionLine?.noEOFCR ?? false) || (deletionLine?.noEOFCR ?? false);
if (collapsedBefore > 0) {
if (hunkIndex > 0) {
this.height += separatorGap;
}
this.height += hunkSeparatorHeight + separatorGap;
}
this.height += this.getLineHeight(
diffStyle === 'split' ? splitLineIndex : unifiedLineIndex,
hasMetadata
);
if (collapsedAfter > 0 && hunkSeparators !== 'simple') {
this.height += separatorGap + hunkSeparatorHeight;
}
},
});
// Bottom padding
if (this.fileDiff.hunks.length > 0) {
this.height += fileGap;
}
if (
this.fileContainer != null &&
this.virtualizer.config.resizeDebugging &&
!isFirstCompute
) {
const rect = this.fileContainer.getBoundingClientRect();
if (rect.height !== this.height) {
console.log(
'VirtualizedFileDiff.computeApproximateSize: computed height doesnt match',
{
name: this.fileDiff.name,
elementHeight: rect.height,
computedHeight: this.height,
}
);
} else {
console.log(
'VirtualizedFileDiff.computeApproximateSize: computed height IS CORRECT'
);
}
}
}
override render({
fileContainer,
oldFile,
newFile,
fileDiff,
...props
}: FileDiffRenderProps<LAnnotation> = {}): boolean {
// NOTE(amadeus): Probably not the safest way to determine first render...
// but for now...
const isFirstRender = this.fileContainer == null;
this.fileDiff ??=
fileDiff ??
(oldFile != null && newFile != null
? // NOTE(amadeus): We might be forcing ourselves to double up the
// computation of fileDiff (in the super.render() call), so we might want
// to figure out a way to avoid that. That also could be just as simple as
// passing through fileDiff though... so maybe we good?
parseDiffFromFile(oldFile, newFile)
: undefined);
fileContainer = this.getOrCreateFileContainer(fileContainer);
if (this.fileDiff == null) {
console.error(
'VirtualizedFileDiff.render: attempting to virtually render when we dont have the correct data'
);
return false;
}
if (isFirstRender) {
this.computeApproximateSize();
this.virtualizer.connect(fileContainer, this);
this.top ??= this.virtualizer.getOffsetInScrollContainer(fileContainer);
this.isVisible = this.virtualizer.isInstanceVisible(
this.top,
this.height
);
} else {
this.top ??= this.virtualizer.getOffsetInScrollContainer(fileContainer);
}
if (!this.isVisible) {
return this.renderPlaceholder(this.height);
}
const windowSpecs = this.virtualizer.getWindowSpecs();
const renderRange = this.computeRenderRangeFromWindow(
this.fileDiff,
this.top,
windowSpecs
);
return super.render({
fileDiff: this.fileDiff,
fileContainer,
renderRange,
oldFile,
newFile,
...props,
});
}
private getDiffStyle(): 'split' | 'unified' {
return this.options.diffStyle ?? 'split';
}
private getExpandedRegion(
isPartial: boolean,
hunkIndex: number,
rangeSize: number
): ExpandedRegionSpecs {
if (rangeSize <= 0 || isPartial) {
return {
fromStart: 0,
fromEnd: 0,
collapsedLines: Math.max(rangeSize, 0),
renderAll: false,
};
}
const {
expandUnchanged = false,
collapsedContextThreshold = DEFAULT_COLLAPSED_CONTEXT_THRESHOLD,
} = this.options;
if (
expandUnchanged ||
this.hunksRenderer.isAllExpanded() ||
rangeSize <= collapsedContextThreshold
) {
return {
fromStart: rangeSize,
fromEnd: 0,
collapsedLines: 0,
renderAll: true,
};
}
const region = this.hunksRenderer.getExpandedHunk(hunkIndex);
const fromStart = Math.min(Math.max(region.fromStart, 0), rangeSize);
const fromEnd = Math.min(Math.max(region.fromEnd, 0), rangeSize);
const expandedCount = fromStart + fromEnd;
const renderAll = expandedCount >= rangeSize;
return {
fromStart,
fromEnd,
collapsedLines: Math.max(rangeSize - expandedCount, 0),
renderAll,
};
}
private getExpandedLineCount(
fileDiff: FileDiffMetadata,
diffStyle: 'split' | 'unified'
): number {
let count = 0;
if (fileDiff.isPartial) {
for (const hunk of fileDiff.hunks) {
count +=
diffStyle === 'split' ? hunk.splitLineCount : hunk.unifiedLineCount;
}
return count;
}
for (const [hunkIndex, hunk] of fileDiff.hunks.entries()) {
const hunkCount =
diffStyle === 'split' ? hunk.splitLineCount : hunk.unifiedLineCount;
count += hunkCount;
const collapsedBefore = Math.max(hunk.collapsedBefore, 0);
const { fromStart, fromEnd, renderAll } = this.getExpandedRegion(
fileDiff.isPartial,
hunkIndex,
collapsedBefore
);
if (collapsedBefore > 0) {
count += renderAll ? collapsedBefore : fromStart + fromEnd;
}
}
const lastHunk = fileDiff.hunks.at(-1);
if (lastHunk != null && hasFinalHunk(fileDiff)) {
const additionRemaining =
fileDiff.additionLines.length -
(lastHunk.additionLineIndex + lastHunk.additionCount);
const deletionRemaining =
fileDiff.deletionLines.length -
(lastHunk.deletionLineIndex + lastHunk.deletionCount);
if (lastHunk != null && additionRemaining !== deletionRemaining) {
throw new Error(
`VirtualizedFileDiff: trailing context mismatch (additions=${additionRemaining}, deletions=${deletionRemaining}) for ${fileDiff.name}`
);
}
const trailingRangeSize = Math.min(additionRemaining, deletionRemaining);
if (lastHunk != null && trailingRangeSize > 0) {
const { fromStart, renderAll } = this.getExpandedRegion(
fileDiff.isPartial,
fileDiff.hunks.length,
trailingRangeSize
);
count += renderAll ? trailingRangeSize : fromStart;
}
}
return count;
}
private computeRenderRangeFromWindow(
fileDiff: FileDiffMetadata,
fileTop: number,
{ top, bottom }: RenderWindow
): RenderRange {
const {
disableFileHeader = false,
expandUnchanged = false,
collapsedContextThreshold = DEFAULT_COLLAPSED_CONTEXT_THRESHOLD,
hunkSeparators = 'line-info',
} = this.options;
const {
diffHeaderHeight,
fileGap,
hunkLineCount,
hunkSeparatorHeight,
lineHeight,
} = this.metrics;
const diffStyle = this.getDiffStyle();
const fileHeight = this.height;
const lineCount = this.getExpandedLineCount(fileDiff, diffStyle);
// Calculate headerRegion before early returns
const headerRegion = disableFileHeader ? fileGap : diffHeaderHeight;
// File is outside render window
if (fileTop < top - fileHeight || fileTop > bottom) {
return {
startingLine: 0,
totalLines: 0,
bufferBefore: 0,
bufferAfter:
fileHeight -
headerRegion -
// This last file gap represents the bottom padding that buffers
// should not account for
fileGap,
};
}
// Whole file is under hunkLineCount, just render it all
if (lineCount <= hunkLineCount || fileDiff.hunks.length === 0) {
return {
startingLine: 0,
totalLines: hunkLineCount,
bufferBefore: 0,
bufferAfter: 0,
};
}
const estimatedTargetLines = Math.ceil(
Math.max(bottom - top, 0) / lineHeight
);
const totalLines =
Math.ceil(estimatedTargetLines / hunkLineCount) * hunkLineCount +
hunkLineCount;
const totalHunks = totalLines / hunkLineCount;
const overflowHunks = totalHunks;
const hunkOffsets: number[] = [];
// Halfway between top & bottom, represented as an absolute position
const viewportCenter = (top + bottom) / 2;
const separatorGap =
hunkSeparators === 'simple' ||
hunkSeparators === 'metadata' ||
hunkSeparators === 'line-info-basic'
? 0
: fileGap;
let absoluteLineTop = fileTop + headerRegion;
let currentLine = 0;
let firstVisibleHunk: number | undefined;
let centerHunk: number | undefined;
let overflowCounter: number | undefined;
iterateOverDiff({
diff: fileDiff,
diffStyle,
expandedHunks:
expandUnchanged || this.hunksRenderer.isAllExpanded()
? true
: this.hunksRenderer.getExpandedHunksMap(),
collapsedContextThreshold,
callback: ({
hunkIndex,
collapsedBefore,
collapsedAfter,
deletionLine,
additionLine,
}) => {
const splitLineIndex =
additionLine != null
? additionLine.splitLineIndex
: deletionLine.splitLineIndex;
const unifiedLineIndex =
additionLine != null
? additionLine.unifiedLineIndex
: deletionLine.unifiedLineIndex;
const hasMetadata =
(additionLine?.noEOFCR ?? false) || (deletionLine?.noEOFCR ?? false);
let gapAdjustment =
collapsedBefore > 0
? hunkSeparatorHeight +
separatorGap +
(hunkIndex > 0 ? separatorGap : 0)
: 0;
if (hunkIndex === 0 && hunkSeparators === 'simple') {
gapAdjustment = 0;
}
absoluteLineTop += gapAdjustment;
const isAtHunkBoundary = currentLine % hunkLineCount === 0;
// Track the boundary positional offset at a hunk
if (isAtHunkBoundary) {
hunkOffsets.push(
absoluteLineTop - (fileTop + headerRegion + gapAdjustment)
);
// Check if we should bail (overflow complete)
if (overflowCounter != null) {
if (overflowCounter <= 0) {
return true;
}
overflowCounter--;
}
}
const lineHeight = this.getLineHeight(
diffStyle === 'split' ? splitLineIndex : unifiedLineIndex,
hasMetadata
);
const currentHunk = Math.floor(currentLine / hunkLineCount);
// Track visible region
if (absoluteLineTop > top - lineHeight && absoluteLineTop < bottom) {
firstVisibleHunk ??= currentHunk;
}
// Track which hunk contains the viewport center
// If viewport center is above this line and we haven't set centerHunk yet,
// this is the first line at or past the center
if (
centerHunk == null &&
absoluteLineTop + lineHeight > viewportCenter
) {
centerHunk = currentHunk;
}
// Start overflow when we are out of the viewport at a hunk boundary
if (
overflowCounter == null &&
absoluteLineTop >= bottom &&
isAtHunkBoundary
) {
overflowCounter = overflowHunks;
}
currentLine++;
absoluteLineTop += lineHeight;
if (collapsedAfter > 0 && hunkSeparators !== 'simple') {
absoluteLineTop += hunkSeparatorHeight + separatorGap;
}
return false;
},
});
// No visible lines found
if (firstVisibleHunk == null) {
return {
startingLine: 0,
totalLines: 0,
bufferBefore: 0,
bufferAfter:
fileHeight -
headerRegion -
// We gotta subtract the bottom padding off of the buffer
fileGap,
};
}
// Calculate balanced startingLine centered around the viewport center
// Fall back to firstVisibleHunk if center wasn't found (e.g., center in a gap)
const collectedHunks = hunkOffsets.length;
centerHunk ??= firstVisibleHunk;
const idealStartHunk = Math.round(centerHunk - totalHunks / 2);
// Clamp startHunk: at the beginning, reduce totalLines; at the end, shift startHunk back
const maxStartHunk = Math.max(0, collectedHunks - totalHunks);
const startHunk = Math.max(0, Math.min(idealStartHunk, maxStartHunk));
const startingLine = startHunk * hunkLineCount;
// If we wanted to start before 0, reduce totalLines by the clamped amount
const clampedTotalLines =
idealStartHunk < 0
? totalLines + idealStartHunk * hunkLineCount
: totalLines;
// Use hunkOffsets array for efficient buffer calculations
const bufferBefore = hunkOffsets[startHunk] ?? 0;
// Calculate bufferAfter using hunkOffset if available, otherwise use cumulative height
const finalHunkIndex = startHunk + clampedTotalLines / hunkLineCount;
const bufferAfter =
finalHunkIndex < hunkOffsets.length
? fileHeight -
headerRegion -
hunkOffsets[finalHunkIndex] -
// We gotta subtract the bottom padding off of the buffer
fileGap
: // We stopped early, calculate from current position
fileHeight -
(absoluteLineTop - fileTop) -
// We gotta subtract the bottom padding off of the buffer
fileGap;
return {
startingLine,
totalLines: clampedTotalLines,
bufferBefore,
bufferAfter,
};
}
}
function hasFinalHunk(fileDiff: FileDiffMetadata): boolean {
const lastHunk = fileDiff.hunks.at(-1);
if (
lastHunk == null ||
fileDiff.isPartial ||
fileDiff.additionLines.length === 0 ||
fileDiff.deletionLines.length === 0
) {
return false;
}
return (
lastHunk.additionLineIndex + lastHunk.additionCount <
fileDiff.additionLines.length ||
lastHunk.deletionLineIndex + lastHunk.deletionCount <
fileDiff.deletionLines.length
);
}
// Extracts the view-specific line index from the data-line-index attribute.
// Format is "unifiedIndex,splitIndex"
function parseLineIndex(
lineIndexAttr: string,
diffStyle: 'split' | 'unified'
): number {
const [unifiedIndex, splitIndex] = lineIndexAttr.split(',').map(Number);
return diffStyle === 'split' ? splitIndex : unifiedIndex;
}