introduce backup restorer and use it for untitled

This commit is contained in:
Benjamin Pasero
2016-11-18 11:56:35 +01:00
parent 8408940fa0
commit 4439f3e20b
4 changed files with 61 additions and 8 deletions
@@ -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<void>();
this._onDidChangeDirty = new Emitter<void>();
@@ -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<void> {
@@ -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
(<IWorkbenchContributionsRegistry>Registry.as(WorkbenchExtensions.Workbench)).registerWorkbenchContribution(
// Register Backup Model Tracker
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
BackupModelTracker
);
// Register Backup Restorer
Registry.as<IWorkbenchContributionsRegistry>(WorkbenchExtensions.Workbench).registerWorkbenchContribution(
BackupRestorer
);
@@ -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';
}
}
@@ -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);