From 25af6430c502c9838d11518bd97f9bf939ede5d1 Mon Sep 17 00:00:00 2001 From: Nakul Date: Mon, 17 Nov 2025 17:53:15 +0530 Subject: [PATCH] fixing file editor finding with filepath --- src/diff/unified-file.ts | 5 +++++ src/plugin.ts | 35 +++++++++++++++++++++++++++-------- 2 files changed, 32 insertions(+), 8 deletions(-) diff --git a/src/diff/unified-file.ts b/src/diff/unified-file.ts index 4677465..c7b6bfc 100644 --- a/src/diff/unified-file.ts +++ b/src/diff/unified-file.ts @@ -100,6 +100,11 @@ export class UnifiedFileDiffManager extends BaseUnifiedDiffManager { return; } + // HARD GUARD — JupyterLab may have already destroyed the toolbar + if (!toolbar || toolbar.isDisposed || !toolbar.parent) { + return; + } + // Remove and dispose items only if they were added if (this.showActionButtons) { // Dispose of the spacer diff --git a/src/plugin.ts b/src/plugin.ts index 07d042e..cb0e882 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -5,8 +5,9 @@ import { import { ICellModel } from '@jupyterlab/cells'; import { INotebookTracker, NotebookPanel } from '@jupyterlab/notebook'; import { ITranslator, nullTranslator } from '@jupyterlab/translation'; -import { IEditorTracker } from '@jupyterlab/fileeditor'; +import { FileEditor, IEditorTracker } from '@jupyterlab/fileeditor'; import { ICellFooterTracker } from 'jupyterlab-cell-input-footer'; +import { IDocumentWidget } from '@jupyterlab/docregistry'; import { IDiffWidgetOptions } from './widget'; import { createCodeMirrorSplitDiffWidget } from './diff/cell'; @@ -19,6 +20,7 @@ import { UnifiedFileDiffManager } from './diff/unified-file'; import { CodeMirrorEditor } from '@jupyterlab/codemirror'; +import { PathExt } from '@jupyterlab/coreutils'; /** * The translation namespace for the plugin. @@ -369,19 +371,36 @@ const unifiedFileDiffPlugin: JupyterFrontEndPlugin = { } // Try to find the file editor widget by its filepath using IEditorTracker - let fileEditorWidget = editorTracker.currentWidget; + let fileEditorWidget: IDocumentWidget | null = null; + + // Look for a matching open file if (filePath) { - // Search through all open file editors in the tracker - const fileEditors = editorTracker.find(widget => { - return widget.context?.path === filePath; + editorTracker.forEach(widget => { + const widgetPath = widget.context.path; + if (PathExt.basename(widgetPath) === PathExt.basename(filePath)) { + fileEditorWidget = widget; + } }); - if (fileEditors) { - fileEditorWidget = fileEditors; + + // If not opened, try opening it + if (!fileEditorWidget) { + try { + fileEditorWidget = await app.commands.execute( + 'filebrowser:open-path', + { path: filePath } + ); + } catch (err) { + console.error('Failed to open file:', err); + } } } - // If no specific file editor found, try to get the current widget from the tracker + // If still null → fallback only if user did NOT pass filePath if (!fileEditorWidget) { + if (filePath) { + console.error(`No open editor for path: ${filePath}`); + return; + } fileEditorWidget = editorTracker.currentWidget; }