terminal.contribution.ts 5.4 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 nls = require('vs/nls');
A
Alex Dima 已提交
8
import {TPromise} from 'vs/base/common/winjs.base';
E
Erich Gamma 已提交
9 10 11
import {Registry} from 'vs/platform/platform';
import baseplatform = require('vs/base/common/platform');
import {IAction, Action} from 'vs/base/common/actions';
12
import {IWorkbenchActionRegistry, Extensions as ActionExtensions} from 'vs/workbench/common/actionRegistry';
E
Erich Gamma 已提交
13 14 15 16 17 18 19 20
import paths = require('vs/base/common/paths');
import {Scope, IActionBarRegistry, Extensions as ActionBarExtensions, ActionBarContributor} from 'vs/workbench/browser/actionBarRegistry';
import uri from 'vs/base/common/uri';
import {asFileResource} from 'vs/workbench/parts/files/common/files';
import {IWorkspaceContextService} from 'vs/workbench/services/workspace/common/contextService';
import {ITerminalService} from 'vs/workbench/parts/execution/common/execution';
import {SyncActionDescriptor} from 'vs/platform/actions/common/actions';
import {IInstantiationService} from 'vs/platform/instantiation/common/instantiation';
21 22
import {IWorkbenchEditorService} from 'vs/workbench/services/editor/common/editorService';
import {asFileEditorInput} from 'vs/workbench/common/editor';
E
Erich Gamma 已提交
23
import {KeyMod, KeyCode} from 'vs/base/common/keyCodes';
24
import {Extensions, IConfigurationRegistry} from 'vs/platform/configuration/common/configurationRegistry';
25
import {DEFAULT_TERMINAL_WINDOWS, DEFAULT_TERMINAL_LINUX, DEFAULT_TERMINAL_OSX} from 'vs/workbench/parts/execution/electron-browser/terminal';
26 27 28

let configurationRegistry = <IConfigurationRegistry>Registry.as(Extensions.Configuration);
configurationRegistry.registerConfiguration({
29
	'id': 'externalTerminal',
30
	'order': 100,
31
	'title': nls.localize('terminalConfigurationTitle', "External Terminal"),
32 33
	'type': 'object',
	'properties': {
34
		'terminal.external.windowsExec': {
35
			'type': 'string',
36
			'description': nls.localize('terminal.external.windowsExec', "Customizes which terminal to run on Windows."),
37 38
			'default': DEFAULT_TERMINAL_WINDOWS
		},
39
		'terminal.external.osxExec': {
40
			'type': 'string',
41 42
			'description': nls.localize('terminal.external.osxExec', "Customizes which terminal application to run on OS X."),
			'default': DEFAULT_TERMINAL_OSX
43
		},
44
		'terminal.external.linuxExec': {
45
			'type': 'string',
46
			'description': nls.localize('terminal.external.linuxExec', "Customizes which terminal to run on Linux."),
47
			'default': DEFAULT_TERMINAL_LINUX
48 49 50
		}
	}
});
E
Erich Gamma 已提交
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65

export class OpenConsoleAction extends Action {

	public static ID = 'workbench.action.terminal.openNativeConsole';
	public static Label = baseplatform.isWindows ? nls.localize('globalConsoleActionWin', "Open New Command Prompt") :
		nls.localize('globalConsoleActionMacLinux', "Open New Terminal");
	public static ScopedLabel = baseplatform.isWindows ? nls.localize('scopedConsoleActionWin', "Open in Command Prompt") :
		nls.localize('scopedConsoleActionMacLinux', "Open in Terminal");

	private resource: uri;

	constructor(
		id: string,
		label: string,
		@ITerminalService private terminalService: ITerminalService,
66
		@IWorkbenchEditorService private editorService: IWorkbenchEditorService,
E
Erich Gamma 已提交
67 68 69 70 71 72 73 74 75 76 77 78
		@IWorkspaceContextService private contextService: IWorkspaceContextService
	) {
		super(id, label);

		this.order = 49; // Allow other actions to position before or after
	}

	public setResource(resource: uri): void {
		this.resource = resource;
		this.enabled = !paths.isUNC(this.resource.fsPath);
	}

A
Alex Dima 已提交
79
	public run(event?: any): TPromise<any> {
80 81 82
		let pathToOpen: string;

		// Try workspace path first
E
Erich Gamma 已提交
83
		let workspace = this.contextService.getWorkspace();
84 85 86 87 88 89 90 91 92
		pathToOpen = this.resource ? this.resource.fsPath : (workspace && workspace.resource.fsPath);

		// Otherwise check if we have an active file open
		if (!pathToOpen) {
			const file = asFileEditorInput(this.editorService.getActiveEditorInput(), true);
			if (file) {
				pathToOpen = paths.dirname(file.getResource().fsPath); // take parent folder of file
			}
		}
E
Erich Gamma 已提交
93

94
		this.terminalService.openTerminal(pathToOpen);
E
Erich Gamma 已提交
95

A
Alex Dima 已提交
96
		return TPromise.as(null);
E
Erich Gamma 已提交
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
	}
}

class FileViewerActionContributor extends ActionBarContributor {

	constructor( @IInstantiationService private instantiationService: IInstantiationService) {
		super();
	}

	public hasSecondaryActions(context: any): boolean {
		return !!asFileResource(context.element);
	}

	public getSecondaryActions(context: any): IAction[] {
		let fileResource = asFileResource(context.element);
		let resource = fileResource.resource;
		if (!fileResource.isDirectory) {
			resource = uri.file(paths.dirname(resource.fsPath));
		}

		let action = this.instantiationService.createInstance(OpenConsoleAction, OpenConsoleAction.ID, OpenConsoleAction.ScopedLabel);
		action.setResource(resource);

		return [action];
	}
}

const actionBarRegistry = <IActionBarRegistry>Registry.as(ActionBarExtensions.Actionbar);
actionBarRegistry.registerActionBarContributor(Scope.VIEWER, FileViewerActionContributor);

// Register Global Action to Open Console
(<IWorkbenchActionRegistry>Registry.as(ActionExtensions.WorkbenchActions)).registerWorkbenchAction(
	new SyncActionDescriptor(
		OpenConsoleAction,
		OpenConsoleAction.ID,
		OpenConsoleAction.Label,
		{ primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_C }
134 135
	),
	'Open New Command Prompt'
136
);