-
-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathconcise-diff-view.svelte.ts
More file actions
1161 lines (1021 loc) · 41 KB
/
Copy pathconcise-diff-view.svelte.ts
File metadata and controls
1161 lines (1021 loc) · 41 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
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import { diffArrays, type StructuredPatchHunk, type StructuredPatch, parsePatch } from "diff";
import {
codeToTokens,
type BundledLanguage,
type BundledTheme,
type CodeToTokensOptions,
type GrammarState,
type TokensResult,
type ThemedToken,
type ThemeRegistration,
bundledThemes,
} from "shiki";
import { guessLanguageFromExtension, type MutableValue, type ReadableBoxedValues } from "$lib/util";
import type { IRawThemeSetting } from "shiki/textmate";
import chroma from "chroma-js";
import { getEffectiveGlobalTheme } from "$lib/theme.svelte";
import { onDestroy } from "svelte";
export const DEFAULT_THEME_LIGHT: BundledTheme = "github-light-default";
export const DEFAULT_THEME_DARK: BundledTheme = "github-dark-default";
export type DiffViewerPatch = {
hunks: DiffViewerPatchHunk[];
};
export type DiffViewerPatchHunk = {
lines: PatchLine[];
innerPatchHeaderChangesOnly?: boolean;
};
export type PatchLine = {
type: PatchLineType;
content: LineSegment[];
lineBreak?: boolean;
innerPatchLineType: InnerPatchLineType;
oldLineNo?: number;
newLineNo?: number;
};
export type LineSegment = {
text?: string | null;
iconClass?: string | null;
caption?: string | null;
classes?: string;
style?: string;
};
export enum PatchLineType {
HEADER,
CONTEXT,
ADD,
REMOVE,
SPACER,
}
export enum InnerPatchLineType {
ADD,
REMOVE,
NONE,
}
export type PatchLineTypeProps = {
classes: string;
lineNoClasses: string;
prefix: string;
};
export const patchLineTypeProps: Record<PatchLineType, PatchLineTypeProps> = {
[PatchLineType.HEADER]: {
classes: "bg-[var(--hunk-header-bg)] text-[var(--hunk-header-fg)]",
lineNoClasses: "bg-[var(--hunk-header-bg)] text-[var(--hunk-header-fg)]",
prefix: "",
},
[PatchLineType.ADD]: {
classes: "bg-[var(--inserted-line-bg)]",
lineNoClasses: "bg-[var(--inserted-line-bg)]",
prefix: "+",
},
[PatchLineType.REMOVE]: {
classes: "bg-[var(--removed-line-bg)]",
lineNoClasses: "bg-[var(--removed-line-bg)]",
prefix: "-",
},
[PatchLineType.CONTEXT]: {
classes: "",
lineNoClasses: "text-em-med",
prefix: "",
},
[PatchLineType.SPACER]: {
classes: "h-2",
lineNoClasses: "",
prefix: "",
},
};
export type InnerPatchLineTypeProps = {
style: string;
};
export const innerPatchLineTypeProps: Record<InnerPatchLineType, InnerPatchLineTypeProps> = {
[InnerPatchLineType.ADD]: {
// Make sure tailwind emits these props
// "bg-green-100 bg-green-300 bg-green-400 bg-green-800"
style: `
--fg-override: var(--inner-inserted-line-fg);
background-color: var(--inner-inserted-line-bg);`,
},
[InnerPatchLineType.REMOVE]: {
// Make sure tailwind emits these props
// "bg-red-100 bg-red-300 bg-red-400 bg-red-800"
style: `
--fg-override: var(--inner-removed-line-fg);
background-color: var(--inner-removed-line-bg);`,
},
[InnerPatchLineType.NONE]: {
style: "",
},
};
const joiner = "\uE000";
const noTrailingNewlineMarker: string = joiner + joiner + ["PATCH", "ROULETTE", "NO", "TRAILING", "NEWLINE", "MARKER"].join(joiner) + joiner + joiner;
enum LineProcessorState {
CONTEXT,
ADD,
REMOVE,
}
class LineProcessor {
private contentLines: string[] = [];
private output: PatchLine[] = [];
private state: LineProcessorState = LineProcessorState.CONTEXT;
private addLinesText: string[] = [];
private removeLinesText: string[] = [];
private contextLinesText: string[] = [];
private fromFile: string | undefined;
private toFile: string | undefined;
private patchFile: boolean = false;
private lastShikiStateAdd: GrammarState | null = null;
private lastShikiStateRemove: GrammarState | null = null;
private lastShikiStateContext: GrammarState | null = null;
private syntaxHighlighting: boolean = true;
private syntaxHighlightingTheme: BundledTheme = DEFAULT_THEME_LIGHT;
private wordDiffs: boolean = true;
private oldLineNo: number = 0;
private newLineNo: number = 0;
async process(
fromFile: string | undefined,
toFile: string | undefined,
hunk: StructuredPatchHunk,
syntaxHighlighting: boolean,
syntaxHighlightingTheme: BundledTheme | undefined,
wordDiffs: boolean,
): Promise<PatchLine[]> {
this.initialize(fromFile, toFile, hunk, syntaxHighlighting, syntaxHighlightingTheme, wordDiffs);
await this.processInternal();
return this.output;
}
private initialize(
fromFile: string | undefined,
toFile: string | undefined,
hunk: StructuredPatchHunk,
syntaxHighlighting: boolean,
syntaxHighlightingTheme: BundledTheme | undefined,
wordDiffs: boolean,
) {
this.contentLines = hunk.lines;
this.output = [];
this.oldLineNo = hunk.oldStart;
this.newLineNo = hunk.newStart;
this.state = LineProcessorState.CONTEXT;
this.addLinesText = [];
this.removeLinesText = [];
this.contextLinesText = [];
this.fromFile = fromFile;
this.toFile = toFile;
this.patchFile = this.isPatchFile(fromFile) || this.isPatchFile(toFile);
this.lastShikiStateAdd = null;
this.lastShikiStateRemove = null;
this.lastShikiStateContext = null;
this.syntaxHighlighting = syntaxHighlighting;
this.syntaxHighlightingTheme = syntaxHighlightingTheme || this.syntaxHighlightingTheme;
this.wordDiffs = wordDiffs;
}
private eitherFileName(): string | undefined {
return this.fromFile || this.toFile;
}
private isPatchFile(path: string | undefined): boolean {
if (path === undefined) {
return false;
}
return path.endsWith(".patch") || path.endsWith(".diff");
}
private async processInternal() {
for (let i = 0; i < this.contentLines.length; i++) {
const lineText = this.contentLines[i];
const oldState = this.state;
if (lineText.startsWith("+")) {
this.state = LineProcessorState.ADD;
} else if (lineText.startsWith("-")) {
this.state = LineProcessorState.REMOVE;
} else {
if (isNoNewlineAtEofLine(lineText)) {
// This is metadata for the previous line
switch (this.state) {
case LineProcessorState.ADD:
this.addLinesText[this.addLinesText.length - 1] += noTrailingNewlineMarker;
break;
case LineProcessorState.REMOVE:
this.removeLinesText[this.removeLinesText.length - 1] += noTrailingNewlineMarker;
break;
case LineProcessorState.CONTEXT:
this.contextLinesText[this.contextLinesText.length - 1] += noTrailingNewlineMarker;
break;
}
continue;
} else {
this.state = LineProcessorState.CONTEXT;
}
}
const stateChanged = oldState !== this.state;
if (stateChanged && this.state === LineProcessorState.CONTEXT) {
/*
* Transition to CONTEXT
*/
if (this.wordDiffs && this.addLinesText.length == this.removeLinesText.length) {
await this.processLineDiff();
} else {
// The added and removed lines are not adjacent or are not symmetric
await this.appendRemainingPlain();
}
} else if (stateChanged && oldState === LineProcessorState.CONTEXT) {
/*
* Transition from CONTEXT
*/
await this.appendRemainingPlain();
}
if (this.state === LineProcessorState.ADD) {
this.addLinesText.push(lineText.substring(1));
} else if (this.state === LineProcessorState.REMOVE) {
this.removeLinesText.push(lineText.substring(1));
} else {
this.contextLinesText.push(lineText.substring(1));
}
}
if (this.state === LineProcessorState.CONTEXT) {
await this.appendRemainingPlain();
} else if (this.wordDiffs && this.addLinesText.length == this.removeLinesText.length) {
await this.processLineDiff();
} else {
// The added and removed lines are not adjacent or are not symmetric
await this.appendRemainingPlain();
}
this.postprocess();
}
private async codeToSegmentsPlain(text: string, state: LineProcessorState): Promise<LineSegment[]> {
const tokensResult = await this.codeToTokensResult(text, state);
if (tokensResult) {
return tokensResult.tokens[0].map((token) => {
return { text: token.content, style: this.getSegmentStyle(state, token.color) };
});
} else {
return [{ text, style: this.getSegmentStyle(state) }];
}
}
private getSegmentStyle(state: LineProcessorState, color?: string | undefined): string {
const segmentFg = color ? `--segment-fg: ${color};` : "";
switch (state) {
case LineProcessorState.ADD:
return `color: var(--fg-override, var(--inserted-line-fg-themed, var(--segment-fg, var(--editor-fg)))); ${segmentFg}`;
case LineProcessorState.REMOVE:
return `color: var(--fg-override, var(--removed-line-fg-themed, var(--segment-fg, var(--editor-fg)))); ${segmentFg}`;
case LineProcessorState.CONTEXT:
return `color: var(--fg-override, var(--segment-fg, var(--editor-fg)));${segmentFg};`;
}
}
private async codeToTokensResult(text: string, state: LineProcessorState): Promise<TokensResult | null> {
if (!this.syntaxHighlighting) {
return null;
}
const opts: CodeToTokensOptions<BundledLanguage, BundledTheme> = {
lang: guessLanguageFromExtension(this.eitherFileName()!),
theme: this.syntaxHighlightingTheme,
};
// Use state from the previous line, using context to fill the gaps
// for adds/removes and adds to fill the gaps for context
switch (state) {
case LineProcessorState.ADD:
opts.grammarState = this.lastShikiStateAdd || this.lastShikiStateContext || undefined;
break;
case LineProcessorState.REMOVE:
opts.grammarState = this.lastShikiStateRemove || this.lastShikiStateContext || undefined;
break;
case LineProcessorState.CONTEXT:
opts.grammarState = this.lastShikiStateContext || this.lastShikiStateAdd || undefined;
break;
}
let result;
try {
result = await codeToTokens(text, opts);
} catch (err) {
this.lastShikiStateContext = null;
this.lastShikiStateAdd = null;
this.lastShikiStateRemove = null;
console.error(`Error tokenizing line '${text}' of file '${this.fromFile}' -> '${this.toFile}'`, err);
return null;
}
result.tokens = result.tokens.map(mergeTokens);
switch (state) {
case LineProcessorState.ADD:
this.lastShikiStateAdd = result.grammarState || null;
break;
case LineProcessorState.REMOVE:
this.lastShikiStateRemove = result.grammarState || null;
break;
case LineProcessorState.CONTEXT:
this.lastShikiStateContext = result.grammarState || null;
this.lastShikiStateAdd = null;
this.lastShikiStateRemove = null;
break;
}
return result;
}
private async appendRemainingPlain() {
for (let i = 0; i < this.removeLinesText.length; i++) {
const text = this.removeLinesText[i];
const content = await this.codeToSegmentsPlain(text, LineProcessorState.REMOVE);
this.output.push({
content,
type: PatchLineType.REMOVE,
innerPatchLineType: this.getInnerType(text),
oldLineNo: this.oldLineNo++,
newLineNo: undefined,
});
}
for (let i = 0; i < this.addLinesText.length; i++) {
const text = this.addLinesText[i];
const content = await this.codeToSegmentsPlain(text, LineProcessorState.ADD);
this.output.push({
content,
type: PatchLineType.ADD,
innerPatchLineType: this.getInnerType(text),
oldLineNo: undefined,
newLineNo: this.newLineNo++,
});
}
for (let i = 0; i < this.contextLinesText.length; i++) {
const text = this.contextLinesText[i];
const content = await this.codeToSegmentsPlain(text, LineProcessorState.CONTEXT);
this.output.push({
content,
type: PatchLineType.CONTEXT,
innerPatchLineType: this.getInnerType(text),
oldLineNo: this.oldLineNo++,
newLineNo: this.newLineNo++,
});
}
this.removeLinesText = [];
this.addLinesText = [];
this.contextLinesText = [];
}
private async processLineDiff() {
const addLines: LineSegment[][] = [];
const removeLines: LineSegment[][] = [];
for (let j = 0; j < this.addLinesText.length; j++) {
// Get syntax highlighting from Shiki for both lines
const removeShikiResult = await this.codeToTokensResult(this.removeLinesText[j], LineProcessorState.REMOVE);
const addShikiResult = await this.codeToTokensResult(this.addLinesText[j], LineProcessorState.ADD);
// Tokenize for diff
const removeStringTokens = genericTokenize(this.removeLinesText[j]);
const addStringTokens = genericTokenize(this.addLinesText[j]);
const diffResult = diffArrays(removeStringTokens, addStringTokens, {
oneChangePerToken: false,
});
// Map colors from Shiki to our tokens
const addLine: LineSegment[] = [];
const removeLine: LineSegment[] = [];
let removePos = 0;
let addPos = 0;
for (const change of diffResult) {
const text = change.value.join("");
if (change.added) {
const segments = this.makeSegments(addShikiResult, addPos, text, `bg-[var(--inserted-text-bg)]`, LineProcessorState.ADD);
segments[0].classes = segments[0].classes + " rounded-l-sm";
segments[segments.length - 1].classes = segments[segments.length - 1].classes + " rounded-r-sm";
addLine.push(...segments);
addPos += text.length;
} else if (change.removed) {
const segments = this.makeSegments(removeShikiResult, removePos, text, `bg-[var(--removed-text-bg)]`, LineProcessorState.REMOVE);
segments[0].classes = segments[0].classes + " rounded-l-sm";
segments[segments.length - 1].classes = segments[segments.length - 1].classes + " rounded-r-sm";
removeLine.push(...segments);
removePos += text.length;
} else {
addLine.push(...this.makeSegments(addShikiResult, addPos, text, "", LineProcessorState.ADD));
addPos += text.length;
removeLine.push(...this.makeSegments(removeShikiResult, removePos, text, "", LineProcessorState.REMOVE));
removePos += text.length;
}
}
if (addLine.length !== 0) {
addLines.push(addLine);
}
if (removeLine.length !== 0) {
removeLines.push(removeLine);
}
}
removeLines.forEach((line) => {
this.output.push({
content: line,
type: PatchLineType.REMOVE,
innerPatchLineType: this.getInnerType(line[0].text!),
oldLineNo: this.oldLineNo++,
newLineNo: undefined,
});
});
addLines.forEach((line) => {
this.output.push({
content: line,
type: PatchLineType.ADD,
innerPatchLineType: this.getInnerType(line[0].text!),
oldLineNo: undefined,
newLineNo: this.newLineNo++,
});
});
this.addLinesText = [];
this.removeLinesText = [];
}
private makeSegments(
shikiResult: TokensResult | null,
startPosition: number,
text: string,
baseClasses: string,
lineState: LineProcessorState,
): LineSegment[] {
if (shikiResult) {
return this.makeSegmentsShiki(shikiResult, startPosition, text, baseClasses, lineState);
}
return [{ text, classes: baseClasses, style: this.getSegmentStyle(lineState) }];
}
// Use the Shiki color data to split the text into colored segments
private makeSegmentsShiki(
shikiResult: TokensResult,
startPosition: number,
text: string,
baseClasses: string,
lineState: LineProcessorState,
): LineSegment[] {
const segments: LineSegment[] = [];
let remainingText = text;
let position = startPosition;
const tokens = [...shikiResult.tokens[0]];
let token: ThemedToken;
while (tokens.length > 0) {
token = tokens.shift()!;
const tokenStart = token.offset;
const tokenEnd = tokenStart + token.content.length;
// Skip tokens that end before the current position
if (position >= tokenEnd) {
continue;
}
if (tokenStart >= position + remainingText.length) {
throw Error("Encountered token that starts after the end of the text");
}
// Split the text into parts that are in the Shiki token and the trailing text
const overlapLength = Math.min(tokenEnd - position, remainingText.length);
const consumedToken = overlapLength === remainingText.length;
const overlapText = remainingText.substring(0, overlapLength);
const trailingText = remainingText.substring(overlapLength);
segments.push({
text: overlapText,
classes: baseClasses,
style: this.getSegmentStyle(lineState, token.color),
});
remainingText = trailingText;
position = position + overlapLength;
if (!consumedToken) {
tokens.unshift(token);
}
if (remainingText.length === 0) {
// We reached the end of the text
break;
}
}
if (remainingText.length > 0) {
throw Error("Remaining text after processing all tokens");
}
return segments;
}
private postprocess() {
for (const line of this.output) {
if (line.content.length === 0 || (line.content.length === 1 && line.content[0].text === "")) {
line.lineBreak = true;
line.content = [];
continue;
}
const lastSegment = line.content[line.content.length - 1];
if (!lastSegment.text) {
continue;
}
if (lastSegment.text.endsWith(noTrailingNewlineMarker)) {
lastSegment.text = lastSegment.text.substring(0, lastSegment.text.length - noTrailingNewlineMarker.length);
if (lastSegment.text === "") {
line.content.pop();
}
line.content.push({
iconClass: "octicon--no-entry-16",
caption: "No trailing newline",
classes: lastSegment.classes + " text-red-600",
});
}
}
}
private getInnerType(text: string) {
if (this.patchFile) {
if (text.startsWith("+")) {
return InnerPatchLineType.ADD;
} else if (text.startsWith("-")) {
return InnerPatchLineType.REMOVE;
}
}
return InnerPatchLineType.NONE;
}
}
export function isNoNewlineAtEofLine(text: string) {
return text === "\\ No newline at end of file";
}
// Our tokens will be split when they intersect two shiki tokens, so we preprocess to merge tokens with same style,
// accounting for style of whitespace being irrelevant
function mergeTokens(tokens: ThemedToken[]): ThemedToken[] {
function isWhitespace(token: ThemedToken) {
return token.content.trim() === "";
}
const mergedTokens: ThemedToken[] = [];
let lastToken: ThemedToken | null = null;
for (const token of tokens) {
if (lastToken === null) {
lastToken = { ...token };
} else if (lastToken.color === token.color) {
lastToken.content += token.content;
} else if (isWhitespace(lastToken)) {
token.content = lastToken.content + token.content;
token.offset = lastToken.offset;
lastToken = token;
} else if (isWhitespace(token)) {
lastToken.content += token.content;
} else {
mergedTokens.push(lastToken);
lastToken = { ...token };
}
}
if (lastToken !== null) {
mergedTokens.push(lastToken);
}
return mergedTokens;
}
const lineProcessors: LineProcessor[] = [];
async function withLineProcessor<R>(fn: (proc: LineProcessor) => Promise<R>): Promise<R> {
const lineProcessor = lineProcessors.pop() ?? new LineProcessor();
try {
return await fn(lineProcessor);
} finally {
lineProcessors.push(lineProcessor);
}
}
export async function parseDiffViewerPatch(
patchPromise: StructuredPatch | Promise<StructuredPatch>,
syntaxHighlighting: boolean,
syntaxHighlightingTheme: BundledTheme | undefined,
omitPatchHeaderOnlyHunks: boolean,
wordDiffs: boolean,
): Promise<DiffViewerPatch> {
const patch = await patchPromise;
const hunks: DiffViewerPatchHunk[] = [];
for (let i = 0; i < patch.hunks.length; i++) {
const hunk = patch.hunks[i];
hunks.push(await makeHunk(patch, hunk, syntaxHighlighting, syntaxHighlightingTheme, omitPatchHeaderOnlyHunks, wordDiffs));
}
return { hunks };
}
async function makeHunk(
patch: StructuredPatch,
hunk: StructuredPatchHunk,
syntaxHighlighting: boolean,
syntaxHighlightingTheme: BundledTheme | undefined,
omitPatchHeaderOnlyHunks: boolean,
wordDiffs: boolean,
): Promise<DiffViewerPatchHunk> {
// Skip this hunk if it only contains header changes
if (omitPatchHeaderOnlyHunks && !hasNonHeaderChanges(hunk.lines)) {
return { innerPatchHeaderChangesOnly: true, lines: [] };
}
const lines: PatchLine[] = [];
// Add the hunk header
const header = `@@ -${hunk.oldStart},${hunk.oldLines} +${hunk.newStart},${hunk.newLines} @@`;
lines.push({
type: PatchLineType.HEADER,
content: [{ text: header }],
innerPatchLineType: InnerPatchLineType.NONE,
});
const oldFileName = patch.oldFileName === "/dev/null" ? undefined : patch.oldFileName;
const newFileName = patch.newFileName === "/dev/null" ? undefined : patch.newFileName;
const hunkLines = await withLineProcessor((proc) => {
return proc.process(oldFileName, newFileName, hunk, syntaxHighlighting, syntaxHighlightingTheme, wordDiffs);
});
lines.push(...hunkLines);
// Add a separator between hunks
lines.push({ content: [{ text: "" }], type: PatchLineType.SPACER, innerPatchLineType: InnerPatchLineType.NONE });
return { lines };
}
export function hasNonHeaderChanges(contentLines: string[]) {
for (const line of contentLines) {
if (lineHasNonHeaderChange(line)) {
return true;
}
}
return false;
}
const indexHeaderRegex = /^index [0-9a-f]+\.\.[0-9a-f]+ \d+$/;
function lineHasNonHeaderChange(line: string) {
if (!(line.startsWith("+") || line.startsWith("-"))) {
// context line
return false;
}
// Added or removed content
const content = line.substring(1);
// Skip header lines and hunk headers in nested patches
return !(
content.startsWith("+++") ||
content.startsWith("---") ||
content.startsWith("@@ -") ||
content.startsWith("@@ +") ||
content.match(indexHeaderRegex)
);
}
const delimiters = [
" ",
"\t",
"\n",
".",
",",
":",
";",
"(",
")",
"[",
"]",
"{",
"}",
"'",
'"',
"`",
"|",
"&",
"<",
">",
"=",
"+",
"-",
"*",
"/",
"%",
"!",
"?",
"#",
"@",
"^",
"~",
"\\",
"$",
];
function genericTokenize(content: string): string[] {
const tokens: string[] = [];
let currentToken = "";
for (let i = 0; i < content.length; i++) {
const char = content[i];
if (delimiters.includes(char)) {
if (currentToken) {
tokens.push(currentToken);
currentToken = "";
}
tokens.push(char);
} else {
currentToken += char;
}
}
if (currentToken) {
tokens.push(currentToken);
}
return tokens;
}
function hasScope(token: IRawThemeSetting, scope: string) {
return token.scope && (token.scope === scope || token.scope.includes(scope));
}
type ThemeColorQuery = {
// color name in theme.colors
color?: string;
// scope of token to use background color from
bgTokenScope?: string;
// scope of token to use foreground color from
fgTokenScope?: string;
// optional modifier for the color value
modifier?: (value: string | undefined) => string | undefined;
};
function extractColor(theme: ThemeRegistration, opts: ThemeColorQuery): string | undefined {
const colors = theme.colors || {};
const tokenColors = theme.tokenColors || [];
const modifier = opts.modifier || ((v) => v);
const value = opts.color ? colors[opts.color] : null;
if (value) {
return modifier(value);
}
if (opts.bgTokenScope) {
const token = tokenColors.find((t) => hasScope(t, opts.bgTokenScope!) && t.settings.background);
if (token) {
return modifier(token.settings.background);
}
}
if (opts.fgTokenScope) {
const token = tokenColors.find((t) => hasScope(t, opts.fgTokenScope!) && t.settings.foreground);
if (token) {
return modifier(token.settings.foreground);
}
}
return undefined;
}
function makeLCHVars(prefix: string, color: string | undefined, into: Map<string, string | undefined>) {
if (!color) {
return;
}
const oklch = chroma(color).oklch();
into.set(`${prefix}-l`, `${oklch[0]}`);
into.set(`${prefix}-c`, `${oklch[1]}`);
if (isNaN(oklch[2])) {
into.set(`${prefix}-h`, "0");
} else {
into.set(`${prefix}-h`, `${oklch[2]}`);
}
}
function moreChroma(color: string | undefined, value: number = 1) {
if (!color) return undefined;
return chroma(color).saturate(value).css("oklch");
}
function darken(color: string | undefined, value: number = 1) {
if (!color) return undefined;
return chroma(color).darken(value).css("oklch");
}
function makeTransparent(hex: string | undefined) {
if (!hex) return undefined;
const rgb = chroma(hex).rgb();
return `rgba(${rgb[0]}, ${rgb[1]}, ${rgb[2]}, 0.5)`;
}
export async function getBaseColors(themeKey: BundledTheme | undefined, syntaxHighlighting: boolean): Promise<string> {
const theme = await getTheme(themeKey);
if (!syntaxHighlighting || !theme) {
let styles = "";
if (getEffectiveGlobalTheme() === "dark") {
// Make sure tailwind emits these props
// "text-green-600 text-red-600 text-green-700 text-red-700 text-green-800 text-red-800 text-blue-800"
styles += `
--hunk-header-bg-themed: var(--color-gray-800);
--select-bg-themed: var(--color-blue-800);
--inserted-text-bg-themed: var(--color-green-700);
--removed-text-bg-themed: var(--color-red-700);
--inserted-line-bg-themed: var(--color-green-800);
--removed-line-bg-themed: var(--color-red-800);
--inner-inserted-line-bg-themed: var(--color-green-600);
--inner-removed-line-bg-themed: var(--color-red-600);
--inner-inserted-line-fg-themed: var(--color-green-300);
--inner-removed-line-fg-themed: var(--color-red-300);
`;
} else {
styles += `
--inserted-text-bg-themed: var(--color-green-400);
--removed-text-bg-themed: var(--color-red-400);
--inserted-line-bg-themed: var(--color-green-100);
--removed-line-bg-themed: var(--color-red-100);
--inner-inserted-line-bg-themed: var(--color-green-300);
--inner-removed-line-bg-themed: var(--color-red-300);
--inner-inserted-line-fg-themed: var(--color-green-800);
--inner-removed-line-fg-themed: var(--color-red-800);
`;
}
return styles;
}
const tokenColors = theme.default.tokenColors || [];
const style: Map<string, string | undefined> = new Map();
// Find the foreground/default text color for the theme
const foundFg = extractColor(theme.default, { color: "editor.foreground" });
if (foundFg) {
style.set("--editor-fg-themed", foundFg);
makeLCHVars("--editor-foreground", foundFg, style);
} else {
let globalScope = tokenColors.find((t) => t.scope === undefined);
if (!globalScope) {
// Tokenize something to force Shiki to run it's fix for 'broken' themes
await codeToTokens("hi", { theme: theme.default, lang: "text" });
globalScope = tokenColors.find((t) => t.scope === undefined);
}
const globalFg = globalScope?.settings.foreground;
if (globalFg) {
style.set("--editor-fg-themed", globalFg);
makeLCHVars("--editor-foreground", globalFg, style);
} else {
console.error("No foreground color found in theme");
}
}
// These colors are mostly universal
style.set("--editor-bg-themed", extractColor(theme.default, { color: "editor.background" }));
makeLCHVars("--editor-background", style.get("--editor-bg-themed"), style);
style.set("--select-bg-themed", extractColor(theme.default, { color: "editor.selectionBackground" }));
// Don't use these - just add chroma to the inner diff highlight below for consistency
// These are also applied to the line by VSCode...?
// style.set("--inserted-text-bg-themed", extractColor(theme.default, { color: "diffEditor.insertedTextBackground" }));
// style.set("--removed-text-bg-themed", extractColor(theme.default, { color: "diffEditor.removedTextBackground" }));
// 1) Try diffEditor.insertedLineBackground for inserted line highlight color
// 2) Try editorGutter.addedBackground for inserted line highlight color
// 3) Try markup.inserted scope bg for inserted line highlight color
// 4) Try markup.inserted scope fg for inserted line text color
let insertLineBg = extractColor(theme.default, { color: "diffEditor.insertedLineBackground" });
if (!insertLineBg) {
insertLineBg = extractColor(theme.default, { color: "editorGutter.addedBackground", modifier: makeTransparent });
}
if (!insertLineBg) {
insertLineBg = extractColor(theme.default, { bgTokenScope: "markup.inserted", modifier: makeTransparent });
}
if (insertLineBg) {
style.set("--inserted-line-bg-themed", insertLineBg);
style.set("--inner-inserted-line-bg-themed", moreChroma(insertLineBg, 0.5));
style.set("--inserted-text-bg-themed", darken(moreChroma(insertLineBg, 1.25), 0.25));
// Only use the fg color if we have a bg color -- otherwise it will conflict with the top level diff add/remove lines
// Increase chroma to match our adjustments to bg color above
style.set("--inner-inserted-line-fg-themed", moreChroma(extractColor(theme.default, { fgTokenScope: "markup.inserted" })));
} else {
style.set("--inserted-line-fg-themed", extractColor(theme.default, { fgTokenScope: "markup.inserted" }));
}
// 1) Try diffEditor.removedLineBackground for removed line highlight color
// 2) Try editorGutter.deletedBackground for removed line highlight color
// 3) Try markup.deleted scope bg for removed line highlight color
// 4) Try markup.deleted scope fg for removed line text color
let removeLineBg = extractColor(theme.default, { color: "diffEditor.removedLineBackground" });
if (!removeLineBg) {
removeLineBg = extractColor(theme.default, { color: "editorGutter.deletedBackground", modifier: makeTransparent });
}
if (!removeLineBg) {
removeLineBg = extractColor(theme.default, { bgTokenScope: "markup.deleted", modifier: makeTransparent });
}
if (removeLineBg) {
style.set("--removed-line-bg-themed", removeLineBg);
style.set("--inner-removed-line-bg-themed", moreChroma(removeLineBg, 0.5));
style.set("--removed-text-bg-themed", darken(moreChroma(removeLineBg, 1.25), 0.25));
// Only use the fg color if we have a bg color -- otherwise it will conflict with the top level diff add/remove lines
// Increase chroma to match our adjustments to bg color above
style.set("--inner-removed-line-fg-themed", moreChroma(extractColor(theme.default, { fgTokenScope: "markup.deleted" })));
} else {
style.set("--removed-line-fg-themed", extractColor(theme.default, { fgTokenScope: "markup.deleted" }));
}
// One or both of these is often missing, see ConciseDiffView.svelte <style> for fallback behavior
style.set("--hunk-header-bg-themed", extractColor(theme.default, { color: "sideBarSectionHeader.background" }));
style.set("--hunk-header-fg-themed", extractColor(theme.default, { fgTokenScope: "meta.diff.header" }));
let styleString = "";
style.forEach((value, key) => {
if (value) {
styleString += `${key}: ${value};`;
}
});
return styleString;
}
let cachedThemeKey: BundledTheme | undefined = $state(undefined);
let cachedTheme: Promise<null | { default: ThemeRegistration }> | undefined = $state(undefined);
async function getTheme(theme: BundledTheme | undefined): Promise<null | { default: ThemeRegistration }> {
if (!theme) {
return null;
}
if (cachedThemeKey === theme && cachedTheme) {
return cachedTheme;
}
cachedTheme = bundledThemes[theme]();
cachedThemeKey = theme;
return cachedTheme;
}
export class ConciseDiffViewCachedState {
diffViewerPatch: Promise<DiffViewerPatch>;
syntaxHighlighting: boolean;
syntaxHighlightingTheme: BundledTheme | undefined;
omitPatchHeaderOnlyHunks: boolean;
wordDiffs: boolean;
constructor(diffViewerPatch: Promise<DiffViewerPatch>, props: ConciseDiffViewStateProps<unknown>) {
this.diffViewerPatch = diffViewerPatch;
this.syntaxHighlighting = props.syntaxHighlighting.current;
this.syntaxHighlightingTheme = props.syntaxHighlightingTheme.current;
this.omitPatchHeaderOnlyHunks = props.omitPatchHeaderOnlyHunks.current;
this.wordDiffs = props.wordDiffs.current;
}
compatible(props: ConciseDiffViewStateProps<unknown>): boolean {
return (
this.syntaxHighlighting === props.syntaxHighlighting.current &&
this.syntaxHighlightingTheme === props.syntaxHighlightingTheme.current &&
this.omitPatchHeaderOnlyHunks === props.omitPatchHeaderOnlyHunks.current &&