提交 7dc49956 编写于 作者: I isidor

execution: properly contribute actions to explorer

上级 50bc4383
...@@ -8,18 +8,14 @@ import * as nls from 'vs/nls'; ...@@ -8,18 +8,14 @@ import * as nls from 'vs/nls';
import * as env from 'vs/base/common/platform'; import * as env from 'vs/base/common/platform';
import { TPromise } from 'vs/base/common/winjs.base'; import { TPromise } from 'vs/base/common/winjs.base';
import { Registry } from 'vs/platform/registry/common/platform'; import { Registry } from 'vs/platform/registry/common/platform';
import { IAction, Action } from 'vs/base/common/actions'; import { Action } from 'vs/base/common/actions';
import { IConfigurationService } from 'vs/platform/configuration/common/configuration'; import { IConfigurationService } from 'vs/platform/configuration/common/configuration';
import { registerSingleton } from 'vs/platform/instantiation/common/extensions'; import { registerSingleton } from 'vs/platform/instantiation/common/extensions';
import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions'; import { IWorkbenchActionRegistry, Extensions as ActionExtensions } from 'vs/workbench/common/actions';
import paths = require('vs/base/common/paths'); import paths = require('vs/base/common/paths');
import resources = require('vs/base/common/resources');
import { Scope, IActionBarRegistry, Extensions as ActionBarExtensions, ActionBarContributor } from 'vs/workbench/browser/actions';
import uri from 'vs/base/common/uri'; import uri from 'vs/base/common/uri';
import { explorerItemToFileResource } from 'vs/workbench/parts/files/common/files';
import { ITerminalService } from 'vs/workbench/parts/execution/common/execution'; import { ITerminalService } from 'vs/workbench/parts/execution/common/execution';
import { SyncActionDescriptor, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions'; import { SyncActionDescriptor, MenuId, MenuRegistry } from 'vs/platform/actions/common/actions';
import { IInstantiationService } from 'vs/platform/instantiation/common/instantiation';
import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService'; import { IWorkbenchEditorService } from 'vs/workbench/services/editor/common/editorService';
import { toResource, IEditorContext } from 'vs/workbench/common/editor'; import { toResource, IEditorContext } from 'vs/workbench/common/editor';
import { KeyMod, KeyCode } from 'vs/base/common/keyCodes'; import { KeyMod, KeyCode } from 'vs/base/common/keyCodes';
...@@ -30,7 +26,7 @@ import { WinTerminalService, MacTerminalService, LinuxTerminalService } from 'vs ...@@ -30,7 +26,7 @@ import { WinTerminalService, MacTerminalService, LinuxTerminalService } from 'vs
import { IHistoryService } from 'vs/workbench/services/history/common/history'; import { IHistoryService } from 'vs/workbench/services/history/common/history';
import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands'; import { CommandsRegistry, ICommandService } from 'vs/platform/commands/common/commands';
import { EditorWithResourceFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands'; import { EditorWithResourceFocusedInOpenEditorsContext } from 'vs/workbench/parts/files/electron-browser/fileCommands';
import { ContextKeyExpr } from 'vs/platform/contextkey/common/contextkey'; import { ResourceContextKey } from 'vs/workbench/common/resources';
if (env.isWindows) { if (env.isWindows) {
registerSingleton(ITerminalService, WinTerminalService); registerSingleton(ITerminalService, WinTerminalService);
...@@ -109,74 +105,6 @@ class OpenConsoleAction extends Action { ...@@ -109,74 +105,6 @@ class OpenConsoleAction extends Action {
} }
} }
class OpenIntegratedTerminalAction extends Action {
public static readonly ID = 'workbench.action.terminal.openFolderInIntegratedTerminal';
public static readonly Label = nls.localize('openFolderInIntegratedTerminal', "Open in Terminal");
private resource: uri;
public setResource(resource: uri): void {
this.resource = resource;
this.enabled = !paths.isUNC(this.resource.fsPath);
}
constructor(
id: string,
label: string,
@ICommandService private commandService: ICommandService
) {
super(id, label);
}
public run(event?: any): TPromise<any> {
return this.commandService.executeCommand(OPEN_CONSOLE_COMMAND_ID, this.resource);
}
}
export class ExplorerViewerActionContributor extends ActionBarContributor {
constructor(
@IInstantiationService private instantiationService: IInstantiationService,
@IConfigurationService private configurationService: IConfigurationService
) {
super();
}
public hasSecondaryActions(context: any): boolean {
const fileResource = explorerItemToFileResource(context.element);
return fileResource && fileResource.resource.scheme === 'file';
}
public getSecondaryActions(context: any): IAction[] {
let fileResource = explorerItemToFileResource(context.element);
let resource = fileResource.resource;
// We want the parent unless this resource is a directory
if (!fileResource.isDirectory) {
resource = resources.dirname(resource);
}
const configuration = this.configurationService.getValue<ITerminalConfiguration>();
const explorerKind = configuration.terminal.explorerKind;
if (explorerKind === 'integrated') {
let action = this.instantiationService.createInstance(OpenIntegratedTerminalAction, OpenIntegratedTerminalAction.ID, OpenIntegratedTerminalAction.Label);
action.setResource(resource);
return [action];
} else {
let action = this.instantiationService.createInstance(OpenConsoleAction, OpenConsoleAction.ID, OpenConsoleAction.ScopedLabel);
action.setResource(resource);
return [action];
}
}
}
const actionBarRegistry = Registry.as<IActionBarRegistry>(ActionBarExtensions.Actionbar);
actionBarRegistry.registerActionBarContributor(Scope.VIEWER, ExplorerViewerActionContributor);
// Register Global Action to Open Console // Register Global Action to Open Console
Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).registerWorkbenchAction( Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).registerWorkbenchAction(
new SyncActionDescriptor( new SyncActionDescriptor(
...@@ -191,29 +119,26 @@ Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).registe ...@@ -191,29 +119,26 @@ Registry.as<IWorkbenchActionRegistry>(ActionExtensions.WorkbenchActions).registe
const OPEN_CONSOLE_COMMAND_ID = 'workbench.command.terminal.openNativeConsole'; const OPEN_CONSOLE_COMMAND_ID = 'workbench.command.terminal.openNativeConsole';
function getPathToOpen(resource: uri, historyService: IHistoryService, editorService: IWorkbenchEditorService): string {
let pathToOpen: string;
// Try workspace path first
const root = historyService.getLastActiveWorkspaceRoot('file');
pathToOpen = resource ? resource.fsPath : (root && root.fsPath);
// Otherwise check if we have an active file open
if (!pathToOpen) {
const file = toResource(editorService.getActiveEditorInput(), { supportSideBySide: true, filter: 'file' });
if (file) {
pathToOpen = paths.dirname(file.fsPath); // take parent folder of file
}
}
return pathToOpen;
}
CommandsRegistry.registerCommand({ CommandsRegistry.registerCommand({
id: OPEN_CONSOLE_COMMAND_ID, id: OPEN_CONSOLE_COMMAND_ID,
handler: (accessor, args: IEditorContext) => { handler: (accessor, args: IEditorContext) => {
const configurationService = accessor.get(IConfigurationService); const configurationService = accessor.get(IConfigurationService);
const pathToOpen = getPathToOpen(args.resource, accessor.get(IHistoryService), accessor.get(IWorkbenchEditorService)); const historyService = accessor.get(IHistoryService);
const editorService = accessor.get(IWorkbenchEditorService);
let pathToOpen: string;
// Try workspace path first
const root = historyService.getLastActiveWorkspaceRoot('file');
pathToOpen = args.resource ? args.resource.fsPath : (root && root.fsPath);
// Otherwise check if we have an active file open
if (!pathToOpen) {
const file = toResource(editorService.getActiveEditorInput(), { supportSideBySide: true, filter: 'file' });
if (file) {
pathToOpen = paths.dirname(file.fsPath); // take parent folder of file
}
}
if (configurationService.getValue<ITerminalConfiguration>().terminal.explorerKind === 'integrated') { if (configurationService.getValue<ITerminalConfiguration>().terminal.explorerKind === 'integrated') {
const integratedTerminalService = accessor.get(IIntegratedTerminalService); const integratedTerminalService = accessor.get(IIntegratedTerminalService);
...@@ -231,13 +156,22 @@ CommandsRegistry.registerCommand({ ...@@ -231,13 +156,22 @@ CommandsRegistry.registerCommand({
} }
}); });
const openConsoleCommand = {
id: OPEN_CONSOLE_COMMAND_ID,
title: env.isWindows ? nls.localize('scopedConsoleActionWin', "Open in Command Prompt") :
nls.localize('scopedConsoleActionMacLinux', "Open in Terminal")
};
MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, { MenuRegistry.appendMenuItem(MenuId.OpenEditorsContext, {
group: '1_files', group: '1_files',
order: 30, order: 30,
command: { command: openConsoleCommand,
id: OPEN_CONSOLE_COMMAND_ID, when: EditorWithResourceFocusedInOpenEditorsContext
title: env.isWindows ? nls.localize('scopedConsoleActionWin', "Open in Command Prompt") : });
nls.localize('scopedConsoleActionMacLinux', "Open in Terminal")
}, MenuRegistry.appendMenuItem(MenuId.ExplorerContext, {
when: ContextKeyExpr.and(EditorWithResourceFocusedInOpenEditorsContext) group: '1_files',
order: 30,
command: openConsoleCommand,
when: ResourceContextKey.Scheme.isEqualTo('file')
}); });
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册