diff --git a/src/vs/workbench/common/editor/untitledEditorModel.ts b/src/vs/workbench/common/editor/untitledEditorModel.ts index 0b1f4904cdc..b431eda418c 100644 --- a/src/vs/workbench/common/editor/untitledEditorModel.ts +++ b/src/vs/workbench/common/editor/untitledEditorModel.ts @@ -50,7 +50,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS super(value, modeId, resource, modeService, modelService); this.hasAssociatedFilePath = hasAssociatedFilePath; - this.dirty = hasAssociatedFilePath || value !== ''; // untitled associated to file path are dirty right away + this.dirty = hasAssociatedFilePath || !!value; // untitled associated to file path are dirty right away as well as untitled with content this._onDidChangeContent = new Emitter(); this._onDidChangeDirty = new Emitter(); @@ -59,6 +59,13 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS this.contentChangeEventScheduler = new RunOnceScheduler(() => this._onDidChangeContent.fire(), UntitledEditorModel.DEFAULT_CONTENT_CHANGE_BUFFER_DELAY); this.registerListeners(); + + // Indicate dirty state to listeners + if (this.dirty) { + setTimeout(() => { + this._onDidChangeDirty.fire(); + }, 0 /* prevent race condition between creating input and emitting dirty event */); + } } public get onDidChangeContent(): Event { diff --git a/src/vs/workbench/parts/backup/common/backup.contribution.ts b/src/vs/workbench/parts/backup/common/backup.contribution.ts index 3693b0b9285..c531ff3baa3 100644 --- a/src/vs/workbench/parts/backup/common/backup.contribution.ts +++ b/src/vs/workbench/parts/backup/common/backup.contribution.ts @@ -8,8 +8,14 @@ import { Registry } from 'vs/platform/platform'; import { IWorkbenchContributionsRegistry, Extensions as WorkbenchExtensions } from 'vs/workbench/common/contributions'; import { BackupModelTracker } from 'vs/workbench/parts/backup/common/backupModelTracker'; +import { BackupRestorer } from 'vs/workbench/parts/backup/common/backupRestorer'; -// Register Dirty Files Tracker -(Registry.as(WorkbenchExtensions.Workbench)).registerWorkbenchContribution( +// Register Backup Model Tracker +Registry.as(WorkbenchExtensions.Workbench).registerWorkbenchContribution( BackupModelTracker +); + +// Register Backup Restorer +Registry.as(WorkbenchExtensions.Workbench).registerWorkbenchContribution( + BackupRestorer ); \ No newline at end of file diff --git a/src/vs/workbench/parts/backup/common/backupRestorer.ts b/src/vs/workbench/parts/backup/common/backupRestorer.ts new file mode 100644 index 00000000000..c3a1fbe566f --- /dev/null +++ b/src/vs/workbench/parts/backup/common/backupRestorer.ts @@ -0,0 +1,45 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (c) Microsoft Corporation. All rights reserved. + * Licensed under the MIT License. See License.txt in the project root for license information. + *--------------------------------------------------------------------------------------------*/ + +'use strict'; + +import { TPromise } from 'vs/base/common/winjs.base'; +import { IUntitledEditorService } from 'vs/workbench/services/untitled/common/untitledEditorService'; +import { IEnvironmentService } from 'vs/platform/environment/common/environment'; +import { IWorkbenchContribution } from 'vs/workbench/common/contributions'; +import { IPartService } from 'vs/workbench/services/part/common/partService'; +import errors = require('vs/base/common/errors'); + +// TODO@ben TODO@tyriar this should restore any backup that exists on disk and not rely +// on the editors to be restored already in the stacks model. For that a method is needed +// to get all backups that exist on disk. +export class BackupRestorer implements IWorkbenchContribution { + + public _serviceBrand: any; + + constructor( + @IUntitledEditorService private untitledEditorService: IUntitledEditorService, + @IEnvironmentService private environmentService: IEnvironmentService, + @IPartService private partService: IPartService + ) { + if (!this.environmentService.isExtensionDevelopment) { + this.restoreBackups(); + } + } + + private restoreBackups(): void { + + // Wait for all editors being restored before restoring backups + this.partService.joinCreation().then(() => { + + // Resolve all untitled so that their backups get loaded + TPromise.join(this.untitledEditorService.getAll().map(untitled => untitled.resolve())).done(null, errors.onUnexpectedError); + }); + } + + public getId(): string { + return 'vs.backup.backupRestorer'; + } +} \ No newline at end of file diff --git a/src/vs/workbench/services/untitled/common/untitledEditorService.ts b/src/vs/workbench/services/untitled/common/untitledEditorService.ts index cf28c9df72e..3749c9a1416 100644 --- a/src/vs/workbench/services/untitled/common/untitledEditorService.ts +++ b/src/vs/workbench/services/untitled/common/untitledEditorService.ts @@ -186,11 +186,6 @@ export class UntitledEditorService implements IUntitledEditorService { } const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId); - if (input.isDirty()) { - setTimeout(() => { - this._onDidChangeDirty.fire(resource); - }, 0 /* prevent race condition between creating input and emitting dirty event */); - } const contentListener = input.onDidModelChangeContent(() => { this._onDidChangeContent.fire(resource);