Skip to content

Commit 57016e6

Browse files
committed
use contribution instead of rebind
Signed-off-by: Jonah Iden <[email protected]>
1 parent 18f155d commit 57016e6

File tree

4 files changed

+54
-18
lines changed

4 files changed

+54
-18
lines changed

Diff for: packages/remote/src/electron-browser/remote-frontend-module.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ import { RemoteUserStorageContribution } from './remote-user-storage-provider';
3737
import { remoteFileSystemPath, RemoteFileSystemProxyFactory, RemoteFileSystemServer } from '@theia/filesystem/lib/common/remote-file-system-provider';
3838
import { LocalEnvVariablesServer, LocalRemoteFileSystemContribution, LocalRemoteFileSystemProvider, LocalRemoteFileSytemServer } from './local-backend-services';
3939
import { envVariablesPath, EnvVariablesServer } from '@theia/core/lib/common/env-variables';
40-
import { WorkspaceService } from '@theia/workspace/lib/browser';
41-
import { RemoteWorkspaceService } from './remote-workspace-service';
40+
import { WorkspaceHandlingContribution, WorkspaceOpenHandlerContribution } from '@theia/workspace/lib/browser';
41+
import { RemoteLocalWorkspaceContribution } from './remote-local-workspace-contribution';
4242
import { FileServiceContribution } from '@theia/filesystem/lib/browser/file-service';
4343

4444
export default new ContainerModule((bind, _, __, rebind) => {
@@ -85,7 +85,9 @@ export default new ContainerModule((bind, _, __, rebind) => {
8585
rebind(UserStorageContribution).to(RemoteUserStorageContribution);
8686

8787
if (isRemote) {
88-
rebind(WorkspaceService).to(RemoteWorkspaceService).inSingletonScope();
88+
bind(RemoteLocalWorkspaceContribution).toSelf().inSingletonScope();
89+
bind(WorkspaceOpenHandlerContribution).toService(RemoteLocalWorkspaceContribution);
90+
bind(WorkspaceHandlingContribution).toService(RemoteLocalWorkspaceContribution);
8991
bind(LocalRemoteFileSystemContribution).toSelf().inSingletonScope();
9092
bind(FileServiceContribution).toService(LocalRemoteFileSystemContribution);
9193
}

Diff for: packages/remote/src/electron-browser/remote-workspace-service.ts renamed to packages/remote/src/electron-browser/remote-local-workspace-contribution.ts

+34-12
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,34 @@
1414
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
1515
// *****************************************************************************
1616

17-
import { URI } from '@theia/core';
17+
import { ILogger, MaybePromise, URI } from '@theia/core';
1818
import { inject, injectable } from '@theia/core/shared/inversify';
19-
import { WorkspaceInput, WorkspaceService } from '@theia/workspace/lib/browser';
19+
import { WorkspaceHandlingContribution, WorkspaceInput, WorkspaceOpenHandlerContribution, WorkspacePreferences } from '@theia/workspace/lib/browser';
2020
import { LOCAL_FILE_SCHEME } from './local-backend-services';
2121
import { CURRENT_PORT_PARAM, LOCAL_PORT_PARAM, getCurrentPort, getLocalPort } from '@theia/core/lib/electron-browser/messaging/electron-local-ws-connection-source';
2222
import { RemoteStatusService } from '../electron-common/remote-status-service';
23+
import { WindowService } from '@theia/core/lib/browser/window/window-service';
2324

2425
@injectable()
25-
export class RemoteWorkspaceService extends WorkspaceService {
26+
export class RemoteLocalWorkspaceContribution implements WorkspaceOpenHandlerContribution, WorkspaceHandlingContribution {
2627

2728
@inject(RemoteStatusService)
2829
protected readonly remoteStatusService: RemoteStatusService;
2930

30-
override canHandle(uri: URI): boolean {
31-
return super.canHandle(uri) || uri.scheme === LOCAL_FILE_SCHEME;
31+
@inject(WindowService)
32+
protected readonly windowService: WindowService;
33+
34+
@inject(ILogger)
35+
protected logger: ILogger;
36+
37+
@inject(WorkspacePreferences)
38+
protected preferences: WorkspacePreferences;
39+
40+
canHandle(uri: URI): boolean {
41+
return uri.scheme === LOCAL_FILE_SCHEME;
3242
}
3343

34-
override async recentWorkspaces(): Promise<string[]> {
35-
const workspaces = await super.recentWorkspaces();
44+
async modifyRecentWorksapces(workspaces: string[]): Promise<string[]> {
3645
return workspaces.map(workspace => {
3746
const uri = new URI(workspace);
3847
if (uri.scheme === 'file') {
@@ -43,15 +52,28 @@ export class RemoteWorkspaceService extends WorkspaceService {
4352
});
4453
}
4554

46-
protected override reloadWindow(options?: WorkspaceInput): void {
55+
openWorkspace(uri: URI, options?: WorkspaceInput | undefined): MaybePromise<void> {
56+
const workspacePath = uri.path.toString();
57+
58+
if (this.preferences['workspace.preserveWindow'] || (options && options.preserveWindow)) {
59+
this.reloadWindow(workspacePath);
60+
} else {
61+
try {
62+
this.openNewWindow(workspacePath);
63+
} catch (error) {
64+
this.logger.error(error.toString()).then(() => this.reloadWindow(workspacePath));
65+
}
66+
}
67+
}
68+
69+
protected reloadWindow(workspacePath: string): void {
4770
const currentPort = getCurrentPort();
48-
const url = this.getModifiedUrl();
49-
history.replaceState(undefined, '', url.toString());
5071
this.remoteStatusService.connectionClosed(parseInt(currentPort ?? '0'));
51-
super.reloadWindow(options);
72+
const searchParams = this.getModifiedUrl().searchParams;
73+
this.windowService.reload({ hash: encodeURI(workspacePath), search: Object.fromEntries(searchParams) });
5274
}
5375

54-
protected override openNewWindow(workspacePath: string, options?: WorkspaceInput): void {
76+
protected openNewWindow(workspacePath: string): void {
5577
const url = this.getModifiedUrl();
5678
url.hash = encodeURI(workspacePath);
5779
this.windowService.openNewWindow(url.toString());

Diff for: packages/workspace/src/browser/workspace-frontend-module.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ import { LabelProviderContribution } from '@theia/core/lib/browser/label-provide
3232
import { VariableContribution } from '@theia/variable-resolver/lib/browser';
3333
import { WorkspaceServer, workspacePath, UntitledWorkspaceService, WorkspaceFileService } from '../common';
3434
import { WorkspaceFrontendContribution } from './workspace-frontend-contribution';
35-
import { WorkspaceOpenHandlerContribution, WorkspaceService } from './workspace-service';
35+
import { WorkspaceHandlingContribution, WorkspaceOpenHandlerContribution, WorkspaceService } from './workspace-service';
3636
import { WorkspaceCommandContribution, FileMenuContribution, EditMenuContribution } from './workspace-commands';
3737
import { WorkspaceVariableContribution } from './workspace-variable-contribution';
3838
import { WorkspaceStorageService } from './workspace-storage-service';
@@ -60,6 +60,7 @@ export default new ContainerModule((bind: interfaces.Bind, unbind: interfaces.Un
6060
bindWorkspacePreferences(bind);
6161
bindWorkspaceTrustPreferences(bind);
6262
bindContributionProvider(bind, WorkspaceOpenHandlerContribution);
63+
bindContributionProvider(bind, WorkspaceHandlingContribution);
6364

6465
bind(WorkspaceService).toSelf().inSingletonScope();
6566
bind(FrontendApplicationContribution).toService(WorkspaceService);

Diff for: packages/workspace/src/browser/workspace-service.ts

+13-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ export interface WorkspaceOpenHandlerContribution {
4444
getWorkspaceLabel?(uri: URI): MaybePromise<string | undefined>;
4545
}
4646

47+
export const WorkspaceHandlingContribution = Symbol('WorkspaceHandlingContribution');
4748
export interface WorkspaceHandlingContribution {
4849
modifyRecentWorksapces?(workspaces: string[]): MaybePromise<string[]>;
49-
5050
}
5151

5252
/**
@@ -108,6 +108,9 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
108108
@inject(ContributionProvider) @named(WorkspaceOpenHandlerContribution)
109109
protected readonly openHandlerContribution: ContributionProvider<WorkspaceOpenHandlerContribution>;
110110

111+
@inject(ContributionProvider) @named(WorkspaceHandlingContribution)
112+
protected readonly workspaceHandlingContribution: ContributionProvider<WorkspaceHandlingContribution>;
113+
111114
protected _ready = new Deferred<void>();
112115
get ready(): Promise<void> {
113116
return this._ready.promise;
@@ -336,7 +339,15 @@ export class WorkspaceService implements FrontendApplicationContribution, Worksp
336339
}
337340

338341
async recentWorkspaces(): Promise<string[]> {
339-
return this.server.getRecentWorkspaces();
342+
let recentWorkspaces = await this.server.getRecentWorkspaces();
343+
344+
for (const handler of this.workspaceHandlingContribution.getContributions()) {
345+
if (handler.modifyRecentWorksapces) {
346+
recentWorkspaces = await handler.modifyRecentWorksapces(recentWorkspaces);
347+
}
348+
}
349+
350+
return recentWorkspaces;
340351
}
341352

342353
async removeRecentWorkspace(uri: string): Promise<void> {

0 commit comments

Comments
 (0)