Fixed widget lookup to eliminate duplicate tabs.

- Removed `@theia/editor-preview`,
 - Patched opener options when repairing layout on start, and
 - Compare widget keys with deepEquals instead of string equal.

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
This commit is contained in:
Akos Kitta
2022-06-20 16:15:03 +02:00
committed by Akos Kitta
parent 083337de1c
commit 84109e416a
8 changed files with 217 additions and 42 deletions

View File

@@ -2,10 +2,13 @@ import { notEmpty } from '@theia/core';
import { WidgetDescription } from '@theia/core/lib/browser';
import { ShellLayoutRestorer as TheiaShellLayoutRestorer } from '@theia/core/lib/browser/shell/shell-layout-restorer';
import { injectable } from '@theia/core/shared/inversify';
import { EditorPreviewWidgetFactory } from '@theia/editor-preview/lib/browser/editor-preview-widget-factory';
import { EditorWidgetFactory } from '@theia/editor/lib/browser/editor-widget-factory';
import { FrontendApplication } from './frontend-application';
namespace EditorPreviewWidgetFactory {
export const ID = 'editor-preview-widget'; // The factory ID must be a hard-coded string because IDE2 does not depend on `@theia/editor-preview`.
}
@injectable()
export class ShellLayoutRestorer extends TheiaShellLayoutRestorer {
override async restoreLayout(app: FrontendApplication): Promise<boolean> {
@@ -160,8 +163,8 @@ export class ShellLayoutRestorer extends TheiaShellLayoutRestorer {
constructionOptions: {
factoryId: EditorWidgetFactory.ID,
options: {
uri,
kind: 'navigatable',
uri,
counter: 0,
},
},

View File

@@ -0,0 +1,48 @@
import type { MaybePromise } from '@theia/core';
import type { Widget } from '@theia/core/lib/browser';
import { WidgetManager as TheiaWidgetManager } from '@theia/core/lib/browser/widget-manager';
import { injectable } from '@theia/core/shared/inversify';
import deepEqual = require('deep-equal');
@injectable()
export class WidgetManager extends TheiaWidgetManager {
/**
* Customized to find any existing widget based on `options` deepEquals instead of string equals.
* See https://github.com/eclipse-theia/theia/issues/11309.
*/
protected override doGetWidget<T extends Widget>(
key: string
): MaybePromise<T> | undefined {
const pendingWidget = this.findExistingWidget<T>(key);
if (pendingWidget) {
return pendingWidget as MaybePromise<T>;
}
return undefined;
}
private findExistingWidget<T extends Widget>(
key: string
): MaybePromise<T> | undefined {
const parsed = this.parseJson(key);
for (const [candidateKey, widget] of [
...this.widgetPromises.entries(),
...this.pendingWidgetPromises.entries(),
]) {
const candidate = this.parseJson(candidateKey);
if (deepEqual(candidate, parsed)) {
return widget as MaybePromise<T>;
}
}
return undefined;
}
// eslint-disable-next-line @typescript-eslint/no-explicit-any
private parseJson(json: string): any {
try {
return JSON.parse(json);
} catch (err) {
console.log(`Failed to parse JSON: <${json}>.`, err);
throw err;
}
}
}

View File

@@ -1,21 +1,13 @@
import { injectable } from '@theia/core/shared/inversify';
import { EditorPreviewContribution as TheiaEditorPreviewContribution } from '@theia/editor-preview/lib/browser/editor-preview-contribution';
import { TextEditor } from '@theia/editor/lib/browser';
import { EditorContribution as TheiaEditorContribution } from '@theia/editor/lib/browser/editor-contribution';
@injectable()
export class EditorPreviewContribution extends TheiaEditorPreviewContribution {
protected updateLanguageStatus(editor: TextEditor | undefined): void {}
// protected setCursorPositionStatus(editor: TextEditor | undefined): void {
// if (!editor) {
// this.statusBar.removeElement('editor-status-cursor-position');
// return;
// }
// const { cursor } = editor;
// this.statusBar.setElement('editor-status-cursor-position', {
// text: `${cursor.line + 1}`,
// alignment: StatusBarAlignment.LEFT,
// priority: 100,
// });
// }
export class EditorContribution extends TheiaEditorContribution {
protected override updateLanguageStatus(
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-vars
editor: TextEditor | undefined
): void {
// NOOP
}
}