outputActions.ts 5.2 KB
Newer Older
E
Erich Gamma 已提交
1 2 3 4 5 6 7 8 9 10 11
/*---------------------------------------------------------------------------------------------
 *  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 {Promise, TPromise} from 'vs/base/common/winjs.base';
import nls = require('vs/nls');
import {Registry} from 'vs/platform/platform';
import errors = require('vs/base/common/errors');
import arrays = require('vs/base/common/arrays');
B
Benjamin Pasero 已提交
12
import {IDisposable} from 'vs/base/common/lifecycle';
E
Erich Gamma 已提交
13 14 15 16
import {IAction, Action} from 'vs/base/common/actions';
import {EditorAction, Behaviour} from 'vs/editor/common/editorAction';
import {ICommonCodeEditor, IEditorActionDescriptorData} from 'vs/editor/common/editorCommon';
import {EditorInputAction} from 'vs/workbench/browser/parts/editor/baseEditor';
17
import {IOutputChannelRegistry, Extensions, IOutputService, OUTPUT_EDITOR_INPUT_ID, OUTPUT_MODE_ID, OUTPUT_PANEL_ID} from 'vs/workbench/parts/output/common/output';
18
import {OutputEditorInput} from 'vs/workbench/parts/output/common/outputEditorInput';
E
Erich Gamma 已提交
19
import {SelectActionItem} from 'vs/base/browser/ui/actionbar/actionbar';
20
import {IQuickOpenService} from 'vs/workbench/services/quickopen/common/quickOpenService';
21 22
import {IPartService} from 'vs/workbench/services/part/common/partService';
import {IPanelService} from 'vs/workbench/services/panel/common/panelService';
E
Erich Gamma 已提交
23 24 25 26 27 28 29 30 31
import {IInstantiationService, INullService} from 'vs/platform/instantiation/common/instantiation';

export class ToggleOutputAction extends Action {

	public static ID = 'workbench.action.output.toggleOutput';
	public static LABEL = nls.localize('toggleOutput', "Toggle Output");

	constructor(
		id: string, label: string,
32 33 34
		@IPartService private partService: IPartService,
		@IPanelService private panelService: IPanelService,
		@IOutputService private outputService: IOutputService
E
Erich Gamma 已提交
35 36 37 38 39
	) {
		super(id, label);
	}

	public run(event?: any): Promise {
40 41 42
		const panel = this.panelService.getActivePanel();
		if (panel && panel.getId() === OUTPUT_PANEL_ID) {
			this.partService.setPanelHidden(true);
E
Erich Gamma 已提交
43

44
			return Promise.as(null);
E
Erich Gamma 已提交
45 46
		}

47
		return this.outputService.showOutput();
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
	}
}

export class ClearOutputAction extends EditorInputAction {

	constructor( @INullService ns) {
		super('workbench.output.action.clearOutput', nls.localize('clearOutput', "Clear Output"), 'output-action clear-output');
	}

	public run(): Promise {
		let outputEditorInput = <OutputEditorInput>this.input;
		outputEditorInput.clearOutput();

		return Promise.as(true);
	}
}

export class ClearOutputEditorAction extends EditorAction {

	public static ID = 'editor.action.clearoutput';

	constructor(
		descriptor: IEditorActionDescriptorData,
		editor: ICommonCodeEditor,
72
		@IOutputService private outputService: IOutputService
E
Erich Gamma 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
	) {
		super(descriptor, editor, Behaviour.WidgetFocus | Behaviour.ShowInContextMenu);
	}

	public getGroupId(): string {
		return 'clear';
	}

	public isSupported(): boolean {
		let model = this.editor.getModel();
		let mode = model && model.getMode();

		return mode && mode.getId() === OUTPUT_MODE_ID && super.isSupported();
	}

	public run(): TPromise<boolean> {
89
		this.outputService.clearOutput(this.outputService.getActiveChannel());
E
Erich Gamma 已提交
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
		return TPromise.as(false);
	}
}

export class SwitchOutputAction extends EditorInputAction {

	public static ID = 'workbench.output.action.switchBetweenOutputs';

	constructor( @IOutputService private outputService: IOutputService) {
		super(SwitchOutputAction.ID, nls.localize('switchToOutput.label', "Switch to Output"));

		this.class = 'output-action switch-to-output';
	}

	public isEnabled(): boolean {
		return super.isEnabled() && this.input instanceof OutputEditorInput;
	}

	public run(channel?: string): Promise {
		return this.outputService.showOutput(channel);
	}
}

export class SwitchOutputActionItem extends SelectActionItem {
	private input: OutputEditorInput;
115
	private outputListenerDispose: IDisposable;
E
Erich Gamma 已提交
116 117 118 119 120 121 122 123 124

	constructor(
		action: IAction,
		input: OutputEditorInput,
		@IOutputService private outputService: IOutputService
	) {
		super(null, action, SwitchOutputActionItem.getChannels(outputService, input), SwitchOutputActionItem.getChannels(outputService, input).indexOf(input.getChannel()));

		this.input = input;
125

126
		this.outputListenerDispose = this.outputService.onOutputChannel(this.onOutputChannel, this);
E
Erich Gamma 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
	}

	private onOutputChannel(): void {
		let channels = SwitchOutputActionItem.getChannels(this.outputService, this.input);
		let selected = channels.indexOf(this.input.getChannel());

		this.setOptions(channels, selected);
	}

	private static getChannels(outputService: IOutputService, input: OutputEditorInput): string[] {
		const contributedChannels = (<IOutputChannelRegistry>Registry.as(Extensions.OutputChannels)).getChannels();
		const usedChannels = outputService.getChannels();
		usedChannels.push(input.getChannel());

		return arrays.distinct(contributedChannels.concat(usedChannels)).sort(); // sort by name
	}

	public dispose(): void {
		super.dispose();

147 148 149 150 151
		if (this.outputListenerDispose) {
			this.outputListenerDispose.dispose();
			delete this.outputListenerDispose;
		}

E
Erich Gamma 已提交
152 153 154
		delete this.input;
	}
}