mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-28 12:33:35 +01:00
Merge pull request #15797 from joaomoreno/reference-counted-models
Reference counted (workbench) text models
This commit is contained in:
@@ -12,8 +12,8 @@ import { EditorModel } from 'vs/workbench/common/editor';
|
||||
* and the modified version.
|
||||
*/
|
||||
export class DiffEditorModel extends EditorModel {
|
||||
private _originalModel: EditorModel;
|
||||
private _modifiedModel: EditorModel;
|
||||
protected _originalModel: EditorModel;
|
||||
protected _modifiedModel: EditorModel;
|
||||
|
||||
constructor(originalModel: EditorModel, modifiedModel: EditorModel) {
|
||||
super();
|
||||
|
||||
@@ -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 promise: TPromise<IReference<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.promise) {
|
||||
this.promise = this.textModelResolverService.createModelReference(this.resource);
|
||||
}
|
||||
|
||||
// Otherwise Create Model and handle dispose event
|
||||
return this.textModelResolverService.resolve(this.resource).then(model => {
|
||||
return this.promise.then(ref => {
|
||||
const model = ref.object;
|
||||
|
||||
if (!(model instanceof ResourceEditorModel)) {
|
||||
ref.dispose();
|
||||
this.promise = 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.promise) {
|
||||
this.promise.done(ref => ref.dispose());
|
||||
this.promise = null;
|
||||
}
|
||||
|
||||
super.dispose();
|
||||
|
||||
@@ -23,6 +23,14 @@ export class TextDiffEditorModel extends DiffEditorModel {
|
||||
this.updateTextDiffEditorModel();
|
||||
}
|
||||
|
||||
get originalModel(): BaseTextEditorModel {
|
||||
return this._originalModel as BaseTextEditorModel;
|
||||
}
|
||||
|
||||
get modifiedModel(): BaseTextEditorModel {
|
||||
return this._modifiedModel as BaseTextEditorModel;
|
||||
}
|
||||
|
||||
public load(): TPromise<EditorModel> {
|
||||
return super.load().then(() => {
|
||||
this.updateTextDiffEditorModel();
|
||||
@@ -37,15 +45,15 @@ export class TextDiffEditorModel extends DiffEditorModel {
|
||||
// Create new
|
||||
if (!this._textDiffEditorModel) {
|
||||
this._textDiffEditorModel = {
|
||||
original: (<BaseTextEditorModel>this.originalModel).textEditorModel,
|
||||
modified: (<BaseTextEditorModel>this.modifiedModel).textEditorModel
|
||||
original: this.originalModel.textEditorModel,
|
||||
modified: this.modifiedModel.textEditorModel
|
||||
};
|
||||
}
|
||||
|
||||
// Update existing
|
||||
else {
|
||||
this._textDiffEditorModel.original = (<BaseTextEditorModel>this.originalModel).textEditorModel;
|
||||
this._textDiffEditorModel.modified = (<BaseTextEditorModel>this.modifiedModel).textEditorModel;
|
||||
this._textDiffEditorModel.original = this.originalModel.textEditorModel;
|
||||
this._textDiffEditorModel.modified = this.modifiedModel.textEditorModel;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -9,7 +9,7 @@ import { EndOfLinePreference, IModel, IRawText } from 'vs/editor/common/editorCo
|
||||
import { IMode } from 'vs/editor/common/modes';
|
||||
import { EditorModel } from 'vs/workbench/common/editor';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import { ITextEditorModel } from 'vs/platform/editor/common/editor';
|
||||
import { ITextEditorModel } from 'vs/editor/common/services/resolverService';
|
||||
import { IModeService } from 'vs/editor/common/services/modeService';
|
||||
import { IModelService } from 'vs/editor/common/services/modelService';
|
||||
import { RawText } from 'vs/editor/common/model/textModel';
|
||||
|
||||
Reference in New Issue
Block a user