outputActions.ts 7.4 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 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
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';
import {IOutputChannelRegistry, Extensions, IOutputService, OUTPUT_EDITOR_INPUT_ID, OUTPUT_MODE_ID} from 'vs/workbench/parts/output/common/output';
import {OutputEditorInput} from 'vs/workbench/parts/output/browser/outputEditorInput';
import {SelectActionItem} from 'vs/base/browser/ui/actionbar/actionbar';
import {IWorkbenchEditorService}  from 'vs/workbench/services/editor/common/editorService';
import {IQuickOpenService} from 'vs/workbench/services/quickopen/browser/quickOpenService';
import {IInstantiationService, INullService} from 'vs/platform/instantiation/common/instantiation';

export class GlobalShowOutputAction extends Action {

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

	constructor(
		id: string,
		label: string,
		@IOutputService private outputService: IOutputService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IQuickOpenService private quickOpenService: IQuickOpenService
	) {
		super(id, label);

		this.order = 20; // Allow other actions to position before or after
		this.class = 'output-action showoutput';
	}

	public run(event?: any): Promise {
		let channelToOpen: string = null;

		// Check for previously opened output
		let channels = <OutputEditorInput[]>this.quickOpenService.getEditorHistory().filter((i) => i instanceof OutputEditorInput);
		if (channels.length > 0) {

			// See if output is already opened and just focus it
			let editors = this.editorService.getVisibleEditors();
			if (editors.some((e) => {
				if (e.input instanceof OutputEditorInput) {
					this.editorService.focusEditor(e);

					return true;
				}

				return false;
			})) {
				return Promise.as(null);
			}

			// Otherwise pick a channel from the list
			channelToOpen = channels[0].getChannel();
		}

67
		// Fallback to any contributed channel otherwise if we don't have history
E
Erich Gamma 已提交
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 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185
		else {
			channelToOpen = (<IOutputChannelRegistry>Registry.as(Extensions.OutputChannels)).getChannels()[0];
		}

		let sideBySide = !!(event && (event.ctrlKey || event.metaKey));

		return this.outputService.showOutput(channelToOpen, sideBySide, false /* Do not preserve Focus */);
	}
}

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,
		@IQuickOpenService private quickOpenService: IQuickOpenService,
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
		@IInstantiationService private instantiationService: IInstantiationService
	) {
		super(id, label);
	}

	public run(event?: any): Promise {
		let activeInput = this.editorService.getActiveEditorInput();

		// Restore Previous Non-Output Editor
		if (activeInput instanceof OutputEditorInput) {
			let history = this.quickOpenService.getEditorHistory();
			for (let i = 1; i < history.length; i++) {
				if (!(history[i] instanceof OutputEditorInput)) {
					return this.editorService.openEditor(history[i]);
				}
			}
		}

		// Show Output
		else {
			let action = this.instantiationService.createInstance(GlobalShowOutputAction, GlobalShowOutputAction.ID, GlobalShowOutputAction.LABEL);
			action.run().done(() => action.dispose(), errors.onUnexpectedError);
		}

		return Promise.as(true);
	}
}

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,
		@IWorkbenchEditorService private myEditorService: IWorkbenchEditorService
	) {
		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> {
		let input = this.myEditorService.getActiveEditorInput();
		if (input && input.getId() === OUTPUT_EDITOR_INPUT_ID) {
			let outputEditorInput = <OutputEditorInput>input;
			outputEditorInput.clearOutput();

			return Promise.as(true);
		}

		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;
186
	private outputListenerDispose: IDisposable;
E
Erich Gamma 已提交
187 188 189 190 191 192 193 194 195

	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;
196

197
		this.outputListenerDispose = this.outputService.onOutputChannel(this.onOutputChannel, this);
E
Erich Gamma 已提交
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
	}

	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();

218 219 220 221 222
		if (this.outputListenerDispose) {
			this.outputListenerDispose.dispose();
			delete this.outputListenerDispose;
		}

E
Erich Gamma 已提交
223 224 225
		delete this.input;
	}
}