mirror of
https://github.com/microsoft/vscode.git
synced 2026-04-26 03:29:00 +01:00
move inputs/models from workbench browser => common
This commit is contained in:
98
src/vs/workbench/common/editor/resourceEditorInput.ts
Normal file
98
src/vs/workbench/common/editor/resourceEditorInput.ts
Normal file
@@ -0,0 +1,98 @@
|
||||
/*---------------------------------------------------------------------------------------------
|
||||
* Copyright (c) Microsoft Corporation. All rights reserved.
|
||||
* Licensed under the MIT License. See License.txt in the project root for license information.
|
||||
*--------------------------------------------------------------------------------------------*/
|
||||
'use strict';
|
||||
|
||||
import {TPromise} from 'vs/base/common/winjs.base';
|
||||
import {EditorModel, EditorInput} from 'vs/workbench/common/editor';
|
||||
import {ResourceEditorModel} from 'vs/workbench/common/editor/resourceEditorModel';
|
||||
import URI from 'vs/base/common/uri';
|
||||
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
|
||||
import {IModelService} from 'vs/editor/common/services/modelService';
|
||||
|
||||
/**
|
||||
* A read-only text editor input whos contents are made of the provided resource that points to an existing
|
||||
* code editor model.
|
||||
*/
|
||||
export class ResourceEditorInput extends EditorInput {
|
||||
|
||||
public static ID = 'workbench.editors.resourceEditorInput';
|
||||
|
||||
protected cachedModel: ResourceEditorModel;
|
||||
protected resource: URI;
|
||||
|
||||
private name: string;
|
||||
private description: string;
|
||||
|
||||
constructor(
|
||||
name: string,
|
||||
description: string,
|
||||
resource: URI,
|
||||
@IModelService protected modelService: IModelService,
|
||||
@IInstantiationService protected instantiationService: IInstantiationService
|
||||
) {
|
||||
super();
|
||||
|
||||
this.name = name;
|
||||
this.description = description;
|
||||
this.resource = resource;
|
||||
}
|
||||
|
||||
public getId(): string {
|
||||
return ResourceEditorInput.ID;
|
||||
}
|
||||
|
||||
public getName(): string {
|
||||
return this.name;
|
||||
}
|
||||
|
||||
public getDescription(): string {
|
||||
return this.description;
|
||||
}
|
||||
|
||||
public resolve(refresh?: boolean): TPromise<EditorModel> {
|
||||
|
||||
// We need the resource to point to an existing model
|
||||
if (!this.modelService.getModel(this.resource)) {
|
||||
return TPromise.wrapError(new Error(`Document with resource ${this.resource.toString()} does not exist`));
|
||||
}
|
||||
|
||||
// Use Cached Model
|
||||
if (this.cachedModel) {
|
||||
return TPromise.as<EditorModel>(this.cachedModel);
|
||||
}
|
||||
|
||||
//Otherwise Create Model and Load
|
||||
let model = this.instantiationService.createInstance(ResourceEditorModel, this.resource);
|
||||
return model.load().then((resolvedModel: ResourceEditorModel) => {
|
||||
this.cachedModel = resolvedModel;
|
||||
|
||||
return this.cachedModel;
|
||||
});
|
||||
}
|
||||
|
||||
public matches(otherInput: any): boolean {
|
||||
if (super.matches(otherInput) === true) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if (otherInput instanceof ResourceEditorInput) {
|
||||
let otherResourceEditorInput = <ResourceEditorInput>otherInput;
|
||||
|
||||
// Compare by properties
|
||||
return otherResourceEditorInput.resource.toString() === this.resource.toString();
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public dispose(): void {
|
||||
if (this.cachedModel) {
|
||||
this.cachedModel.dispose();
|
||||
this.cachedModel = null;
|
||||
}
|
||||
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user