55import { ICellModel } from '@jupyterlab/cells' ;
66import { INotebookTracker , NotebookPanel } from '@jupyterlab/notebook' ;
77import { ITranslator , nullTranslator } from '@jupyterlab/translation' ;
8- import { IEditorTracker } from '@jupyterlab/fileeditor' ;
8+ import { FileEditor , IEditorTracker } from '@jupyterlab/fileeditor' ;
99import { ICellFooterTracker } from 'jupyterlab-cell-input-footer' ;
10+ import { IDocumentWidget } from '@jupyterlab/docregistry' ;
1011
1112import { IDiffWidgetOptions } from './widget' ;
1213import { createCodeMirrorSplitDiffWidget } from './diff/cell' ;
@@ -19,6 +20,7 @@ import {
1920 UnifiedFileDiffManager
2021} from './diff/unified-file' ;
2122import { CodeMirrorEditor } from '@jupyterlab/codemirror' ;
23+ import { PathExt } from '@jupyterlab/coreutils' ;
2224
2325/**
2426 * The translation namespace for the plugin.
@@ -369,19 +371,36 @@ const unifiedFileDiffPlugin: JupyterFrontEndPlugin<void> = {
369371 }
370372
371373 // Try to find the file editor widget by its filepath using IEditorTracker
372- let fileEditorWidget = editorTracker . currentWidget ;
374+ let fileEditorWidget : IDocumentWidget < FileEditor > | null = null ;
375+
376+ // Look for a matching open file
373377 if ( filePath ) {
374- // Search through all open file editors in the tracker
375- const fileEditors = editorTracker . find ( widget => {
376- return widget . context ?. path === filePath ;
378+ editorTracker . forEach ( widget => {
379+ const widgetPath = widget . context . path ;
380+ if ( PathExt . basename ( widgetPath ) === PathExt . basename ( filePath ) ) {
381+ fileEditorWidget = widget ;
382+ }
377383 } ) ;
378- if ( fileEditors ) {
379- fileEditorWidget = fileEditors ;
384+
385+ // If not opened, try opening it
386+ if ( ! fileEditorWidget ) {
387+ try {
388+ fileEditorWidget = await app . commands . execute (
389+ 'filebrowser:open-path' ,
390+ { path : filePath }
391+ ) ;
392+ } catch ( err ) {
393+ console . error ( 'Failed to open file:' , err ) ;
394+ }
380395 }
381396 }
382397
383- // If no specific file editor found, try to get the current widget from the tracker
398+ // If still null → fallback only if user did NOT pass filePath
384399 if ( ! fileEditorWidget ) {
400+ if ( filePath ) {
401+ console . error ( `No open editor for path: ${ filePath } ` ) ;
402+ return ;
403+ }
385404 fileEditorWidget = editorTracker . currentWidget ;
386405 }
387406
0 commit comments