@@ -43,6 +43,7 @@ export interface EditorOptions {
4343
4444export interface EditorState {
4545 code : Code ;
46+ originalCode ?: Code ;
4647 offset : number ;
4748 selection : Selection | null ;
4849 runLines : number [ ] ;
@@ -94,7 +95,7 @@ export class AnycodeEditor {
9495 private diffEnabled : boolean = false ;
9596 private focusedDiffEnabled : boolean ;
9697 private focusedDiffContextLines : number ;
97- private originalCode ?: string ;
98+ private originalCode ?: Code ;
9899 private diffs ?: Map < number , DiffInfo > ;
99100 private readonly readOnly : boolean ;
100101
@@ -118,12 +119,6 @@ export class AnycodeEditor {
118119
119120 this . settings = { lineHeight : 20 , buffer : 30 } ;
120121
121- if ( this . diffEnabled ) {
122- this . originalCode = initialText ;
123- const currentText = this . code . getContent ( ) ;
124- this . diffs = computeGitChanges ( this . originalCode , currentText ) ;
125- }
126-
127122 const theme = options . theme || vesper ;
128123 const css = generateCssClasses ( theme ) ;
129124 addCssToDocument ( css , 'anyeditor-theme' ) ;
@@ -261,6 +256,33 @@ export class AnycodeEditor {
261256 this . setupEventListeners ( ) ;
262257 }
263258
259+ private async initOriginalCode ( content : string ) : Promise < boolean > {
260+ if ( this . originalCode ?. getContent ( ) === content ) {
261+ return false ;
262+ }
263+ const originalCode = new Code (
264+ content ,
265+ this . code . filename ,
266+ this . code . language ?? 'text' ,
267+ ) ;
268+ this . originalCode = originalCode ;
269+
270+ try {
271+ await originalCode . init ( ) ;
272+ // Ignore stale async completion if a newer baseline replaced this instance.
273+ if ( this . originalCode !== originalCode ) return false ;
274+ this . originalCode = originalCode ;
275+ return true ;
276+ } catch ( error ) {
277+ // Don't wipe newer baseline on stale failure.
278+ if ( this . originalCode === originalCode ) {
279+ this . originalCode = undefined ;
280+ }
281+ console . warn ( 'Failed to initialize original code for diff rendering' , error ) ;
282+ return false ;
283+ }
284+ }
285+
264286 private setupReadOnlyEventListeners ( ) {
265287 this . handleScroll = this . handleScroll . bind ( this ) ;
266288 this . container . addEventListener ( "scroll" , this . handleScroll ) ;
@@ -472,6 +494,7 @@ export class AnycodeEditor {
472494 private getEditorState ( ) : EditorState {
473495 return {
474496 code : this . code ,
497+ originalCode : this . originalCode ,
475498 offset : this . offset ,
476499 selection : this . selection ,
477500 runLines : this . runLines ,
@@ -911,7 +934,6 @@ export class AnycodeEditor {
911934 }
912935
913936 private async handleKeydown ( event : KeyboardEvent ) {
914- console . log ( 'keydown' , event ) ;
915937 this . clearPendingHover ( ) ;
916938 this . closeHover ( ) ;
917939
@@ -1497,8 +1519,14 @@ export class AnycodeEditor {
14971519 this . diffEnabled = enabled ;
14981520 this . renderer . setDiffEnabled ( enabled ) ;
14991521
1500- if ( enabled && this . originalCode === undefined ) {
1501- this . originalCode = this . code . getContent ( ) ;
1522+ if ( enabled ) {
1523+ const baseline = this . originalCode ?. getContent ( ) ?? this . code . getContent ( ) ;
1524+ void this . initOriginalCode ( baseline ) . then ( ( updated ) => {
1525+ if ( ! this . diffEnabled || ! updated ) return ;
1526+ this . recomputeDiffs ( ) ;
1527+ this . renderer . render ( this . getEditorState ( ) , this . search ) ;
1528+ this . verifyDiffRendering ( ) ;
1529+ } ) ;
15021530 }
15031531
15041532 this . recomputeDiffs ( ) ;
@@ -1523,17 +1551,17 @@ export class AnycodeEditor {
15231551 }
15241552
15251553 public setOriginalCode ( content : string ) : void {
1526- this . originalCode = content ;
1527- if ( this . diffEnabled ) {
1554+ void this . initOriginalCode ( content ) . then ( ( updated ) => {
1555+ if ( ! this . diffEnabled || ! updated ) return ;
15281556 this . recomputeDiffs ( ) ;
15291557 this . renderer . render ( this . getEditorState ( ) , this . search ) ;
15301558 this . verifyDiffRendering ( ) ;
1531- }
1559+ } ) ;
15321560 }
15331561
15341562 private recomputeDiffs ( ) : void {
1535- if ( this . diffEnabled && this . originalCode !== undefined ) {
1536- this . diffs = computeGitChanges ( this . originalCode , this . code . getContent ( ) ) ;
1563+ if ( this . diffEnabled && this . originalCode ) {
1564+ this . diffs = computeGitChanges ( this . originalCode . getContent ( ) , this . code . getContent ( ) ) ;
15371565 } else {
15381566 this . diffs = undefined ;
15391567 }
0 commit comments