stringEditorInput.ts 4.3 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9
/*---------------------------------------------------------------------------------------------
 *  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 {MIME_TEXT} from 'vs/base/common/mime';
import {EditorModel, EditorInput} from 'vs/workbench/common/editor';
10
import {StringEditorModel} from 'vs/workbench/common/editor/stringEditorModel';
E
Erich Gamma 已提交
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
import URI from 'vs/base/common/uri';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';

/**
 * A read-only text editor input whos contents are made of the provided value and mime type.
 */
export class StringEditorInput extends EditorInput {

	public static ID = 'workbench.editors.stringEditorInput';

	protected cachedModel: StringEditorModel;

	private name: string;
	private description: string;
	protected value: string;
	protected mime: string;
	private singleton: boolean;

	constructor(
		name: string,
		description: string,
		value: string,
		mime: string,
		singleton: boolean,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super();

		this.name = name;
		this.description = description;
		this.value = value;
		this.mime = mime || MIME_TEXT;
		this.singleton = singleton;
	}

	protected getResource(): URI {
B
Benjamin Pasero 已提交
47
		// Subclasses can implement to associate a resource with the input
E
Erich Gamma 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
		return null;
	}

	public getId(): string {
		return StringEditorInput.ID;
	}

	public getName(): string {
		return this.name;
	}

	public getDescription(): string {
		return this.description;
	}

	public getValue(): string {
		return this.value;
	}

	public getMime(): string {
		return this.mime;
	}

	/**
	 * Sets the textual value of this input and will also update the underlying model if this input is resolved.
	 */
	public setValue(value: string): void {
		this.value = value;
		if (this.cachedModel) {
			this.cachedModel.setValue(value);
		}
	}

	/**
	 * Clears the textual value of this input and will also update the underlying model if this input is resolved.
	 */
	public clearValue(): void {
		this.value = '';
		if (this.cachedModel) {
			this.cachedModel.clearValue();
		}
	}

	/**
	 * Appends to the textual value of this input and will also update the underlying model if this input is resolved.
	 */
	public append(value: string): void {
		this.value += value;
		if (this.cachedModel) {
			this.cachedModel.append(value);
		}
	}

	/**
	 * Removes all lines from the top if the line number exceeds the given line count. Returns the new value if lines got trimmed.
	 *
	 * Note: This method is a no-op if the input has not yet been resolved.
	 */
	public trim(linecount: number): string {
		if (this.cachedModel) {
			let newValue = this.cachedModel.trim(linecount);
			if (newValue !== null) {
				this.value = newValue;

				return this.value;
			}
		}

		return null;
	}

	public resolve(refresh?: boolean): TPromise<EditorModel> {

		// Use Cached Model
		if (this.cachedModel) {
			return TPromise.as<EditorModel>(this.cachedModel);
		}

		//Otherwise Create Model and Load
		let model = this.instantiationService.createInstance(StringEditorModel, this.value, this.mime, this.getResource());
		return model.load().then((resolvedModel: StringEditorModel) => {
			this.cachedModel = resolvedModel;

			return this.cachedModel;
		});
	}

	public matches(otherInput: any): boolean {
		if (super.matches(otherInput) === true) {
			return true;
		}

		if (otherInput instanceof StringEditorInput) {
			let otherStringEditorInput = <StringEditorInput>otherInput;

			// If both inputs are singletons, check on the mime for equalness
			if (otherStringEditorInput.singleton && this.singleton && otherStringEditorInput.mime === this.mime) {
				return true;
			}

			// Otherwise compare by properties
			return otherStringEditorInput.value === this.value &&
				otherStringEditorInput.mime === this.mime &&
				otherStringEditorInput.description === this.description &&
				otherStringEditorInput.name === this.name;
		}

		return false;
	}

	public dispose(): void {
		if (this.cachedModel) {
			this.cachedModel.dispose();
			this.cachedModel = null;
		}

		super.dispose();
	}
}