diff --git a/src/vs/workbench/common/editor/untitledEditorInput.ts b/src/vs/workbench/common/editor/untitledEditorInput.ts index f63ef3249cd..ba09dc49bb2 100644 --- a/src/vs/workbench/common/editor/untitledEditorInput.ts +++ b/src/vs/workbench/common/editor/untitledEditorInput.ts @@ -92,7 +92,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { public suggestFileName(): string { if (!this.hasAssociatedFilePath) { - let mime = this.getMime(); + const mime = this.getMime(); if (mime && mime !== MIME_TEXT /* do not suggest when the mime type is simple plain text */) { return suggestFilename(mime, this.getName()); } @@ -131,7 +131,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { } // Otherwise Create Model and load - let model = this.createModel(); + const model = this.createModel(); return model.load().then((resolvedModel: UntitledEditorModel) => { this.cachedModel = resolvedModel; @@ -140,10 +140,10 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { } private createModel(): UntitledEditorModel { - let content = ''; + const content = ''; let mime = this.modeId; if (!mime && this.hasAssociatedFilePath) { - let mimeFromPath = guessMimeTypes(this.resource.fsPath)[0]; + const mimeFromPath = guessMimeTypes(this.resource.fsPath)[0]; if (!isUnspecific(mimeFromPath)) { mime = mimeFromPath; // take most specific mime type if file path is associated and mime is specific } @@ -163,7 +163,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput { } if (otherInput instanceof UntitledEditorInput) { - let otherUntitledEditorInput = otherInput; + const otherUntitledEditorInput = otherInput; // Otherwise compare by properties return otherUntitledEditorInput.resource.toString() === this.resource.toString(); diff --git a/src/vs/workbench/common/editor/untitledEditorModel.ts b/src/vs/workbench/common/editor/untitledEditorModel.ts index 9b53bcbe6a9..42192160cd2 100644 --- a/src/vs/workbench/common/editor/untitledEditorModel.ts +++ b/src/vs/workbench/common/editor/untitledEditorModel.ts @@ -9,6 +9,7 @@ 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 URI from 'vs/base/common/uri'; +import {IModelContentChangedEvent2} from 'vs/editor/common/editorCommon'; import {EventType, EndOfLinePreference} from 'vs/editor/common/editorCommon'; import {EventType as WorkbenchEventType, ResourceEvent} from 'vs/workbench/common/events'; import {IFilesConfiguration} from 'vs/platform/files/common/files'; @@ -30,6 +31,8 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS private configuredEncoding: string; private preferredEncoding: string; + private hasAssociatedFilePath: boolean; + constructor( value: string, mime: string, @@ -42,6 +45,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS ) { super(value, mime, resource, modeService, modelService); + this.hasAssociatedFilePath = hasAssociatedFilePath; this.dirty = hasAssociatedFilePath; // untitled associated to file path are dirty right away this._onDidChangeDirty = new Emitter(); @@ -92,7 +96,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS } public setEncoding(encoding: string): void { - let oldEncoding = this.getEncoding(); + const oldEncoding = this.getEncoding(); this.preferredEncoding = encoding; // Emit if it changed @@ -119,7 +123,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS this.configuredEncoding = configuration && configuration.files && configuration.files.encoding; // Listen to content changes - this.textModelChangeListener = this.textEditorModel.onDidChangeContent(() => this.onModelContentChanged()); + this.textModelChangeListener = this.textEditorModel.onDidChangeContent(e => this.onModelContentChanged(e)); // Emit initial dirty event if we are if (this.dirty) { @@ -132,11 +136,20 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS }); } - private onModelContentChanged(): void { + private onModelContentChanged(e:IModelContentChangedEvent2): void { + + // turn dirty if we were not if (!this.dirty) { this.dirty = true; this._onDidChangeDirty.fire(); } + + // mark the untitled editor as non-dirty once its content becomes empty and we do + // not have an associated path set + else if (!this.hasAssociatedFilePath && !e.text && this.textEditorModel.getValueLength() === 0) { + this.dirty = false; + this._onDidChangeDirty.fire(); + } } public dispose(): void { diff --git a/src/vs/workbench/services/untitled/common/untitledEditorService.ts b/src/vs/workbench/services/untitled/common/untitledEditorService.ts index 0b522fd1960..969f8b62474 100644 --- a/src/vs/workbench/services/untitled/common/untitledEditorService.ts +++ b/src/vs/workbench/services/untitled/common/untitledEditorService.ts @@ -109,7 +109,7 @@ export class UntitledEditorService implements IUntitledEditorService { } public isDirty(resource: URI): boolean { - let input = this.get(resource); + const input = this.get(resource); return input && input.isDirty(); } @@ -152,7 +152,7 @@ export class UntitledEditorService implements IUntitledEditorService { } while (Object.keys(UntitledEditorService.CACHE).indexOf(resource.toString()) >= 0); } - let input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId); + const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId); const listener = input.onDidChangeDirty(() => { this._onDidChangeDirty.fire(new UntitledEditorEvent(resource));