From 555da878f4af33027b3bc015968d4534af0703d3 Mon Sep 17 00:00:00 2001 From: Akos Kitta Date: Tue, 7 Jun 2022 14:51:07 +0200 Subject: [PATCH] Editor manager should be singleton. Added some logging when filtering the layout data. Signed-off-by: Akos Kitta --- .../browser/arduino-ide-frontend-module.ts | 2 ++ .../theia/core/shell-layout-restorer.ts | 24 ++++++++++++++++++- .../browser/theia/editor/editor-manager.ts | 2 ++ 3 files changed, 27 insertions(+), 1 deletion(-) diff --git a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts index 9289218b..9ef3c242 100644 --- a/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts +++ b/arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts @@ -541,6 +541,8 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => { bind(SearchInWorkspaceWidget).toSelf(); rebind(TheiaSearchInWorkspaceWidget).toService(SearchInWorkspaceWidget); + // Disabled reference counter in the editor manager to avoid opening the same editor (with different opener options) multiple times. + bind(EditorManager).toSelf().inSingletonScope(); rebind(TheiaEditorManager).to(EditorManager); // replace search icon diff --git a/arduino-ide-extension/src/browser/theia/core/shell-layout-restorer.ts b/arduino-ide-extension/src/browser/theia/core/shell-layout-restorer.ts index bdf7037c..c002b8e1 100644 --- a/arduino-ide-extension/src/browser/theia/core/shell-layout-restorer.ts +++ b/arduino-ide-extension/src/browser/theia/core/shell-layout-restorer.ts @@ -1,9 +1,16 @@ -import { injectable } from '@theia/core/shared/inversify'; +import { inject, injectable } from '@theia/core/shared/inversify'; import { FrontendApplication } from '@theia/core/lib/browser/frontend-application'; import { ShellLayoutRestorer as TheiaShellLayoutRestorer } from '@theia/core/lib/browser/shell/shell-layout-restorer'; +import { EditorManager } from '@theia/editor/lib/browser'; @injectable() export class ShellLayoutRestorer extends TheiaShellLayoutRestorer { + // The editor manager is unused in the layout restorer. + // We inject the editor manager to achieve better logging when filtering duplicate editor tabs. + // Feel free to remove it in later IDE2 releases if the duplicate editor tab issues do not occur anymore. + @inject(EditorManager) + private readonly editorManager: EditorManager; + // Workaround for https://github.com/eclipse-theia/theia/issues/6579. async storeLayoutAsync(app: FrontendApplication): Promise { if (this.shouldStoreLayout) { @@ -35,6 +42,9 @@ export class ShellLayoutRestorer extends TheiaShellLayoutRestorer { const layoutData = await this.inflate(serializedLayoutData); // workaround to remove duplicated tabs + console.log( + '>>> Filtering persisted layout data to eliminate duplicate editor tabs...' + ); const filesUri: string[] = []; if ((layoutData as any)?.mainPanel?.main?.widgets) { (layoutData as any).mainPanel.main.widgets = ( @@ -42,14 +52,26 @@ export class ShellLayoutRestorer extends TheiaShellLayoutRestorer { ).mainPanel.main.widgets.filter((widget: any) => { const uri = widget.getResourceUri().toString(); if (filesUri.includes(uri)) { + console.log(`[SKIP]: Already visited editor URI: '${uri}'.`); return false; } + console.log(`[OK]: Visited editor URI: '${uri}'.`); filesUri.push(uri); return true; }); } + console.log('<<< Filtered the layout data before restoration.'); await app.shell.setLayoutData(layoutData); + const allOpenedEditors = this.editorManager.all; + // If any editor was visited during the layout data filtering, + // but the editor manager does not know about opened editors, then + // the IDE2 will show duplicate editors. + if (filesUri.length && !allOpenedEditors.length) { + console.warn( + 'Inconsistency detected between the editor manager and the restored layout data. Editors were detected to be open in the layout data from the previous session, but the editor manager does not know about the opened editor.' + ); + } this.logger.info('<<< The layout has been successfully restored.'); return true; } diff --git a/arduino-ide-extension/src/browser/theia/editor/editor-manager.ts b/arduino-ide-extension/src/browser/theia/editor/editor-manager.ts index 245fdca3..835715f2 100644 --- a/arduino-ide-extension/src/browser/theia/editor/editor-manager.ts +++ b/arduino-ide-extension/src/browser/theia/editor/editor-manager.ts @@ -1,5 +1,7 @@ +import { injectable } from '@theia/core/shared/inversify'; import { EditorManager as TheiaEditorManager } from '@theia/editor/lib/browser/editor-manager'; +@injectable() export class EditorManager extends TheiaEditorManager { protected override getOrCreateCounterForUri(): number { return 0;