@@ -22,12 +22,12 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA
22
22
import { IFileService } from 'vs/platform/files/common/files' ;
23
23
import { FileService } from 'vs/platform/files/common/fileService' ;
24
24
import { Schemas } from 'vs/base/common/network' ;
25
- import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace' ;
25
+ import { IWorkspaceContextService , toWorkspaceFolder } from 'vs/platform/workspace/common/workspace' ;
26
26
import { IWorkbenchConfigurationService } from 'vs/workbench/services/configuration/common/configuration' ;
27
27
import { onUnexpectedError } from 'vs/base/common/errors' ;
28
28
import { setFullscreen } from 'vs/base/browser/browser' ;
29
- import { URI } from 'vs/base/common/uri' ;
30
- import { IWorkspaceInitializationPayload } from 'vs/platform/workspaces/common/workspaces' ;
29
+ import { encodePath , URI } from 'vs/base/common/uri' ;
30
+ import { isRecentFolder , IWorkspaceInitializationPayload , IWorkspacesService } from 'vs/platform/workspaces/common/workspaces' ;
31
31
import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService' ;
32
32
import { ConfigurationCache } from 'vs/workbench/services/configuration/browser/configurationCache' ;
33
33
import { ISignService } from 'vs/platform/sign/common/sign' ;
@@ -68,6 +68,7 @@ import { safeStringify } from 'vs/base/common/objects';
68
68
import { ICredentialsService } from 'vs/workbench/services/credentials/common/credentials' ;
69
69
import { IndexedDB } from 'vs/base/browser/indexedDB' ;
70
70
import { CodeServerClientAdditions } from 'vs/workbench/browser/client' ;
71
+ import { BrowserWorkspacesService } from 'vs/workbench/services/workspaces/browser/workspacesService' ;
71
72
72
73
class BrowserMain extends Disposable {
73
74
@@ -219,6 +220,63 @@ class BrowserMain extends Disposable {
219
220
} )
220
221
] ) ;
221
222
223
+ /**
224
+ * Added to persist recent workspaces in the browser.
225
+ * @author coder
226
+ * @example User specified a directory at startup.
227
+ * ```sh
228
+ * code-server ./path/to/project/
229
+ * ```
230
+ *
231
+ * @example Blank project without CLI arguments,
232
+ * using the last opened directory in the browser.
233
+ * ```sh
234
+ * code-server
235
+ * open http://localhost:8000/
236
+ * ```
237
+ *
238
+ * @example Query params override CLI arguments.
239
+ * ```sh
240
+ * code-server ./path/to/project/
241
+ * open http://localhost:8000/?folder=/path/to/different/project
242
+ * ```
243
+ */
244
+ const browserWorkspacesService = new BrowserWorkspacesService ( storageService , configurationService , logService , fileService , environmentService , uriIdentityService ) ;
245
+ serviceCollection . set ( IWorkspacesService , browserWorkspacesService ) ;
246
+ const workspace = configurationService . getWorkspace ( ) ;
247
+
248
+ logService . debug ( 'Workspace Folders:' , workspace . folders ) ;
249
+
250
+ if ( workspace . folders . length === 0 ) {
251
+ logService . debug ( 'Workspace is empty. Checking for recent folders...' ) ;
252
+
253
+ const recentlyOpened = await browserWorkspacesService . getRecentlyOpened ( ) ;
254
+
255
+ for ( const recent of recentlyOpened . workspaces ) {
256
+ if ( isRecentFolder ( recent ) ) {
257
+ logService . debug ( 'Recent folder found...' ) ;
258
+ const folder = toWorkspaceFolder ( recent . folderUri ) ;
259
+ // Note that the `folders` property should be reassigned instead of pushed into.
260
+ // This property has a setter which updates the workspace's file cache.
261
+ workspace . folders = [ folder ] ;
262
+
263
+
264
+ /**
265
+ * Opening a folder from the browser navigates to a URL including the folder param.
266
+ * However, since we're overriding the default state of a blank editor,
267
+ * we update the URL query param to match this behavior.
268
+ * This is especially useful when a user wants to share a link to server with a specific folder.
269
+ *
270
+ * @see `WorkspaceProvider.createTargetUrl`
271
+ * @see `WorkspaceProvider.QUERY_PARAM_FOLDER`
272
+ */
273
+ const nextQueryParam = `?folder=${ encodePath ( folder . uri . path ) } ` ;
274
+ window . history . replaceState ( null , '' , nextQueryParam ) ;
275
+ break ;
276
+ }
277
+ }
278
+ }
279
+
222
280
// Workspace Trust Service
223
281
const workspaceTrustEnablementService = new WorkspaceTrustEnablementService ( configurationService , environmentService ) ;
224
282
serviceCollection . set ( IWorkspaceTrustEnablementService , workspaceTrustEnablementService ) ;
0 commit comments