typed events for untitled (for #7176)

This commit is contained in:
Benjamin Pasero
2016-09-06 14:24:14 +02:00
parent e5d14006dc
commit b081995783
12 changed files with 88 additions and 66 deletions

View File

@@ -17,7 +17,6 @@ import {IWorkspaceContextService} from 'vs/platform/workspace/common/workspace';
import {IModeService} from 'vs/editor/common/services/modeService';
import {IDisposable, dispose} from 'vs/base/common/lifecycle';
import {IEventService} from 'vs/platform/event/common/event';
import {EventType as WorkbenchEventType, UntitledEditorEvent} from 'vs/workbench/common/events';
import {ITextFileService} from 'vs/workbench/parts/files/common/files'; // TODO@Ben layer breaker
@@ -53,19 +52,6 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
this.hasAssociatedFilePath = hasAssociatedFilePath;
this.modeId = modeId;
this.toUnbind = [];
this.registerListeners();
}
private registerListeners(): void {
this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_SAVED, (e: UntitledEditorEvent) => this.onDirtyStateChange(e)));
this.toUnbind.push(this.eventService.addListener2(WorkbenchEventType.UNTITLED_FILE_DIRTY, (e: UntitledEditorEvent) => this.onDirtyStateChange(e)));
}
private onDirtyStateChange(e: UntitledEditorEvent): void {
if (e.resource.toString() === this.resource.toString()) {
this._onDidChangeDirty.fire();
}
}
public getTypeId(): string {
@@ -163,7 +149,12 @@ export class UntitledEditorInput extends AbstractUntitledEditorInput {
}
}
return this.instantiationService.createInstance(UntitledEditorModel, content, mime || MIME_TEXT, this.resource, this.hasAssociatedFilePath);
const model = this.instantiationService.createInstance(UntitledEditorModel, content, mime || MIME_TEXT, this.resource, this.hasAssociatedFilePath);
// detect dirty state changes on model and re-emit
this.toUnbind.push(model.onDidChangeDirty(() => this._onDidChangeDirty.fire()));
return model;
}
public matches(otherInput: any): boolean {

View File

@@ -10,7 +10,7 @@ import {EditorModel, IEncodingSupport} from 'vs/workbench/common/editor';
import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel';
import URI from 'vs/base/common/uri';
import {EventType, EndOfLinePreference} from 'vs/editor/common/editorCommon';
import {EventType as WorkbenchEventType, UntitledEditorEvent, ResourceEvent} from 'vs/workbench/common/events';
import {EventType as WorkbenchEventType, ResourceEvent} from 'vs/workbench/common/events';
import {IFilesConfiguration} from 'vs/platform/files/common/files';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {IEventService} from 'vs/platform/event/common/event';
@@ -18,12 +18,15 @@ import {IModeService} from 'vs/editor/common/services/modeService';
import {IModelService} from 'vs/editor/common/services/modelService';
import {IMode} from 'vs/editor/common/modes';
import {isUnspecific} from 'vs/base/common/mime';
import Event, {Emitter} from 'vs/base/common/event';
export class UntitledEditorModel extends StringEditorModel implements IEncodingSupport {
private textModelChangeListener: IDisposable;
private configurationChangeListener: IDisposable;
private dirty: boolean;
private _onDidChangeDirty: Emitter<void>;
private configuredEncoding: string;
private preferredEncoding: string;
@@ -41,9 +44,15 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
this.dirty = hasAssociatedFilePath; // untitled associated to file path are dirty right away
this._onDidChangeDirty = new Emitter<void>();
this.registerListeners();
}
public get onDidChangeDirty(): Event<void> {
return this._onDidChangeDirty.event;
}
protected getOrCreateMode(modeService: IModeService, mime: string, firstLineText?: string): TPromise<IMode> {
if (isUnspecific(mime)) {
return modeService.getOrCreateModeByFilenameOrFirstLine(this.resource.fsPath, firstLineText); // lookup mode via resource path if the provided mime is unspecific
@@ -99,7 +108,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
public revert(): void {
this.dirty = false;
this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_SAVED, new UntitledEditorEvent(this.resource));
this._onDidChangeDirty.fire();
}
public load(): TPromise<EditorModel> {
@@ -115,7 +124,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
// Emit initial dirty event if we are
if (this.dirty) {
setTimeout(() => {
this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_DIRTY, new UntitledEditorEvent(this.resource));
this._onDidChangeDirty.fire();
}, 0 /* prevent race condition between creating model and emitting dirty event */);
}
@@ -126,7 +135,7 @@ export class UntitledEditorModel extends StringEditorModel implements IEncodingS
private onModelContentChanged(): void {
if (!this.dirty) {
this.dirty = true;
this.eventService.emit(WorkbenchEventType.UNTITLED_FILE_DIRTY, new UntitledEditorEvent(this.resource));
this._onDidChangeDirty.fire();
}
}