@@ -17,11 +17,16 @@ import { ConfigService } from '../../../common/protocol/config-service';
17
17
import {
18
18
SketchesService ,
19
19
Sketch ,
20
+ SketchesError ,
20
21
} from '../../../common/protocol/sketches-service' ;
21
22
import { BoardsServiceProvider } from '../../boards/boards-service-provider' ;
22
23
import { BoardsConfig } from '../../boards/boards-config' ;
23
24
import { FileStat } from '@theia/filesystem/lib/common/files' ;
24
- import { StartupTask } from '../../widgets/sketchbook/startup-task' ;
25
+ import {
26
+ StartupTask ,
27
+ StartupTasks ,
28
+ } from '../../widgets/sketchbook/startup-task' ;
29
+ import { setURL } from '../../utils/window' ;
25
30
26
31
@injectable ( )
27
32
export class WorkspaceService extends TheiaWorkspaceService {
@@ -60,6 +65,35 @@ export class WorkspaceService extends TheiaWorkspaceService {
60
65
this . onCurrentWidgetChange ( { newValue, oldValue : null } ) ;
61
66
}
62
67
68
+ protected override async toFileStat (
69
+ uri : string | URI | undefined
70
+ ) : Promise < FileStat | undefined > {
71
+ const stat = await super . toFileStat ( uri ) ;
72
+ if ( ! stat ) {
73
+ return this . toFileStatWithNewSketchFallback ( uri ) ;
74
+ }
75
+ return stat ;
76
+ }
77
+
78
+ private async toFileStatWithNewSketchFallback (
79
+ uri : string | URI | undefined
80
+ ) : Promise < FileStat | undefined > {
81
+ if ( ! uri ) {
82
+ return ;
83
+ }
84
+ try {
85
+ await this . sketchService . loadSketch (
86
+ uri instanceof URI ? uri . toString ( ) : uri
87
+ ) ;
88
+ } catch ( err ) {
89
+ if ( SketchesError . NotFound . is ( err ) ) {
90
+ this . messageService . error ( err . message ) ;
91
+ }
92
+ }
93
+ const newSketchUri = await this . sketchService . createNewSketch ( ) ;
94
+ return this . toFileStat ( newSketchUri . uri ) ;
95
+ }
96
+
63
97
// Was copied from the Theia implementation.
64
98
// Unlike the default behavior, IDE2 does not check the existence of the workspace before open.
65
99
protected override async doGetDefaultWorkspaceUri ( ) : Promise <
@@ -78,6 +112,7 @@ export class WorkspaceService extends TheiaWorkspaceService {
78
112
const wpPath = decodeURI ( window . location . hash . substring ( 1 ) ) ;
79
113
const workspaceUri = new URI ( ) . withPath ( wpPath ) . withScheme ( 'file' ) ;
80
114
// ### Customization! Here, we do no check if the workspace exists.
115
+ // ### The error or missing sketch handling is done in the customized `toFileStat`.
81
116
return workspaceUri . toString ( ) ;
82
117
} else {
83
118
// Else, ask the server for its suggested workspace (usually the one
@@ -127,7 +162,7 @@ export class WorkspaceService extends TheiaWorkspaceService {
127
162
protected override openWindow ( uri : FileStat , options ?: WorkspaceInput ) : void {
128
163
const workspacePath = uri . resource . path . toString ( ) ;
129
164
if ( this . shouldPreserveWindow ( options ) ) {
130
- this . reloadWindow ( ) ;
165
+ this . reloadWindow ( options ) ; // Unlike Theia, IDE2 passes the `input` downstream.
131
166
} else {
132
167
try {
133
168
this . openNewWindow ( workspacePath , options ) ; // Unlike Theia, IDE2 passes the `input` downstream.
@@ -139,21 +174,25 @@ export class WorkspaceService extends TheiaWorkspaceService {
139
174
}
140
175
}
141
176
177
+ protected override reloadWindow ( options ?: WorkspaceInput ) : void {
178
+ if ( StartupTasks . WorkspaceInput . is ( options ) ) {
179
+ setURL ( StartupTask . append ( options . tasks , new URL ( window . location . href ) ) ) ;
180
+ }
181
+ super . reloadWindow ( ) ;
182
+ }
183
+
142
184
protected override openNewWindow (
143
185
workspacePath : string ,
144
186
options ?: WorkspaceInput
145
187
) : void {
146
188
const { boardsConfig } = this . boardsServiceProvider ;
147
- const url = BoardsConfig . Config . setConfig (
189
+ let url = BoardsConfig . Config . setConfig (
148
190
boardsConfig ,
149
191
new URL ( window . location . href )
150
192
) ; // Set the current boards config for the new browser window.
151
193
url . hash = workspacePath ;
152
- if ( StartupTask . WorkspaceInput . is ( options ) ) {
153
- url . searchParams . set (
154
- StartupTask . QUERY_STRING ,
155
- encodeURIComponent ( JSON . stringify ( options . tasks ) )
156
- ) ;
194
+ if ( StartupTasks . WorkspaceInput . is ( options ) ) {
195
+ url = StartupTask . append ( options . tasks , url ) ;
157
196
}
158
197
159
198
this . windowService . openNewWindow ( url . toString ( ) ) ;
0 commit comments