14
14
// SPDX-License-Identifier: EPL-2.0 OR GPL-2.0-only WITH Classpath-exception-2.0
15
15
// *****************************************************************************
16
16
17
- import { URI } from '@theia/core' ;
17
+ import { ILogger , MaybePromise , URI } from '@theia/core' ;
18
18
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' ;
20
20
import { LOCAL_FILE_SCHEME } from './local-backend-services' ;
21
21
import { CURRENT_PORT_PARAM , LOCAL_PORT_PARAM , getCurrentPort , getLocalPort } from '@theia/core/lib/electron-browser/messaging/electron-local-ws-connection-source' ;
22
22
import { RemoteStatusService } from '../electron-common/remote-status-service' ;
23
+ import { WindowService } from '@theia/core/lib/browser/window/window-service' ;
23
24
24
25
@injectable ( )
25
- export class RemoteWorkspaceService extends WorkspaceService {
26
+ export class RemoteLocalWorkspaceContribution implements WorkspaceOpenHandlerContribution , WorkspaceHandlingContribution {
26
27
27
28
@inject ( RemoteStatusService )
28
29
protected readonly remoteStatusService : RemoteStatusService ;
29
30
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 ;
32
42
}
33
43
34
- override async recentWorkspaces ( ) : Promise < string [ ] > {
35
- const workspaces = await super . recentWorkspaces ( ) ;
44
+ async modifyRecentWorksapces ( workspaces : string [ ] ) : Promise < string [ ] > {
36
45
return workspaces . map ( workspace => {
37
46
const uri = new URI ( workspace ) ;
38
47
if ( uri . scheme === 'file' ) {
@@ -43,15 +52,28 @@ export class RemoteWorkspaceService extends WorkspaceService {
43
52
} ) ;
44
53
}
45
54
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 {
47
70
const currentPort = getCurrentPort ( ) ;
48
- const url = this . getModifiedUrl ( ) ;
49
- history . replaceState ( undefined , '' , url . toString ( ) ) ;
50
71
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 ) } ) ;
52
74
}
53
75
54
- protected override openNewWindow ( workspacePath : string , options ?: WorkspaceInput ) : void {
76
+ protected openNewWindow ( workspacePath : string ) : void {
55
77
const url = this . getModifiedUrl ( ) ;
56
78
url . hash = encodeURI ( workspacePath ) ;
57
79
this . windowService . openNewWindow ( url . toString ( ) ) ;
0 commit comments