@@ -432,31 +432,90 @@ export class DiffViewProvider {
432432
433433 // Open new diff editor.
434434 return new Promise < vscode . TextEditor > ( ( resolve , reject ) => {
435- const fileName = path . basename ( uri . fsPath )
436- const fileExists = this . editType === "modify"
437-
438- const disposable = vscode . window . onDidChangeActiveTextEditor ( ( editor ) => {
439- if ( editor && arePathsEqual ( editor . document . uri . fsPath , uri . fsPath ) ) {
440- disposable . dispose ( )
441- resolve ( editor )
435+ ; ( async ( ) => {
436+ const fileName = path . basename ( uri . fsPath )
437+ const fileExists = this . editType === "modify"
438+ let timeoutId : NodeJS . Timeout | undefined
439+
440+ console . log ( `[DiffViewProvider] Attempting to open diff editor for: ${ uri . fsPath } ` )
441+
442+ const checkAndResolve = ( ) => {
443+ for ( const group of vscode . window . tabGroups . all ) {
444+ for ( const tab of group . tabs ) {
445+ if (
446+ tab . input instanceof vscode . TabInputTextDiff &&
447+ tab . input ?. original ?. scheme === DIFF_VIEW_URI_SCHEME &&
448+ arePathsEqual ( tab . input . modified . fsPath , uri . fsPath )
449+ ) {
450+ // Found the diff editor, now try to show it to get the TextEditor instance
451+ vscode . window . showTextDocument ( tab . input . modified , { preserveFocus : true } ) . then (
452+ ( editor ) => {
453+ console . log (
454+ `[DiffViewProvider] Diff editor found and activated via tabGroups: ${ editor . document . uri . fsPath } ` ,
455+ )
456+ if ( timeoutId ) clearTimeout ( timeoutId )
457+ disposableTabGroup . dispose ( )
458+ resolve ( editor )
459+ } ,
460+ ( err ) => {
461+ console . error (
462+ `[DiffViewProvider] Error showing text document after finding tab: ${ err } ` ,
463+ )
464+ if ( timeoutId ) clearTimeout ( timeoutId )
465+ disposableTabGroup . dispose ( )
466+ reject (
467+ new Error ( `Failed to show diff editor after finding tab: ${ err . message } ` ) ,
468+ )
469+ } ,
470+ )
471+ return true
472+ }
473+ }
474+ }
475+ return false
442476 }
443- } )
444-
445- vscode . commands . executeCommand (
446- "vscode.diff" ,
447- vscode . Uri . parse ( `${ DIFF_VIEW_URI_SCHEME } :${ fileName } ` ) . with ( {
448- query : Buffer . from ( this . originalContent ?? "" ) . toString ( "base64" ) ,
449- } ) ,
450- uri ,
451- `${ fileName } : ${ fileExists ? "Original ↔ Roo's Changes" : "New File" } (Editable)` ,
452- { preserveFocus : true } ,
453- )
454477
455- // This may happen on very slow machines i.e. project idx.
456- setTimeout ( ( ) => {
457- disposable . dispose ( )
458- reject ( new Error ( "Failed to open diff editor, please try again..." ) )
459- } , 10_000 )
478+ // Listen for changes in tab groups, which includes tabs moving between windows
479+ const disposableTabGroup = vscode . window . tabGroups . onDidChangeTabGroups ( ( ) => {
480+ console . log ( `[DiffViewProvider] onDidChangeTabGroups fired. Checking for diff editor.` )
481+ checkAndResolve ( )
482+ } )
483+
484+ vscode . commands
485+ . executeCommand (
486+ "vscode.diff" ,
487+ vscode . Uri . parse ( `${ DIFF_VIEW_URI_SCHEME } :${ fileName } ` ) . with ( {
488+ query : Buffer . from ( this . originalContent ?? "" ) . toString ( "base64" ) ,
489+ } ) ,
490+ uri ,
491+ `${ fileName } : ${ fileExists ? "Original ↔ Roo's Changes" : "New File" } (Editable)` ,
492+ { preserveFocus : true } ,
493+ )
494+ . then (
495+ async ( ) => {
496+ console . log ( `[DiffViewProvider] vscode.diff command executed for: ${ uri . fsPath } ` )
497+ // Give a brief moment for the editor to appear in tab groups
498+ await new Promise ( ( r ) => setTimeout ( r , 100 ) )
499+ if ( ! checkAndResolve ( ) ) {
500+ console . log (
501+ `[DiffViewProvider] Diff editor not immediately found after command. Waiting for tab group changes.` ,
502+ )
503+ }
504+ } ,
505+ ( err ) => {
506+ console . error ( `[DiffViewProvider] Error executing vscode.diff command: ${ err } ` )
507+ if ( timeoutId ) clearTimeout ( timeoutId )
508+ disposableTabGroup . dispose ( )
509+ reject ( new Error ( `Failed to open diff editor command: ${ err . message } ` ) )
510+ } ,
511+ )
512+
513+ timeoutId = setTimeout ( ( ) => {
514+ console . log ( `[DiffViewProvider] Diff editor open timeout triggered for: ${ uri . fsPath } ` )
515+ disposableTabGroup . dispose ( )
516+ reject ( new Error ( "Failed to open diff editor, please try again..." ) )
517+ } , 10_000 )
518+ } ) ( )
460519 } )
461520 }
462521
0 commit comments