ReferenceCollection in TextModelResolverService

This commit is contained in:
Joao Moreno
2016-11-21 11:30:37 +01:00
parent 17361598b8
commit 5e747c32bd
14 changed files with 256 additions and 169 deletions

View File

@@ -7,6 +7,7 @@
import { TPromise } from 'vs/base/common/winjs.base';
import { EditorInput, ITextEditorModel } from 'vs/workbench/common/editor';
import URI from 'vs/base/common/uri';
import { IReference } from 'vs/base/common/lifecycle';
import { telemetryURIDescriptor } from 'vs/platform/telemetry/common/telemetry';
import { ITextModelResolverService } from 'vs/editor/common/services/resolverService';
import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorModel';
@@ -17,9 +18,9 @@ import { ResourceEditorModel } from 'vs/workbench/common/editor/resourceEditorMo
*/
export class ResourceEditorInput extends EditorInput {
public static ID: string = 'workbench.editors.resourceEditorInput';
static ID: string = 'workbench.editors.resourceEditorInput';
protected cachedModel: ResourceEditorModel;
protected modelReference: IReference<TPromise<ResourceEditorModel>>;
protected resource: URI;
private name: string;
@@ -38,64 +39,60 @@ export class ResourceEditorInput extends EditorInput {
this.resource = resource;
}
public getTypeId(): string {
getTypeId(): string {
return ResourceEditorInput.ID;
}
public getName(): string {
getName(): string {
return this.name;
}
public setName(name: string): void {
setName(name: string): void {
if (this.name !== name) {
this.name = name;
this._onDidChangeLabel.fire();
}
}
public getDescription(): string {
getDescription(): string {
return this.description;
}
public setDescription(description: string): void {
setDescription(description: string): void {
if (this.description !== description) {
this.description = description;
this._onDidChangeLabel.fire();
}
}
public getTelemetryDescriptor(): { [key: string]: any; } {
getTelemetryDescriptor(): { [key: string]: any; } {
const descriptor = super.getTelemetryDescriptor();
descriptor['resource'] = telemetryURIDescriptor(this.resource);
return descriptor;
}
public resolve(refresh?: boolean): TPromise<ITextEditorModel> {
// Use Cached Model
if (this.cachedModel) {
return TPromise.as<ITextEditorModel>(this.cachedModel);
resolve(refresh?: boolean): TPromise<ITextEditorModel> {
if (!this.modelReference) {
this.modelReference = this.textModelResolverService.getModelReference(this.resource);
}
// Otherwise Create Model and handle dispose event
return this.textModelResolverService.resolve(this.resource).then(model => {
const modelPromise = this.modelReference.object;
return modelPromise.then(model => {
if (!(model instanceof ResourceEditorModel)) {
this.modelReference.dispose();
this.modelReference = null;
return TPromise.wrapError(`Unexpected model for ResourceInput: ${this.resource}`); // TODO@Ben eventually also files should be supported, but we guard due to the dangerous dispose of the model in dispose()
}
this.cachedModel = model;
// TODO@Joao this should never happen
model.onDispose(() => this.dispose());
const unbind = model.onDispose(() => {
this.cachedModel = null; // make sure we do not dispose model again
unbind.dispose();
this.dispose();
});
return this.cachedModel;
return model;
});
}
public matches(otherInput: any): boolean {
matches(otherInput: any): boolean {
if (super.matches(otherInput) === true) {
return true;
}
@@ -110,10 +107,10 @@ export class ResourceEditorInput extends EditorInput {
return false;
}
public dispose(): void {
if (this.cachedModel) {
this.cachedModel.dispose();
this.cachedModel = null;
dispose(): void {
if (this.modelReference) {
this.modelReference.dispose();
this.modelReference = null;
}
super.dispose();