outputServices.ts 10.0 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Johannes Rieken 已提交
6
import { TPromise } from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
7
import strings = require('vs/base/common/strings');
J
Johannes Rieken 已提交
8
import Event, { Emitter } from 'vs/base/common/event';
9 10
import URI from 'vs/base/common/uri';
import { IDisposable, dispose } from 'vs/base/common/lifecycle';
J
Johannes Rieken 已提交
11 12 13 14 15
import { IEditor } from 'vs/platform/editor/common/editor';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IStorageService, StorageScope } from 'vs/platform/storage/common/storage';
import { Registry } from 'vs/platform/platform';
import { EditorOptions } from 'vs/workbench/common/editor';
16
import { OutputEditors, IOutputEvent, IOutputChannel, IOutputService, Extensions, OUTPUT_PANEL_ID, IOutputChannelRegistry, MAX_OUTPUT_LENGTH, OUTPUT_SCHEME, OUTPUT_MIME } from 'vs/workbench/parts/output/common/output';
J
Johannes Rieken 已提交
17 18 19 20 21
import { OutputPanel } from 'vs/workbench/parts/output/browser/outputPanel';
import { IPanelService } from 'vs/workbench/services/panel/common/panelService';
import { IModelService } from 'vs/editor/common/services/modelService';
import { IWorkspaceContextService } from 'vs/platform/workspace/common/workspace';
import { OutputLinkProvider } from 'vs/workbench/parts/output/common/outputLinkProvider';
22 23 24 25 26 27
import { ITextModelResolverService, ITextModelContentProvider } from 'vs/editor/common/services/resolverService';
import { IModel } from 'vs/editor/common/editorCommon';
import { IModeService } from 'vs/editor/common/services/modeService';
import { RunOnceScheduler } from 'vs/base/common/async';
import { EditOperation } from 'vs/editor/common/core/editOperation';
import { Position } from 'vs/editor/common/core/position';
28 29

const OUTPUT_ACTIVE_CHANNEL_KEY = 'output.activechannel';
E
Erich Gamma 已提交
30 31

export class OutputService implements IOutputService {
B
Benjamin Pasero 已提交
32

33
	public _serviceBrand: any;
E
Erich Gamma 已提交
34 35 36

	private receivedOutput: { [channel: string]: string; };

I
isidor 已提交
37
	private activeChannelId: string;
E
Erich Gamma 已提交
38

39 40
	private _onOutput: Emitter<IOutputEvent>;
	private _onOutputChannel: Emitter<string>;
I
isidor 已提交
41
	private _onActiveOutputChannel: Emitter<string>;
E
Erich Gamma 已提交
42

43 44
	private _outputLinkDetector: OutputLinkProvider;

E
Erich Gamma 已提交
45
	constructor(
46
		@IStorageService private storageService: IStorageService,
E
Erich Gamma 已提交
47
		@IInstantiationService private instantiationService: IInstantiationService,
48
		@IPanelService private panelService: IPanelService,
J
Johannes Rieken 已提交
49
		@IWorkspaceContextService contextService: IWorkspaceContextService,
50 51
		@IModelService modelService: IModelService,
		@ITextModelResolverService textModelResolverService: ITextModelResolverService
E
Erich Gamma 已提交
52
	) {
53 54
		this._onOutput = new Emitter<IOutputEvent>();
		this._onOutputChannel = new Emitter<string>();
I
isidor 已提交
55
		this._onActiveOutputChannel = new Emitter<string>();
E
Erich Gamma 已提交
56 57 58

		this.receivedOutput = Object.create(null);

59
		const channels = Registry.as<IOutputChannelRegistry>(Extensions.OutputChannels).getChannels();
I
isidor 已提交
60
		this.activeChannelId = this.storageService.get(OUTPUT_ACTIVE_CHANNEL_KEY, StorageScope.WORKSPACE, channels && channels.length > 0 ? channels[0].id : null);
61 62

		this._outputLinkDetector = new OutputLinkProvider(contextService, modelService);
63 64 65

		// Register as text model content provider for output
		textModelResolverService.registerTextModelContentProvider(OUTPUT_SCHEME, instantiationService.createInstance(OutputContentProvider, this));
E
Erich Gamma 已提交
66 67
	}

68 69
	public get onOutput(): Event<IOutputEvent> {
		return this._onOutput.event;
E
Erich Gamma 已提交
70 71
	}

72 73
	public get onOutputChannel(): Event<string> {
		return this._onOutputChannel.event;
E
Erich Gamma 已提交
74 75
	}

I
isidor 已提交
76 77 78 79
	public get onActiveOutputChannel(): Event<string> {
		return this._onActiveOutputChannel.event;
	}

I
isidor 已提交
80 81
	public getChannel(id: string): IOutputChannel {
		const channelData = Registry.as<IOutputChannelRegistry>(Extensions.OutputChannels).getChannels().filter(channelData => channelData.id === id).pop();
I
isidor 已提交
82 83

		const self = this;
I
isidor 已提交
84
		return {
I
isidor 已提交
85
			id,
I
isidor 已提交
86
			label: channelData ? channelData.label : id,
87
			get output() {
I
isidor 已提交
88
				return self.getOutput(id);
89
			},
I
isidor 已提交
90
			append: (output: string) => this.append(id, output),
91 92
			show: (preserveFocus: boolean) => this.showOutput(id, preserveFocus),
			clear: () => this.clearOutput(id)
I
isidor 已提交
93 94 95 96
		};
	}

	private append(channelId: string, output: string): void {
97
		
E
Erich Gamma 已提交
98
		// Initialize
I
isidor 已提交
99 100
		if (!this.receivedOutput[channelId]) {
			this.receivedOutput[channelId] = '';
E
Erich Gamma 已提交
101

I
isidor 已提交
102
			this._onOutputChannel.fire(channelId); // emit event that we have a new channel
E
Erich Gamma 已提交
103 104 105 106 107 108 109
		}

		// Sanitize
		output = strings.removeAnsiEscapeCodes(output);

		// Store
		if (output) {
I
isidor 已提交
110
			this.receivedOutput[channelId] = strings.appendWithLimit(this.receivedOutput[channelId], output, MAX_OUTPUT_LENGTH);
E
Erich Gamma 已提交
111 112
		}

I
isidor 已提交
113
		this._onOutput.fire({ output: output, channelId: channelId });
E
Erich Gamma 已提交
114 115
	}

I
isidor 已提交
116 117
	public getActiveChannel(): IOutputChannel {
		return this.getChannel(this.activeChannelId);
E
Erich Gamma 已提交
118 119
	}

I
isidor 已提交
120 121
	private getOutput(channelId: string): string {
		return this.receivedOutput[channelId] || '';
I
isidor 已提交
122 123
	}

124 125
	private clearOutput(channelId: string): void {
		this.receivedOutput[channelId] = '';
E
Erich Gamma 已提交
126

127
		this._onOutput.fire({ channelId: channelId, output: null /* indicator to clear output */ });
E
Erich Gamma 已提交
128 129
	}

130
	private showOutput(channelId: string, preserveFocus?: boolean): TPromise<IEditor> {
131
		const panel = this.panelService.getActivePanel();
132
		if (this.activeChannelId === channelId && panel && panel.getId() === OUTPUT_PANEL_ID) {
133 134
			return TPromise.as(<OutputPanel>panel);
		}
I
isidor 已提交
135

136
		this.activeChannelId = channelId;
I
isidor 已提交
137
		this.storageService.store(OUTPUT_ACTIVE_CHANNEL_KEY, this.activeChannelId, StorageScope.WORKSPACE);
138
		this._onActiveOutputChannel.fire(channelId); // emit event that a new channel is active
139

140
		return this.panelService.openPanel(OUTPUT_PANEL_ID, !preserveFocus).then((outputPanel: OutputPanel) => {
141
			return outputPanel && outputPanel.setInput(OutputEditors.getInstance(this.instantiationService, this.getChannel(channelId)), EditorOptions.create({ preserveFocus: preserveFocus })).
142
				then(() => outputPanel);
I
isidor 已提交
143 144
		});
	}
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
}

class OutputContentProvider implements ITextModelContentProvider {

	private static OUTPUT_DELAY = 300;

	private bufferedOutput: { [channel: string]: string; };
	private appendOutputScheduler: { [channel: string]: RunOnceScheduler; };

	private toDispose: IDisposable[];

	constructor(
		private outputService: IOutputService,
		@IModelService private modelService: IModelService,
		@IModeService private modeService: IModeService,
		@IPanelService private panelService: IPanelService
	) {
		this.bufferedOutput = Object.create(null);
		this.appendOutputScheduler = Object.create(null);
		this.toDispose = [];

		this.registerListeners();
	}

	private registerListeners(): void {
		this.toDispose.push(this.outputService.onOutput(e => this.onOutputReceived(e)));
		this.toDispose.push(this.outputService.onActiveOutputChannel(channel => this.scheduleOutputAppend(channel)));
		this.toDispose.push(this.panelService.onDidPanelOpen(panel => {
			if (panel.getId() === OUTPUT_PANEL_ID) {
				this.appendOutput();
			}
		}));
	}

	private onOutputReceived(e: IOutputEvent): void {
		const model = this.getModel(e.channelId);
		if (!model) {
			return; // only react if we have a known model
		}

		// Append to model
		if (e.output) {
			this.bufferedOutput[e.channelId] = strings.appendWithLimit(this.bufferedOutput[e.channelId] || '', e.output, MAX_OUTPUT_LENGTH);
			this.scheduleOutputAppend(e.channelId);
		}

		// Clear from model
		else if (e.output === null) {
			this.bufferedOutput[e.channelId] = '';
			model.setValue('');
		}
	}

	private getModel(channel: string): IModel {
		return this.modelService.getModel(URI.from({ scheme: OUTPUT_SCHEME, path: channel }));
	}

	private scheduleOutputAppend(channel: string): void {
		if (!this.isVisible(channel)) {
			return; // only if the output channel is visible
		}

		if (!this.bufferedOutput[channel]) {
			return; // only if we have any output to show
		}

		let scheduler = this.appendOutputScheduler[channel];
		if (!scheduler) {
			scheduler = new RunOnceScheduler(() => {
				if (this.isVisible(channel)) {
					this.appendOutput(channel);
				}
			}, OutputContentProvider.OUTPUT_DELAY);

			this.appendOutputScheduler[channel] = scheduler;
			this.toDispose.push(scheduler);
		}

		if (scheduler.isScheduled()) {
			return; // only if not already scheduled
		}

		scheduler.schedule();
	}

	private appendOutput(channel?: string): void {
		if (!channel) {
			const activeChannel = this.outputService.getActiveChannel();
			channel = activeChannel && activeChannel.id;
		}

		if (!channel) {
			return; // return if we do not have a valid channel to append to
		}

		const model = this.getModel(channel);
		if (!model) {
			return; // only react if we have a known model
		}

		const bufferedOutput = this.bufferedOutput[channel];
246
		this.bufferedOutput[channel] = '';
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
		if (!bufferedOutput) {
			return; // return if nothing to append
		}

		// just fill in the full (trimmed) output if we exceed max length
		if (model.getValueLength() + bufferedOutput.length > MAX_OUTPUT_LENGTH) {
			model.setValue(this.outputService.getChannel(channel).output);
		}

		// otherwise append
		else {
			const lastLine = model.getLineCount();
			const lastLineMaxColumn = model.getLineMaxColumn(lastLine);

			model.applyEdits([EditOperation.insert(new Position(lastLine, lastLineMaxColumn), bufferedOutput)]);
		}

		// reveal last line
		const panel = this.panelService.getActivePanel();
		(<OutputPanel>panel).revealLastLine(true);
	}

	private isVisible(channel: string): boolean {
		const panel = this.panelService.getActivePanel();

		return panel && panel.getId() === OUTPUT_PANEL_ID && this.outputService.getActiveChannel().id === channel;
	}

	public provideTextContent(resource: URI): TPromise<IModel> {
		const content = this.outputService.getChannel(resource.fsPath).output;

		let codeEditorModel = this.modelService.getModel(resource);
		if (!codeEditorModel) {
			codeEditorModel = this.modelService.createModel(content, this.modeService.getOrCreateMode(OUTPUT_MIME), resource);
		}

		return TPromise.as(codeEditorModel);
	}

	public dispose(): void {
		this.toDispose = dispose(this.toDispose);
	}
E
Erich Gamma 已提交
289
}