@@ -22,12 +22,12 @@ import { IRemoteAgentService } from 'vs/workbench/services/remote/common/remoteA
2222import { IFileService } from 'vs/platform/files/common/files' ;
2323import { FileService } from 'vs/platform/files/common/fileService' ;
2424import { 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' ;
2626import { IWorkbenchConfigurationService } from 'vs/workbench/services/configuration/common/configuration' ;
2727import { onUnexpectedError } from 'vs/base/common/errors' ;
2828import { 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' ;
3131import { WorkspaceService } from 'vs/workbench/services/configuration/browser/configurationService' ;
3232import { ConfigurationCache } from 'vs/workbench/services/configuration/browser/configurationCache' ;
3333import { ISignService } from 'vs/platform/sign/common/sign' ;
@@ -68,6 +68,7 @@ import { safeStringify } from 'vs/base/common/objects';
6868import { ICredentialsService } from 'vs/workbench/services/credentials/common/credentials' ;
6969import { IndexedDB } from 'vs/base/browser/indexedDB' ;
7070import { CodeServerClientAdditions } from 'vs/workbench/browser/client' ;
71+ import { BrowserWorkspacesService } from 'vs/workbench/services/workspaces/browser/workspacesService' ;
7172
7273class BrowserMain extends Disposable {
7374
@@ -219,6 +220,63 @@ class BrowserMain extends Disposable {
219220 } )
220221 ] ) ;
221222
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+
222280 // Workspace Trust Service
223281 const workspaceTrustEnablementService = new WorkspaceTrustEnablementService ( configurationService , environmentService ) ;
224282 serviceCollection . set ( IWorkspaceTrustEnablementService , workspaceTrustEnablementService ) ;
0 commit comments