logEditorInput.ts 2.6 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7
/*---------------------------------------------------------------------------------------------
 *  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 {ICodeEditor} from 'vs/editor/browser/editorBrowser';
8
import {StringEditorInput} from 'vs/workbench/common/editor/stringEditorInput';
E
Erich Gamma 已提交
9 10 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 47 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
import {BaseTextEditor} from 'vs/workbench/browser/parts/editor/textEditor';
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
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. As a subclass of StringEditorInput
 * it adds additional functionality suitable for using it to show output or logs.
 */
export class LogEditorInput extends StringEditorInput {

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

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

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

	/**
	 * Appends text to the end of this input and automatically reveals the last line if an editor is visible with this input.
	 */
	public append(value: string): void {
		super.append(value);

		this.revealLastLine();
	}

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

		return newValue;
	}

	private revealLastLine(): void {
		let editors = this.editorService.getVisibleEditors();
		for (let i = 0; i < editors.length; i++) {
			let editor = editors[i];
			if (editor.input === this && editor instanceof BaseTextEditor) {
				let textEditor = <BaseTextEditor>editor;
				let editorControl = <ICodeEditor>textEditor.getControl();
				if (editorControl) {
					let model = editorControl.getModel();
					if (model) {
						let lastLine = model.getLineCount();
						editorControl.revealLine(lastLine);
					}
				}
			}
		}
	}
}