@@ -14,7 +14,7 @@ import { dirname, join } from 'vs/base/common/path';
1414import { basename , extUriBiasedIgnorePathCase , joinPath , originalFSPath } from 'vs/base/common/resources' ;
1515import { withNullAsUndefined } from 'vs/base/common/types' ;
1616import { URI } from 'vs/base/common/uri' ;
17- import { Promises , readdirSync , rimrafSync , writeFileSync } from 'vs/base/node/pfs' ;
17+ import { Promises , rimrafSync , writeFileSync } from 'vs/base/node/pfs' ;
1818import { localize } from 'vs/nls' ;
1919import { IBackupMainService } from 'vs/platform/backup/electron-main/backup' ;
2020import { IDialogMainService } from 'vs/platform/dialogs/electron-main/dialogMainService' ;
@@ -51,7 +51,7 @@ export interface IWorkspacesManagementMainService {
5151 deleteUntitledWorkspace ( workspace : IWorkspaceIdentifier ) : Promise < void > ;
5252 deleteUntitledWorkspaceSync ( workspace : IWorkspaceIdentifier ) : void ;
5353
54- getUntitledWorkspacesSync ( ) : IUntitledWorkspaceInfo [ ] ;
54+ getUntitledWorkspaces ( ) : IUntitledWorkspaceInfo [ ] ;
5555 isUntitledWorkspace ( workspace : IWorkspaceIdentifier ) : boolean ;
5656
5757 resolveLocalWorkspaceSync ( path : URI ) : IResolvedWorkspace | undefined ;
@@ -64,14 +64,16 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
6464
6565 declare readonly _serviceBrand : undefined ;
6666
67- private readonly untitledWorkspacesHome = this . environmentMainService . untitledWorkspacesHome ; // local URI that contains all untitled workspaces
68-
6967 private readonly _onDidDeleteUntitledWorkspace = this . _register ( new Emitter < IWorkspaceIdentifier > ( ) ) ;
7068 readonly onDidDeleteUntitledWorkspace : Event < IWorkspaceIdentifier > = this . _onDidDeleteUntitledWorkspace . event ;
7169
7270 private readonly _onDidEnterWorkspace = this . _register ( new Emitter < IWorkspaceEnteredEvent > ( ) ) ;
7371 readonly onDidEnterWorkspace : Event < IWorkspaceEnteredEvent > = this . _onDidEnterWorkspace . event ;
7472
73+ private readonly untitledWorkspacesHome = this . environmentMainService . untitledWorkspacesHome ; // local URI that contains all untitled workspaces
74+
75+ private untitledWorkspaces : IUntitledWorkspaceInfo [ ] = [ ] ;
76+
7577 constructor (
7678 @IEnvironmentMainService private readonly environmentMainService : IEnvironmentMainService ,
7779 @ILogService private readonly logService : ILogService ,
@@ -83,6 +85,30 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
8385 super ( ) ;
8486 }
8587
88+ async initialize ( ) : Promise < void > {
89+
90+ // Reset
91+ this . untitledWorkspaces = [ ] ;
92+
93+ // Resolve untitled workspaces
94+ try {
95+ const untitledWorkspacePaths = ( await Promises . readdir ( this . untitledWorkspacesHome . fsPath ) ) . map ( folder => joinPath ( this . untitledWorkspacesHome , folder , UNTITLED_WORKSPACE_NAME ) ) ;
96+ for ( const untitledWorkspacePath of untitledWorkspacePaths ) {
97+ const workspace = getWorkspaceIdentifier ( untitledWorkspacePath ) ;
98+ const resolvedWorkspace = await this . resolveLocalWorkspace ( untitledWorkspacePath ) ;
99+ if ( ! resolvedWorkspace ) {
100+ await this . deleteUntitledWorkspace ( workspace ) ;
101+ } else {
102+ this . untitledWorkspaces . push ( { workspace, remoteAuthority : resolvedWorkspace . remoteAuthority } ) ;
103+ }
104+ }
105+ } catch ( error ) {
106+ if ( error . code !== 'ENOENT' ) {
107+ this . logService . warn ( `Unable to read folders in ${ this . untitledWorkspacesHome } (${ error } ).` ) ;
108+ }
109+ }
110+ }
111+
86112 resolveLocalWorkspaceSync ( uri : URI ) : IResolvedWorkspace | undefined {
87113 return this . doResolveLocalWorkspace ( uri , path => readFileSync ( path , 'utf8' ) ) ;
88114 }
@@ -158,6 +184,8 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
158184 await Promises . mkdir ( dirname ( configPath ) , { recursive : true } ) ;
159185 await Promises . writeFile ( configPath , JSON . stringify ( storedWorkspace , null , '\t' ) ) ;
160186
187+ this . untitledWorkspaces . push ( { workspace, remoteAuthority } ) ;
188+
161189 return workspace ;
162190 }
163191
@@ -168,6 +196,8 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
168196 mkdirSync ( dirname ( configPath ) , { recursive : true } ) ;
169197 writeFileSync ( configPath , JSON . stringify ( storedWorkspace , null , '\t' ) ) ;
170198
199+ this . untitledWorkspaces . push ( { workspace, remoteAuthority } ) ;
200+
171201 return workspace ;
172202 }
173203
@@ -204,8 +234,8 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
204234 // Delete from disk
205235 this . doDeleteUntitledWorkspaceSync ( workspace ) ;
206236
237+ // unset workspace from profiles
207238 if ( this . userDataProfilesMainService . isEnabled ( ) ) {
208- // unset workspace from profiles
209239 this . userDataProfilesMainService . unsetWorkspace ( workspace ) ;
210240 }
211241
@@ -229,31 +259,16 @@ export class WorkspacesManagementMainService extends Disposable implements IWork
229259 if ( existsSync ( workspaceStoragePath ) ) {
230260 writeFileSync ( join ( workspaceStoragePath , 'obsolete' ) , '' ) ;
231261 }
262+
263+ // Remove from list
264+ this . untitledWorkspaces = this . untitledWorkspaces . filter ( untitledWorkspace => untitledWorkspace . workspace . id !== workspace . id ) ;
232265 } catch ( error ) {
233266 this . logService . warn ( `Unable to delete untitled workspace ${ configPath } (${ error } ).` ) ;
234267 }
235268 }
236269
237- getUntitledWorkspacesSync ( ) : IUntitledWorkspaceInfo [ ] {
238- const untitledWorkspaces : IUntitledWorkspaceInfo [ ] = [ ] ;
239- try {
240- const untitledWorkspacePaths = readdirSync ( this . untitledWorkspacesHome . fsPath ) . map ( folder => joinPath ( this . untitledWorkspacesHome , folder , UNTITLED_WORKSPACE_NAME ) ) ;
241- for ( const untitledWorkspacePath of untitledWorkspacePaths ) {
242- const workspace = getWorkspaceIdentifier ( untitledWorkspacePath ) ;
243- const resolvedWorkspace = this . resolveLocalWorkspaceSync ( untitledWorkspacePath ) ;
244- if ( ! resolvedWorkspace ) {
245- this . doDeleteUntitledWorkspaceSync ( workspace ) ;
246- } else {
247- untitledWorkspaces . push ( { workspace, remoteAuthority : resolvedWorkspace . remoteAuthority } ) ;
248- }
249- }
250- } catch ( error ) {
251- if ( error . code !== 'ENOENT' ) {
252- this . logService . warn ( `Unable to read folders in ${ this . untitledWorkspacesHome } (${ error } ).` ) ;
253- }
254- }
255-
256- return untitledWorkspaces ;
270+ getUntitledWorkspaces ( ) : IUntitledWorkspaceInfo [ ] {
271+ return this . untitledWorkspaces ;
257272 }
258273
259274 async enterWorkspace ( window : ICodeWindow , windows : ICodeWindow [ ] , path : URI ) : Promise < IEnterWorkspaceResult | undefined > {
0 commit comments