diff --git a/src/vs/test/utils/servicesTestUtils.ts b/src/vs/test/utils/servicesTestUtils.ts index 8e04c56f41e836c36777809dfb1293b59eeeff3f..f91843a747eed9bc23b5e7880556e341009d0163 100644 --- a/src/vs/test/utils/servicesTestUtils.ts +++ b/src/vs/test/utils/servicesTestUtils.ts @@ -48,7 +48,7 @@ import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/edi import { IHistoryService } from 'vs/workbench/services/history/common/history'; import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation'; import { TestConfigurationService } from 'vs/platform/configuration/test/common/testConfigurationService'; -import { IWindowsService } from 'vs/platform/windows/common/windows'; +import { IWindowsService, IWindowService } from 'vs/platform/windows/common/windows'; export const TestWorkspace: IWorkspace = { resource: URI.file('C:\\testWorkspace'), @@ -625,6 +625,11 @@ export const TestFileService = { export class TestBackupFileService implements IBackupFileService { public _serviceBrand: any; + + public hasBackups(): TPromise { + return TPromise.as(false); + } + public hasBackup(resource: URI): TPromise { return TPromise.as(false); } @@ -672,6 +677,87 @@ export class TestBackupFileService implements IBackupFileService { } }; +export class TestWindowService implements IWindowService { + + public _serviceBrand: any; + + getCurrentWindowId(): number { + return 0; + } + + openFileFolderPicker(forceNewWindow?: boolean): TPromise { + return TPromise.as(void 0); + } + + openFilePicker(forceNewWindow?: boolean, path?: string): TPromise { + return TPromise.as(void 0); + } + + openFolderPicker(forceNewWindow?: boolean): TPromise { + return TPromise.as(void 0); + } + + reloadWindow(): TPromise { + return TPromise.as(void 0); + } + + openDevTools(): TPromise { + return TPromise.as(void 0); + } + + toggleDevTools(): TPromise { + return TPromise.as(void 0); + } + + closeFolder(): TPromise { + return TPromise.as(void 0); + } + + toggleFullScreen(): TPromise { + return TPromise.as(void 0); + } + + setRepresentedFilename(fileName: string): TPromise { + return TPromise.as(void 0); + } + + addToRecentlyOpen(paths: { path: string, isFile?: boolean }[]): TPromise { + return TPromise.as(void 0); + } + + removeFromRecentlyOpen(paths: string[]): TPromise { + return TPromise.as(void 0); + } + + getRecentlyOpen(): TPromise<{ files: string[]; folders: string[]; }> { + return TPromise.as(void 0); + } + + focusWindow(): TPromise { + return TPromise.as(void 0); + } + + setDocumentEdited(flag: boolean): TPromise { + return TPromise.as(void 0); + } + + toggleMenuBar(): TPromise { + return TPromise.as(void 0); + } + + isMaximized(): TPromise { + return TPromise.as(void 0); + } + + maximizeWindow(): TPromise { + return TPromise.as(void 0); + } + + unmaximizeWindow(): TPromise { + return TPromise.as(void 0); + } +} + export class TestLifecycleService implements ILifecycleService { public _serviceBrand: any; diff --git a/src/vs/workbench/electron-browser/workbench.ts b/src/vs/workbench/electron-browser/workbench.ts index 2b9377f3243919496e0f22cc844c1e63d51cc812..66b8b3e329427b826db8e8479b75d6b641091c96 100644 --- a/src/vs/workbench/electron-browser/workbench.ts +++ b/src/vs/workbench/electron-browser/workbench.ts @@ -149,6 +149,7 @@ export class Workbench implements IPartService { private viewletService: IViewletService; private contextKeyService: IContextKeyService; private keybindingService: IKeybindingService; + private backupFileService: IBackupFileService; private configurationEditingService: IConfigurationEditingService; private titlebarPart: TitlebarPart; private activitybarPart: ActivitybarPart; @@ -387,7 +388,13 @@ export class Workbench implements IPartService { // Empty workbench: some first time users will not have an untiled file; returning users will always have one else if (!this.workbenchParams.workspace && this.telemetryService.getExperiments().openUntitledFile) { - return TPromise.as([{ input: this.untitledEditorService.createOrGet() }]); + return this.backupFileService.hasBackups().then(hasBackups => { + if (hasBackups) { + return TPromise.as([]); // do not open any empty untitled file if we have backups to restore + } + + return TPromise.as([{ input: this.untitledEditorService.createOrGet() }]); + }); } return TPromise.as([]); @@ -462,8 +469,8 @@ export class Workbench implements IPartService { serviceCollection.set(IHistoryService, new SyncDescriptor(HistoryService)); // Backup File Service - const workspace = this.contextService.getWorkspace(); - serviceCollection.set(IBackupFileService, this.instantiationService.createInstance(BackupFileService, this.windowService.getCurrentWindowId())); + this.backupFileService = this.instantiationService.createInstance(BackupFileService); + serviceCollection.set(IBackupFileService, this.backupFileService); // Text File Service serviceCollection.set(ITextFileService, new SyncDescriptor(TextFileService)); @@ -479,6 +486,7 @@ export class Workbench implements IPartService { serviceCollection.set(IConfigurationEditingService, this.configurationEditingService); // Configuration Resolver + const workspace = this.contextService.getWorkspace(); serviceCollection.set(IConfigurationResolverService, new SyncDescriptor(ConfigurationResolverService, workspace ? workspace.resource : null, process.env)); // Quick open service (quick open controller) diff --git a/src/vs/workbench/services/backup/common/backup.ts b/src/vs/workbench/services/backup/common/backup.ts index f2f7c1e117e17874cc483bcca5a7e3d54ec2b38c..b8c71c3cd0d84b45967b9387340c66f2f9f23464 100644 --- a/src/vs/workbench/services/backup/common/backup.ts +++ b/src/vs/workbench/services/backup/common/backup.ts @@ -22,6 +22,11 @@ export const BACKUP_FILE_UPDATE_OPTIONS: IUpdateContentOptions = { encoding: 'ut export interface IBackupFileService { _serviceBrand: any; + /** + * Finds out if there are any backups stored. + */ + hasBackups(): TPromise; + /** * Loads the backup resource for a particular resource within the current workspace. * diff --git a/src/vs/workbench/services/backup/node/backupFileService.ts b/src/vs/workbench/services/backup/node/backupFileService.ts index 47a7198ae9c0bdcd2a13178d47d2a42590d28027..76a597a9f6fa2825964f7c7a3193bee96b34694f 100644 --- a/src/vs/workbench/services/backup/node/backupFileService.ts +++ b/src/vs/workbench/services/backup/node/backupFileService.ts @@ -17,6 +17,7 @@ import { IFileService } from 'vs/platform/files/common/files'; import { TPromise } from 'vs/base/common/winjs.base'; import { readToMatchingString } from 'vs/base/node/stream'; import { IRawTextContent } from 'vs/workbench/services/textfile/common/textfiles'; +import { IWindowService } from 'vs/platform/windows/common/windows'; export interface IBackupFilesModel { resolve(backupRoot: string): TPromise; @@ -25,6 +26,7 @@ export interface IBackupFilesModel { has(resource: Uri, versionId?: number): boolean; get(): Uri[]; remove(resource: Uri): void; + count(): number; clear(): void; } @@ -55,6 +57,10 @@ export class BackupFilesModel implements IBackupFilesModel { this.cache[resource.toString()] = versionId; } + public count(): number { + return Object.keys(this.cache).length; + } + public has(resource: Uri, versionId?: number): boolean { const cachedVersionId = this.cache[resource.toString()]; if (typeof cachedVersionId !== 'number') { @@ -91,12 +97,12 @@ export class BackupFileService implements IBackupFileService { private ready: TPromise; constructor( - windowId: number, @IEnvironmentService private environmentService: IEnvironmentService, @IFileService private fileService: IFileService, + @IWindowService windowService: IWindowService, @IBackupService private backupService: IBackupService ) { - this.ready = this.init(windowId); + this.ready = this.init(windowService.getCurrentWindowId()); } private get backupEnabled(): boolean { @@ -112,10 +118,17 @@ export class BackupFileService implements IBackupFileService { return this.backupService.getBackupPath(windowId).then(backupPath => { this.backupWorkspacePath = backupPath; + return model.resolve(this.backupWorkspacePath); }); } + public hasBackups(): TPromise { + return this.ready.then(model => { + return model.count() > 0; + }); + } + public hasBackup(resource: Uri): TPromise { return this.ready.then(model => { const backupResource = this.getBackupResource(resource); @@ -216,4 +229,4 @@ export class BackupFileService implements IBackupFileService { return crypto.createHash('md5').update(caseAwarePath).digest('hex'); } -} \ No newline at end of file +} diff --git a/src/vs/workbench/services/backup/test/backupFileService.test.ts b/src/vs/workbench/services/backup/test/backupFileService.test.ts index 27d4b7f015d4055e5035dde35a65cce3bfe3eb30..76203be7f5aff9c141247d311bf86e3e5d7a5347 100644 --- a/src/vs/workbench/services/backup/test/backupFileService.test.ts +++ b/src/vs/workbench/services/backup/test/backupFileService.test.ts @@ -22,7 +22,7 @@ import { parseArgs } from 'vs/platform/environment/node/argv'; import { TextModel } from 'vs/editor/common/model/textModel'; import { IRawTextContent } from 'vs/workbench/services/textfile/common/textfiles'; import { TPromise } from 'vs/base/common/winjs.base'; - +import { TestWindowService } from 'vs/test/utils/servicesTestUtils'; class TestEnvironmentService extends EnvironmentService { constructor(private _backupHome: string, private _backupWorkspacesPath: string) { @@ -56,7 +56,7 @@ class TestBackupFileService extends BackupFileService { getBackupPath: () => TPromise.as(workspaceBackupPath) }; - super(1, environmentService, fileService, backupService); + super(environmentService, fileService, new TestWindowService(), backupService); } public getBackupResource(resource: Uri): Uri { @@ -126,7 +126,10 @@ suite('BackupFileService', () => { service = new TestBackupFileService(workspaceResource, backupHome, workspacesJsonPath); service.hasBackup(fooFile).then(exists2 => { assert.equal(exists2, true); - done(); + return service.hasBackups().then(hasBackups => { + assert.ok(hasBackups); + done(); + }); }); }); }); @@ -331,4 +334,4 @@ suite('BackupFilesModel', () => { assert.deepEqual(model.get().map(f => f.fsPath), [file1.fsPath, file2.fsPath, untitled.fsPath]); }); -}); \ No newline at end of file +});