Skip to content

Commit 5e92eb1

Browse files
author
Benjamin Pasero
committed
Empty untitled file should not show up as dirty (fixes microsoft#11554)
1 parent b081995 commit 5e92eb1

3 files changed

Lines changed: 23 additions & 10 deletions

File tree

src/vs/workbench/common/editor/untitledEditorInput.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
9292

9393
public suggestFileName(): string {
9494
if (!this.hasAssociatedFilePath) {
95-
let mime = this.getMime();
95+
const mime = this.getMime();
9696
if (mime && mime !== MIME_TEXT /* do not suggest when the mime type is simple plain text */) {
9797
return suggestFilename(mime, this.getName());
9898
}
@@ -131,7 +131,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
131131
}
132132

133133
// Otherwise Create Model and load
134-
let model = this.createModel();
134+
const model = this.createModel();
135135
return model.load().then((resolvedModel: UntitledEditorModel) => {
136136
this.cachedModel = resolvedModel;
137137

@@ -140,10 +140,10 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
140140
}
141141

142142
private createModel(): UntitledEditorModel {
143-
let content = '';
143+
const content = '';
144144
let mime = this.modeId;
145145
if (!mime && this.hasAssociatedFilePath) {
146-
let mimeFromPath = guessMimeTypes(this.resource.fsPath)[0];
146+
const mimeFromPath = guessMimeTypes(this.resource.fsPath)[0];
147147
if (!isUnspecific(mimeFromPath)) {
148148
mime = mimeFromPath; // take most specific mime type if file path is associated and mime is specific
149149
}
@@ -163,7 +163,7 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
163163
}
164164

165165
if (otherInput instanceof UntitledEditorInput) {
166-
let otherUntitledEditorInput = <UntitledEditorInput>otherInput;
166+
const otherUntitledEditorInput = <UntitledEditorInput>otherInput;
167167

168168
// Otherwise compare by properties
169169
return otherUntitledEditorInput.resource.toString() === this.resource.toString();

src/vs/workbench/common/editor/untitledEditorModel.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {TPromise} from 'vs/base/common/winjs.base';
99
import {EditorModel, IEncodingSupport} from 'vs/workbench/common/editor';
1010
import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel';
1111
import URI from 'vs/base/common/uri';
12+
import {IModelContentChangedEvent2} from 'vs/editor/common/editorCommon';
1213
import {EventType, EndOfLinePreference} from 'vs/editor/common/editorCommon';
1314
import {EventType as WorkbenchEventType, ResourceEvent} from 'vs/workbench/common/events';
1415
import {IFilesConfiguration} from 'vs/platform/files/common/files';
@@ -30,6 +31,8 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
3031
private configuredEncoding: string;
3132
private preferredEncoding: string;
3233

34+
private hasAssociatedFilePath: boolean;
35+
3336
constructor(
3437
value: string,
3538
mime: string,
@@ -42,6 +45,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
4245
) {
4346
super(value, mime, resource, modeService, modelService);
4447

48+
this.hasAssociatedFilePath = hasAssociatedFilePath;
4549
this.dirty = hasAssociatedFilePath; // untitled associated to file path are dirty right away
4650

4751
this._onDidChangeDirty = new Emitter<void>();
@@ -92,7 +96,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
9296
}
9397

9498
public setEncoding(encoding: string): void {
95-
let oldEncoding = this.getEncoding();
99+
const oldEncoding = this.getEncoding();
96100
this.preferredEncoding = encoding;
97101

98102
// Emit if it changed
@@ -119,7 +123,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
119123
this.configuredEncoding = configuration && configuration.files && configuration.files.encoding;
120124

121125
// Listen to content changes
122-
this.textModelChangeListener = this.textEditorModel.onDidChangeContent(() => this.onModelContentChanged());
126+
this.textModelChangeListener = this.textEditorModel.onDidChangeContent(e => this.onModelContentChanged(e));
123127

124128
// Emit initial dirty event if we are
125129
if (this.dirty) {
@@ -132,11 +136,20 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
132136
});
133137
}
134138

135-
private onModelContentChanged(): void {
139+
private onModelContentChanged(e:IModelContentChangedEvent2): void {
140+
141+
// turn dirty if we were not
136142
if (!this.dirty) {
137143
this.dirty = true;
138144
this._onDidChangeDirty.fire();
139145
}
146+
147+
// mark the untitled editor as non-dirty once its content becomes empty and we do
148+
// not have an associated path set
149+
else if (!this.hasAssociatedFilePath && !e.text && this.textEditorModel.getValueLength() === 0) {
150+
this.dirty = false;
151+
this._onDidChangeDirty.fire();
152+
}
140153
}
141154

142155
public dispose(): void {

src/vs/workbench/services/untitled/common/untitledEditorService.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ export class UntitledEditorService implements IUntitledEditorService {
109109
}
110110

111111
public isDirty(resource: URI): boolean {
112-
let input = this.get(resource);
112+
const input = this.get(resource);
113113

114114
return input && input.isDirty();
115115
}
@@ -152,7 +152,7 @@ export class UntitledEditorService implements IUntitledEditorService {
152152
} while (Object.keys(UntitledEditorService.CACHE).indexOf(resource.toString()) >= 0);
153153
}
154154

155-
let input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId);
155+
const input = this.instantiationService.createInstance(UntitledEditorInput, resource, hasAssociatedFilePath, modeId);
156156

157157
const listener = input.onDidChangeDirty(() => {
158158
this._onDidChangeDirty.fire(new UntitledEditorEvent(resource));

0 commit comments

Comments
 (0)