mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-25 19:18:59 +01:00
@@ -28,7 +28,6 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
public static SCHEMA: string = 'untitled';
|
||||
|
||||
private resource: URI;
|
||||
private restoreResource: URI;
|
||||
private hasAssociatedFilePath: boolean;
|
||||
private modeId: string;
|
||||
private cachedModel: UntitledEditorModel;
|
||||
@@ -47,6 +46,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
@ITextFileService private textFileService: ITextFileService
|
||||
) {
|
||||
super();
|
||||
|
||||
this.resource = resource;
|
||||
this.hasAssociatedFilePath = hasAssociatedFilePath;
|
||||
this.modeId = modeId;
|
||||
@@ -66,10 +66,6 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
return this.resource;
|
||||
}
|
||||
|
||||
public setRestoreResource(resource: URI): void {
|
||||
this.restoreResource = resource;
|
||||
}
|
||||
|
||||
public getName(): string {
|
||||
return this.hasAssociatedFilePath ? paths.basename(this.resource.fsPath) : this.resource.fsPath;
|
||||
}
|
||||
@@ -134,25 +130,17 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
|
||||
return TPromise.as(this.cachedModel);
|
||||
}
|
||||
|
||||
// Otherwise Create Model and load, restoring from backup if necessary
|
||||
let restorePromise: TPromise<string>;
|
||||
if (this.restoreResource) {
|
||||
restorePromise = this.textFileService.resolveTextContent(this.restoreResource).then(rawTextContent => rawTextContent.value.lines.join('\n'));
|
||||
} else {
|
||||
restorePromise = TPromise.as('');
|
||||
}
|
||||
// Otherwise Create Model and load
|
||||
const model = this.createModel();
|
||||
return model.load().then((resolvedModel: UntitledEditorModel) => {
|
||||
this.cachedModel = resolvedModel;
|
||||
|
||||
return restorePromise.then(content => {
|
||||
const model = this.createModel(content);
|
||||
return model.load().then((resolvedModel: UntitledEditorModel) => {
|
||||
this.cachedModel = resolvedModel;
|
||||
|
||||
return this.cachedModel;
|
||||
});
|
||||
return this.cachedModel;
|
||||
});
|
||||
}
|
||||
|
||||
private createModel(content: string): UntitledEditorModel {
|
||||
private createModel(): UntitledEditorModel {
|
||||
const content = '';
|
||||
const model = this.instantiationService.createInstance(UntitledEditorModel, content, this.modeId, this.resource, this.hasAssociatedFilePath);
|
||||
|
||||
// re-emit some events from the model
|
||||
|
||||
@@ -11,7 +11,7 @@ import { StringEditorModel } from 'vs/workbench/common/editor/stringEditorModel'
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry';
|
||||
import { EndOfLinePreference } from 'vs/editor/common/editorCommon';
|
||||
import { IFileService, IFilesConfiguration } from 'vs/platform/files/common/files';
|
||||
import { IFilesConfiguration } from 'vs/platform/files/common/files';
|
||||
import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
|
||||
import { IModeService } from 'vs/editor/common/services/modeService';
|
||||
import { IModelService } from 'vs/editor/common/services/modelService';
|
||||
@@ -31,8 +31,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
|
||||
|
||||
private hasAssociatedFilePath: boolean;
|
||||
|
||||
private backupPromises: TPromise<void>[];
|
||||
|
||||
constructor(
|
||||
value: string,
|
||||
modeId: string,
|
||||
@@ -40,19 +38,16 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
|
||||
hasAssociatedFilePath: boolean,
|
||||
@IModeService modeService: IModeService,
|
||||
@IModelService modelService: IModelService,
|
||||
@IFileService private fileService: IFileService,
|
||||
@IConfigurationService private configurationService: IConfigurationService
|
||||
) {
|
||||
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; // untitled associated to file path are dirty right away
|
||||
|
||||
this._onDidChangeDirty = new Emitter<void>();
|
||||
this._onDidChangeEncoding = new Emitter<void>();
|
||||
|
||||
this.backupPromises = [];
|
||||
|
||||
this.registerListeners();
|
||||
}
|
||||
|
||||
@@ -116,10 +111,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
|
||||
return this.dirty;
|
||||
}
|
||||
|
||||
public getResource(): URI {
|
||||
return this.resource;
|
||||
}
|
||||
|
||||
public revert(): void {
|
||||
this.dirty = false;
|
||||
|
||||
@@ -162,15 +153,6 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
|
||||
else if (!this.dirty) {
|
||||
this.dirty = true;
|
||||
this._onDidChangeDirty.fire();
|
||||
|
||||
}
|
||||
|
||||
if (this.fileService.isHotExitEnabled()) {
|
||||
if (this.dirty) {
|
||||
this.doBackup();
|
||||
} else {
|
||||
this.fileService.discardBackup(this.resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -189,36 +171,5 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
|
||||
|
||||
this._onDidChangeDirty.dispose();
|
||||
this._onDidChangeEncoding.dispose();
|
||||
|
||||
this.cancelBackupPromises();
|
||||
this.fileService.discardBackup(this.resource);
|
||||
}
|
||||
|
||||
public backup(): TPromise<void> {
|
||||
return this.doBackup(true);
|
||||
}
|
||||
|
||||
private doBackup(immediate?: boolean): TPromise<void> {
|
||||
// Cancel any currently running backups to make this the one that succeeds
|
||||
this.cancelBackupPromises();
|
||||
|
||||
if (immediate) {
|
||||
return this.fileService.backupFile(this.resource, this.getValue()).then(f => void 0);
|
||||
}
|
||||
|
||||
// Create new backup promise and keep it
|
||||
const promise = TPromise.timeout(1000).then(() => {
|
||||
this.fileService.backupFile(this.resource, this.getValue()); // Very important here to not return the promise because if the timeout promise is canceled it will bubble up the error otherwise - do not change
|
||||
});
|
||||
|
||||
this.backupPromises.push(promise);
|
||||
|
||||
return promise;
|
||||
}
|
||||
|
||||
private cancelBackupPromises(): void {
|
||||
while (this.backupPromises.length) {
|
||||
this.backupPromises.pop().cancel();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user