Skip to content

Commit e7ed2c2

Browse files
committed
WIP rendering
got rendering working with the new data structure
1 parent 2cd73cb commit e7ed2c2

File tree

7 files changed

+521
-2961
lines changed

7 files changed

+521
-2961
lines changed

apps/demo/src/main.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,7 @@ function getThemeType() {
564564
}
565565

566566
// For quick testing diffs
567+
// FAKE_DIFF_LINE_ANNOTATIONS.length = 0;
567568
// (() => {
568569
// const oldFile = {
569570
// name: 'file_old.ts',

packages/diffs/src/renderers/DiffHunksRenderer.ts

Lines changed: 24 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -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

7776
interface 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

9292
const 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

112113
interface 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
}

packages/diffs/src/types.ts

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -308,26 +308,19 @@ export type AnnotationLineMap<LAnnotation> = Record<
308308

309309
export type ExpansionDirections = 'up' | 'down' | 'both';
310310

311-
export interface RenderDiffFilesResult {
312-
oldLines: ElementContent[];
313-
newLines: ElementContent[];
314-
hunks?: undefined;
315-
}
316-
317-
export interface RenderDiffHunksResult {
318-
hunks: RenderDiffFilesResult[];
319-
oldLines?: undefined;
320-
newLines?: undefined;
321-
}
322-
323311
export interface ThemedFileResult {
324312
code: ElementContent[];
325313
themeStyles: string;
326314
baseThemeType: 'light' | 'dark' | undefined;
327315
}
328316

317+
export interface RenderDiffFilesResult {
318+
oldLines: ElementContent[];
319+
newLines: ElementContent[];
320+
}
321+
329322
export interface ThemedDiffResult {
330-
code: RenderDiffFilesResult | RenderDiffHunksResult;
323+
code: RenderDiffFilesResult;
331324
themeStyles: string;
332325
baseThemeType: 'light' | 'dark' | undefined;
333326
}

packages/diffs/src/utils/diffAcceptRejectHunk.ts

Lines changed: 28 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ export function diffAcceptRejectHunk(
88
diff = {
99
...diff,
1010
hunks: [...diff.hunks],
11-
oldLines: diff.oldLines != null ? [...diff.oldLines] : undefined,
12-
newLines: diff.newLines != null ? [...diff.newLines] : undefined,
11+
oldLines: type === 'accept' ? [...diff.oldLines] : diff.oldLines,
12+
newLines: type === 'reject' ? [...diff.newLines] : diff.newLines,
1313
// Automatically update cacheKey if it exists, since content is changing
1414
cacheKey:
1515
diff.cacheKey != null
@@ -28,20 +28,20 @@ export function diffAcceptRejectHunk(
2828
}
2929
if (type === 'reject') {
3030
newLines.splice(
31-
hunk.additionStart - 1,
31+
hunk.newLinesIndex,
3232
hunk.additionCount,
3333
...oldLines.slice(
34-
hunk.deletionStart - 1,
35-
hunk.deletionStart - 1 + hunk.deletionCount
34+
hunk.oldLinesIndex,
35+
hunk.oldLinesIndex + hunk.deletionCount
3636
)
3737
);
3838
} else {
3939
oldLines.splice(
40-
hunk.deletionStart - 1,
40+
hunk.oldLinesIndex,
4141
hunk.deletionCount,
4242
...newLines.slice(
43-
hunk.additionStart - 1,
44-
hunk.additionStart - 1 + hunk.additionCount
43+
hunk.newLinesIndex,
44+
hunk.newLinesIndex + hunk.additionCount
4545
)
4646
);
4747
}
@@ -58,27 +58,26 @@ export function diffAcceptRejectHunk(
5858
'diffResolveRejectHunk: iterating through hunks, hunk doesnt exist...'
5959
);
6060
}
61-
hunk = { ...hunk };
62-
diff.hunks[i] = hunk;
61+
diff.hunks[i] = hunk = { ...hunk };
6362
if (i === hunkIndex) {
6463
const newContent: ContextContent = {
6564
type: 'context',
66-
lines: [],
65+
lines: 0,
6766
noEOFCR: false,
6867
};
6968
for (const content of hunk.hunkContent) {
7069
if (content.type === 'context') {
71-
newContent.lines.push(...content.lines);
70+
newContent.lines += content.lines;
7271
newContent.noEOFCR = content.noEOFCR;
7372
} else if (type === 'accept') {
74-
newContent.lines.push(...content.additions);
73+
newContent.lines += content.additions;
7574
newContent.noEOFCR = content.noEOFCRAdditions;
7675
} else if (type === 'reject') {
77-
newContent.lines.push(...content.deletions);
76+
newContent.lines += content.deletions;
7877
newContent.noEOFCR = content.noEOFCRDeletions;
7978
}
8079
}
81-
const lineCount = newContent.lines.length;
80+
const lineCount = newContent.lines;
8281
hunk.hunkContent = [newContent];
8382
splitOffset = lineCount - hunk.splitLineCount;
8483
hunk.splitLineCount = lineCount;
@@ -92,10 +91,24 @@ export function diffAcceptRejectHunk(
9291
hunk.additionLines = 0;
9392
diff.splitLineCount += splitOffset;
9493
diff.unifiedLineCount += unifiedOffset;
94+
// If we don't need to make any value offset differences for the rest of
95+
// the hunks, we done
96+
if (
97+
splitOffset === 0 &&
98+
unifiedOffset === 0 &&
99+
additionOffset === 0 &&
100+
deletionOffset === 0
101+
) {
102+
break;
103+
}
95104
} else {
96105
hunk.splitLineStart += splitOffset;
97106
hunk.unifiedLineStart += unifiedOffset;
107+
98108
hunk.additionStart += additionOffset;
109+
hunk.newLinesIndex += additionOffset;
110+
111+
hunk.oldLinesIndex += deletionOffset;
99112
hunk.deletionStart += deletionOffset;
100113
}
101114
}

0 commit comments

Comments
 (0)