diff --git a/src/vs/workbench/common/editor/untitledEditorModel.ts b/src/vs/workbench/common/editor/untitledEditorModel.ts index 6d4507fa1f4..9f9679e786c 100644 --- a/src/vs/workbench/common/editor/untitledEditorModel.ts +++ b/src/vs/workbench/common/editor/untitledEditorModel.ts @@ -7,7 +7,7 @@ import { IDisposable } from 'vs/base/common/lifecycle'; import { TPromise } from 'vs/base/common/winjs.base'; import { EditorModel, IEncodingSupport } from 'vs/workbench/common/editor'; -import { StringEditorModel } from 'vs/workbench/common/editor/stringEditorModel'; +import { BaseTextEditorModel } from 'vs/workbench/common/editor/textEditorModel'; import URI from 'vs/base/common/uri'; import { PLAINTEXT_MODE_ID } from 'vs/editor/common/modes/modesRegistry'; import { EndOfLinePreference } from 'vs/editor/common/editorCommon'; @@ -21,7 +21,7 @@ import { RunOnceScheduler } from 'vs/base/common/async'; import { IBackupFileService, BACKUP_FILE_RESOLVE_OPTIONS } from 'vs/workbench/services/backup/common/backup'; import { ITextFileService } from 'vs/workbench/services/textfile/common/textfiles'; -export class UntitledEditorModel extends StringEditorModel implements IEncodingSupport { +export class UntitledEditorModel extends BaseTextEditorModel implements IEncodingSupport { public static DEFAULT_CONTENT_CHANGE_BUFFER_DELAY = CONTENT_CHANGE_EVENT_BUFFER_DELAY; @@ -43,8 +43,8 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS private hasAssociatedFilePath: boolean; constructor( - modeId: string, - resource: URI, + private modeId: string, + private resource: URI, hasAssociatedFilePath: boolean, @IModeService modeService: IModeService, @IModelService modelService: IModelService, @@ -52,7 +52,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS @ITextFileService private textFileService: ITextFileService, @IConfigurationService private configurationService: IConfigurationService ) { - super('', modeId, resource, modeService, modelService); + super(modelService, modeService); this.hasAssociatedFilePath = hasAssociatedFilePath; this.dirty = false; @@ -167,13 +167,11 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS return null; }).then(backupContent => { - if (backupContent) { - this.setValue(backupContent); - } - this.setDirty(this.hasAssociatedFilePath || !!backupContent); // untitled associated to file path are dirty right away as well as untitled with content + // untitled associated to file path are dirty right away as well as untitled with content + this.setDirty(this.hasAssociatedFilePath || !!backupContent); - return super.load().then(model => { + return this.doLoad(backupContent || '').then(model => { const configuration = this.configurationService.getConfiguration(); // Encoding @@ -187,6 +185,21 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS }); } + private doLoad(content: string): TPromise { + + // Create text editor model if not yet done + if (!this.textEditorModel) { + return this.createTextEditorModel(content, this.resource, this.modeId); + } + + // Otherwise update + else { + this.updateTextEditorModel(content); + } + + return TPromise.as(this); + } + private onModelContentChanged(): void { this.versionId++; diff --git a/src/vs/workbench/services/textfile/test/textFileService.test.ts b/src/vs/workbench/services/textfile/test/textFileService.test.ts index b10e5b3ecd1..4d3887107d8 100644 --- a/src/vs/workbench/services/textfile/test/textFileService.test.ts +++ b/src/vs/workbench/services/textfile/test/textFileService.test.ts @@ -163,7 +163,7 @@ suite('Files - TextFileService', () => { return untitled.resolve().then((model: UntitledEditorModel) => { assert.ok(!service.isDirty(untitled.getResource())); assert.equal(service.getDirty().length, 1); - model.setValue('changed'); + model.textEditorModel.setValue('changed'); assert.ok(service.isDirty(untitled.getResource())); assert.equal(service.getDirty().length, 2); diff --git a/src/vs/workbench/test/common/editor/untitledEditor.test.ts b/src/vs/workbench/test/common/editor/untitledEditor.test.ts index 0da7ca4eada..251def9a044 100644 --- a/src/vs/workbench/test/common/editor/untitledEditor.test.ts +++ b/src/vs/workbench/test/common/editor/untitledEditor.test.ts @@ -162,21 +162,21 @@ suite('Workbench - Untitled Editor', () => { }); input.resolve().then((model: UntitledEditorModel) => { - model.setValue('foo'); + model.textEditorModel.setValue('foo'); assert.equal(counter, 0, 'Dirty model should not trigger event immediately'); TPromise.timeout(3).then(() => { assert.equal(counter, 1, 'Dirty model should trigger event'); - model.setValue('bar'); + model.textEditorModel.setValue('bar'); TPromise.timeout(3).then(() => { assert.equal(counter, 2, 'Content change when dirty should trigger event'); - model.setValue(''); + model.textEditorModel.setValue(''); TPromise.timeout(3).then(() => { assert.equal(counter, 3, 'Manual revert should trigger event'); - model.setValue('foo'); + model.textEditorModel.setValue('foo'); TPromise.timeout(3).then(() => { assert.equal(counter, 4, 'Dirty model should trigger event');