@@ -19,6 +19,24 @@ import { onDestroy } from "svelte";
1919export const DEFAULT_THEME_LIGHT : BundledTheme = "github-light-default" ;
2020export const DEFAULT_THEME_DARK : BundledTheme = "github-dark-default" ;
2121
22+ export type DiffViewerPatch = {
23+ hunks : DiffViewerPatchHunk [ ] ;
24+ } ;
25+
26+ export type DiffViewerPatchHunk = {
27+ lines : PatchLine [ ] ;
28+ innerPatchHeaderChangesOnly ?: boolean ;
29+ } ;
30+
31+ export type PatchLine = {
32+ type : PatchLineType ;
33+ content : LineSegment [ ] ;
34+ lineBreak ?: boolean ;
35+ innerPatchLineType : InnerPatchLineType ;
36+ oldLineNo ?: number ;
37+ newLineNo ?: number ;
38+ } ;
39+
2240export type LineSegment = {
2341 text ?: string | null ;
2442 iconClass ?: string | null ;
@@ -35,6 +53,12 @@ export enum PatchLineType {
3553 SPACER ,
3654}
3755
56+ export enum InnerPatchLineType {
57+ ADD ,
58+ REMOVE ,
59+ NONE ,
60+ }
61+
3862export type PatchLineTypeProps = {
3963 classes : string ;
4064 lineNoClasses : string ;
@@ -69,12 +93,6 @@ export const patchLineTypeProps: Record<PatchLineType, PatchLineTypeProps> = {
6993 } ,
7094} ;
7195
72- export enum InnerPatchLineType {
73- ADD ,
74- REMOVE ,
75- NONE ,
76- }
77-
7896export type InnerPatchLineTypeProps = {
7997 style : string ;
8098} ;
@@ -99,15 +117,6 @@ export const innerPatchLineTypeProps: Record<InnerPatchLineType, InnerPatchLineT
99117 } ,
100118} ;
101119
102- export type PatchLine = {
103- type : PatchLineType ;
104- content : LineSegment [ ] ;
105- lineBreak ?: boolean ;
106- innerPatchLineType : InnerPatchLineType ;
107- oldLineNo ?: number ;
108- newLineNo ?: number ;
109- } ;
110-
111120const joiner = "\uE000" ;
112121const noTrailingNewlineMarker : string = joiner + joiner + [ "PATCH" , "ROULETTE" , "NO" , "TRAILING" , "NEWLINE" , "MARKER" ] . join ( joiner ) + joiner + joiner ;
113122
@@ -613,40 +622,39 @@ async function withLineProcessor<R>(fn: (proc: LineProcessor) => Promise<R>): Pr
613622 }
614623}
615624
616- export async function makeLines (
625+ export async function parseDiffViewerPatch (
617626 patchPromise : StructuredPatch | Promise < StructuredPatch > ,
618627 syntaxHighlighting : boolean ,
619628 syntaxHighlightingTheme : BundledTheme | undefined ,
620629 omitPatchHeaderOnlyHunks : boolean ,
621630 wordDiffs : boolean ,
622- ) : Promise < PatchLine [ ] [ ] > {
631+ ) : Promise < DiffViewerPatch > {
623632 const patch = await patchPromise ;
624- const lines : PatchLine [ ] [ ] = [ ] ;
633+ const hunks : DiffViewerPatchHunk [ ] = [ ] ;
625634
626635 for ( let i = 0 ; i < patch . hunks . length ; i ++ ) {
627636 const hunk = patch . hunks [ i ] ;
628- const hunkLines = await makeHunkLines ( patch , hunk , syntaxHighlighting , syntaxHighlightingTheme , omitPatchHeaderOnlyHunks , wordDiffs ) ;
629- lines . push ( hunkLines ) ;
637+ hunks . push ( await makeHunk ( patch , hunk , syntaxHighlighting , syntaxHighlightingTheme , omitPatchHeaderOnlyHunks , wordDiffs ) ) ;
630638 }
631639
632- return lines ;
640+ return { hunks } ;
633641}
634642
635- async function makeHunkLines (
643+ async function makeHunk (
636644 patch : StructuredPatch ,
637645 hunk : StructuredPatchHunk ,
638646 syntaxHighlighting : boolean ,
639647 syntaxHighlightingTheme : BundledTheme | undefined ,
640648 omitPatchHeaderOnlyHunks : boolean ,
641649 wordDiffs : boolean ,
642- ) : Promise < PatchLine [ ] > {
643- const lines : PatchLine [ ] = [ ] ;
644-
650+ ) : Promise < DiffViewerPatchHunk > {
645651 // Skip this hunk if it only contains header changes
646652 if ( omitPatchHeaderOnlyHunks && ! hasNonHeaderChanges ( hunk . lines ) ) {
647- return lines ;
653+ return { innerPatchHeaderChangesOnly : true , lines : [ ] } ;
648654 }
649655
656+ const lines : PatchLine [ ] = [ ] ;
657+
650658 // Add the hunk header
651659 const header = `@@ -${ hunk . oldStart } ,${ hunk . oldLines } +${ hunk . newStart } ,${ hunk . newLines } @@` ;
652660 lines . push ( {
@@ -665,7 +673,7 @@ async function makeHunkLines(
665673 // Add a separator between hunks
666674 lines . push ( { content : [ { text : "" } ] , type : PatchLineType . SPACER , innerPatchLineType : InnerPatchLineType . NONE } ) ;
667675
668- return lines ;
676+ return { lines } ;
669677}
670678
671679export function hasNonHeaderChanges ( contentLines : string [ ] ) {
@@ -971,14 +979,14 @@ async function getTheme(theme: BundledTheme | undefined): Promise<null | { defau
971979}
972980
973981export class ConciseDiffViewCachedState {
974- patchLines : Promise < PatchLine [ ] [ ] > ;
982+ diffViewerPatch : Promise < DiffViewerPatch > ;
975983 syntaxHighlighting : boolean ;
976984 syntaxHighlightingTheme : BundledTheme | undefined ;
977985 omitPatchHeaderOnlyHunks : boolean ;
978986 wordDiffs : boolean ;
979987
980- constructor ( patchLines : Promise < PatchLine [ ] [ ] > , props : ConciseDiffViewStateProps < unknown > ) {
981- this . patchLines = patchLines ;
988+ constructor ( diffViewerPatch : Promise < DiffViewerPatch > , props : ConciseDiffViewStateProps < unknown > ) {
989+ this . diffViewerPatch = diffViewerPatch ;
982990 this . syntaxHighlighting = props . syntaxHighlighting . current ;
983991 this . syntaxHighlightingTheme = props . syntaxHighlightingTheme . current ;
984992 this . omitPatchHeaderOnlyHunks = props . omitPatchHeaderOnlyHunks . current ;
@@ -1034,8 +1042,9 @@ export type ConciseDiffViewStateProps<K> = ReadableBoxedValues<{
10341042} > ;
10351043
10361044export class ConciseDiffViewState < K > {
1037- patchLines : Promise < PatchLine [ ] [ ] > = $state ( new Promise < PatchLine [ ] [ ] > ( ( ) => [ ] ) ) ;
1045+ diffViewerPatch : Promise < DiffViewerPatch > = $state ( new Promise < DiffViewerPatch > ( ( ) => [ ] ) ) ;
10381046 cachedState : ConciseDiffViewCachedState | undefined = undefined ;
1047+ rootStyle : Promise < string > = $state ( new Promise < string > ( ( ) => [ ] ) ) ;
10391048
10401049 private readonly props : ConciseDiffViewStateProps < K > ;
10411050
@@ -1046,6 +1055,18 @@ export class ConciseDiffViewState<K> {
10461055 this . update ( ) ;
10471056 } ) ;
10481057
1058+ $effect ( ( ) => {
1059+ const promise = getBaseColors ( this . props . syntaxHighlightingTheme . current , this . props . syntaxHighlighting . current ) ;
1060+ promise . then (
1061+ ( ) => {
1062+ this . rootStyle = promise ;
1063+ } ,
1064+ ( ) => {
1065+ this . rootStyle = promise ;
1066+ } ,
1067+ ) ;
1068+ } ) ;
1069+
10491070 onDestroy ( ( ) => {
10501071 if ( this . props . cache . current !== undefined && this . props . cacheKey . current !== undefined && this . cachedState !== undefined ) {
10511072 this . props . cache . current . set ( this . props . cacheKey . current , this . cachedState ) ;
@@ -1068,7 +1089,7 @@ export class ConciseDiffViewState<K> {
10681089 }
10691090 }
10701091
1071- const promise = makeLines (
1092+ const promise = parseDiffViewerPatch (
10721093 this . props . patch . current ,
10731094 this . props . syntaxHighlighting . current ,
10741095 this . props . syntaxHighlightingTheme . current ,
@@ -1079,17 +1100,17 @@ export class ConciseDiffViewState<K> {
10791100 promise . then (
10801101 ( ) => {
10811102 // Don't replace a potentially completed promise with a pending one, wait until the replacement is ready for smooth transitions
1082- this . patchLines = promise ;
1103+ this . diffViewerPatch = promise ;
10831104 } ,
10841105 ( ) => {
10851106 // Propagate errors
1086- this . patchLines = promise ;
1107+ this . diffViewerPatch = promise ;
10871108 } ,
10881109 ) ;
10891110 }
10901111
10911112 restore ( state : ConciseDiffViewCachedState ) {
1092- this . patchLines = state . patchLines ;
1113+ this . diffViewerPatch = state . diffViewerPatch ;
10931114 this . cachedState = state ;
10941115 }
10951116}
0 commit comments