Skip to content

Commit 25af643

Browse files
committed
fixing file editor finding with filepath
1 parent 249205b commit 25af643

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

src/diff/unified-file.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ export class UnifiedFileDiffManager extends BaseUnifiedDiffManager {
100100
return;
101101
}
102102

103+
// HARD GUARD — JupyterLab may have already destroyed the toolbar
104+
if (!toolbar || toolbar.isDisposed || !toolbar.parent) {
105+
return;
106+
}
107+
103108
// Remove and dispose items only if they were added
104109
if (this.showActionButtons) {
105110
// Dispose of the spacer

src/plugin.ts

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ import {
55
import { ICellModel } from '@jupyterlab/cells';
66
import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook';
77
import { ITranslator, nullTranslator } from '@jupyterlab/translation';
8-
import { IEditorTracker } from '@jupyterlab/fileeditor';
8+
import { FileEditor, IEditorTracker } from '@jupyterlab/fileeditor';
99
import { ICellFooterTracker } from 'jupyterlab-cell-input-footer';
10+
import { IDocumentWidget } from '@jupyterlab/docregistry';
1011

1112
import { IDiffWidgetOptions } from './widget';
1213
import { createCodeMirrorSplitDiffWidget } from './diff/cell';
@@ -19,6 +20,7 @@ import {
1920
UnifiedFileDiffManager
2021
} from './diff/unified-file';
2122
import { 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

Comments
 (0)