Skip to content

Commit 05052e4

Browse files
author
Akos Kitta
committed
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 <[email protected]>
1 parent a715da3 commit 05052e4

File tree

8 files changed

+217
-42
lines changed

8 files changed

+217
-42
lines changed

Diff for: arduino-ide-extension/package.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
"@theia/application-package": "1.25.0",
2626
"@theia/core": "1.25.0",
2727
"@theia/editor": "1.25.0",
28-
"@theia/editor-preview": "1.25.0",
2928
"@theia/electron": "1.25.0",
3029
"@theia/filesystem": "1.25.0",
3130
"@theia/keymaps": "1.25.0",
@@ -43,6 +42,7 @@
4342
"@types/auth0-js": "^9.14.0",
4443
"@types/btoa": "^1.2.3",
4544
"@types/dateformat": "^3.0.1",
45+
"@types/deep-equal": "^1.0.1",
4646
"@types/deepmerge": "^2.2.0",
4747
"@types/glob": "^7.2.0",
4848
"@types/google-protobuf": "^3.7.2",
@@ -63,6 +63,7 @@
6363
"auth0-js": "^9.14.0",
6464
"btoa": "^1.2.1",
6565
"dateformat": "^3.0.3",
66+
"deep-equal": "^2.0.5",
6667
"deepmerge": "2.0.1",
6768
"electron-updater": "^4.6.5",
6869
"fast-safe-stringify": "^2.1.1",

Diff for: arduino-ide-extension/src/browser/arduino-ide-frontend-module.ts

+9-5
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,8 @@ import { FileNavigatorContribution as TheiaFileNavigatorContribution } from '@th
4242
import { KeymapsFrontendContribution } from './theia/keymaps/keymaps-frontend-contribution';
4343
import { KeymapsFrontendContribution as TheiaKeymapsFrontendContribution } from '@theia/keymaps/lib/browser/keymaps-frontend-contribution';
4444
import { ArduinoToolbarContribution } from './toolbar/arduino-toolbar-contribution';
45-
import { EditorPreviewContribution as TheiaEditorPreviewContribution } from '@theia/editor-preview/lib/browser/editor-preview-contribution';
46-
import { EditorPreviewContribution } from './theia/editor/editor-contribution';
45+
import { EditorContribution as TheiaEditorContribution } from '@theia/editor/lib/browser/editor-contribution';
46+
import { EditorContribution } from './theia/editor/editor-contribution';
4747
import { MonacoStatusBarContribution as TheiaMonacoStatusBarContribution } from '@theia/monaco/lib/browser/monaco-status-bar-contribution';
4848
import { MonacoStatusBarContribution } from './theia/monaco/monaco-status-bar-contribution';
4949
import {
@@ -298,6 +298,8 @@ import {
298298
} from '../common/protocol/survey-service';
299299
import { WindowContribution } from './theia/core/window-contribution';
300300
import { WindowContribution as TheiaWindowContribution } from '@theia/core/lib/browser/window-contribution';
301+
import { WidgetManager } from './theia/core/widget-manager';
302+
import { WidgetManager as TheiaWidgetManager } from '@theia/core/lib/browser/widget-manager';
301303

302304
MonacoThemingService.register({
303305
id: 'arduino-theme',
@@ -506,9 +508,7 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
506508
rebind(TheiaKeymapsFrontendContribution)
507509
.to(KeymapsFrontendContribution)
508510
.inSingletonScope();
509-
rebind(TheiaEditorPreviewContribution)
510-
.to(EditorPreviewContribution)
511-
.inSingletonScope();
511+
rebind(TheiaEditorContribution).to(EditorContribution).inSingletonScope();
512512
rebind(TheiaMonacoStatusBarContribution)
513513
.to(MonacoStatusBarContribution)
514514
.inSingletonScope();
@@ -787,6 +787,10 @@ export default new ContainerModule((bind, unbind, isBound, rebind) => {
787787
bind(DebugConfigurationManager).toSelf().inSingletonScope();
788788
rebind(TheiaDebugConfigurationManager).toService(DebugConfigurationManager);
789789

790+
// To avoid duplicate tabs use deepEqual instead of string equal: https://github.com/eclipse-theia/theia/issues/11309
791+
bind(WidgetManager).toSelf().inSingletonScope();
792+
rebind(TheiaWidgetManager).toService(WidgetManager);
793+
790794
// Preferences
791795
bindArduinoPreferences(bind);
792796

Diff for: arduino-ide-extension/src/browser/theia/core/shell-layout-restorer.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ import { notEmpty } from '@theia/core';
22
import { WidgetDescription } from '@theia/core/lib/browser';
33
import { ShellLayoutRestorer as TheiaShellLayoutRestorer } from '@theia/core/lib/browser/shell/shell-layout-restorer';
44
import { injectable } from '@theia/core/shared/inversify';
5-
import { EditorPreviewWidgetFactory } from '@theia/editor-preview/lib/browser/editor-preview-widget-factory';
65
import { EditorWidgetFactory } from '@theia/editor/lib/browser/editor-widget-factory';
76
import { FrontendApplication } from './frontend-application';
87

8+
namespace EditorPreviewWidgetFactory {
9+
export const ID = 'editor-preview-widget'; // The factory ID must be a hard-coded string because IDE2 does not depend on `@theia/editor-preview`.
10+
}
11+
912
@injectable()
1013
export class ShellLayoutRestorer extends TheiaShellLayoutRestorer {
1114
override async restoreLayout(app: FrontendApplication): Promise<boolean> {
@@ -160,8 +163,8 @@ export class ShellLayoutRestorer extends TheiaShellLayoutRestorer {
160163
constructionOptions: {
161164
factoryId: EditorWidgetFactory.ID,
162165
options: {
163-
uri,
164166
kind: 'navigatable',
167+
uri,
165168
counter: 0,
166169
},
167170
},
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
import type { MaybePromise } from '@theia/core';
2+
import type { Widget } from '@theia/core/lib/browser';
3+
import { WidgetManager as TheiaWidgetManager } from '@theia/core/lib/browser/widget-manager';
4+
import { injectable } from '@theia/core/shared/inversify';
5+
import deepEqual = require('deep-equal');
6+
7+
@injectable()
8+
export class WidgetManager extends TheiaWidgetManager {
9+
/**
10+
* Customized to find any existing widget based on `options` deepEquals instead of string equals.
11+
* See https://github.com/eclipse-theia/theia/issues/11309.
12+
*/
13+
protected override doGetWidget<T extends Widget>(
14+
key: string
15+
): MaybePromise<T> | undefined {
16+
const pendingWidget = this.findExistingWidget<T>(key);
17+
if (pendingWidget) {
18+
return pendingWidget as MaybePromise<T>;
19+
}
20+
return undefined;
21+
}
22+
23+
private findExistingWidget<T extends Widget>(
24+
key: string
25+
): MaybePromise<T> | undefined {
26+
const parsed = this.parseJson(key);
27+
for (const [candidateKey, widget] of [
28+
...this.widgetPromises.entries(),
29+
...this.pendingWidgetPromises.entries(),
30+
]) {
31+
const candidate = this.parseJson(candidateKey);
32+
if (deepEqual(candidate, parsed)) {
33+
return widget as MaybePromise<T>;
34+
}
35+
}
36+
return undefined;
37+
}
38+
39+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40+
private parseJson(json: string): any {
41+
try {
42+
return JSON.parse(json);
43+
} catch (err) {
44+
console.log(`Failed to parse JSON: <${json}>.`, err);
45+
throw err;
46+
}
47+
}
48+
}
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
11
import { injectable } from '@theia/core/shared/inversify';
2-
import { EditorPreviewContribution as TheiaEditorPreviewContribution } from '@theia/editor-preview/lib/browser/editor-preview-contribution';
32
import { TextEditor } from '@theia/editor/lib/browser';
3+
import { EditorContribution as TheiaEditorContribution } from '@theia/editor/lib/browser/editor-contribution';
44

55
@injectable()
6-
export class EditorPreviewContribution extends TheiaEditorPreviewContribution {
7-
protected updateLanguageStatus(editor: TextEditor | undefined): void {}
8-
9-
// protected setCursorPositionStatus(editor: TextEditor | undefined): void {
10-
// if (!editor) {
11-
// this.statusBar.removeElement('editor-status-cursor-position');
12-
// return;
13-
// }
14-
// const { cursor } = editor;
15-
// this.statusBar.setElement('editor-status-cursor-position', {
16-
// text: `${cursor.line + 1}`,
17-
// alignment: StatusBarAlignment.LEFT,
18-
// priority: 100,
19-
// });
20-
// }
6+
export class EditorContribution extends TheiaEditorContribution {
7+
protected override updateLanguageStatus(
8+
// eslint-disable-next-line @typescript-eslint/no-unused-vars, unused-imports/no-unused-vars
9+
editor: TextEditor | undefined
10+
): void {
11+
// NOOP
12+
}
2113
}

Diff for: browser-app/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
"@theia/core": "1.25.0",
88
"@theia/debug": "1.25.0",
99
"@theia/editor": "1.25.0",
10-
"@theia/editor-preview": "1.25.0",
1110
"@theia/file-search": "1.25.0",
1211
"@theia/filesystem": "1.25.0",
1312
"@theia/keymaps": "1.25.0",

Diff for: electron-app/package.json

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
"@theia/core": "1.25.0",
99
"@theia/debug": "1.25.0",
1010
"@theia/editor": "1.25.0",
11-
"@theia/editor-preview": "1.25.0",
1211
"@theia/electron": "1.25.0",
1312
"@theia/file-search": "1.25.0",
1413
"@theia/filesystem": "1.25.0",

0 commit comments

Comments
 (0)