提交 5e92eb1e 编写于 作者: B Benjamin Pasero

Empty untitled file should not show up as dirty (fixes #11554)

上级 b0819957
......@@ -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 = <UntitledEditorInput>otherInput;
const otherUntitledEditorInput = <UntitledEditorInput>otherInput;
// Otherwise compare by properties
return otherUntitledEditorInput.resource.toString() === this.resource.toString();
......
......@@ -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<void>();
......@@ -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 {
......
......@@ -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));
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册