Skip to content

Commit

Permalink
Editor manager should be singleton.
Browse files Browse the repository at this point in the history
Added some logging when filtering the layout data.

Signed-off-by: Akos Kitta <a.kitta@arduino.cc>
  • Loading branch information
Akos Kitta authored and kittaakos committed Jun 8, 2022
1 parent df8658e commit 555da87
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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<void> {
if (this.shouldStoreLayout) {
Expand Down Expand Up @@ -35,21 +42,36 @@ 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 = (
layoutData as any
).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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down

0 comments on commit 555da87

Please sign in to comment.