提交 4ad4d908 编写于 作者: D Dirk Baeumer

Make more actions commands to ease keyboard shortcut assignment

上级 8bcd40f1
......@@ -61,9 +61,9 @@ export interface PlatformTaskDescription {
export interface CommandBinding {
/**
* The command Id the task is bound to.
* The command identifer the task is bound to.
*/
commandId?: string;
identifier?: string;
/**
* The title to use
......@@ -652,7 +652,7 @@ namespace ProblemMatcherConverter {
namespace CommandBinding {
export function isEmpty(value: TaskSystem.CommandBinding): boolean {
return !value || value.commandId === void 0 && value.title === void 0 && value.category === void 0;
return !value || value.identifier === void 0 && value.title === void 0 && value.category === void 0;
}
export function from(this: void, binding: CommandBinding, context: ParseContext): TaskSystem.CommandBinding {
......@@ -660,19 +660,14 @@ namespace CommandBinding {
return undefined;
}
if (!Types.isString(binding.commandId)) {
if (!Types.isString(binding.identifier)) {
context.validationStatus.state = ValidationState.Warning;
context.logger.log(nls.localize('noCommandId', 'Warning: a command binding must defined a commandId. Ignoring binding.'));
return undefined;
}
if (!Types.isString(binding.title)) {
context.validationStatus.state = ValidationState.Warning;
context.logger.log(nls.localize('noTitle', 'Warning: a command binding must defined a title. Ignoring binding.'));
context.logger.log(nls.localize('noCommandId', 'Warning: a command binding must defined an identifier. Ignoring binding.'));
return undefined;
}
let result: TaskSystem.CommandBinding = {
commandId: binding.commandId,
title: binding.title
identifier: binding.identifier,
title: ''
};
if (Types.isString(binding.category)) {
result.category = binding.category;
......
......@@ -138,9 +138,9 @@ export interface CommandConfiguration {
export interface CommandBinding {
/**
* The command Id the task is bound to.
* The command identifier the task is bound to.
*/
commandId: string;
identifier: string;
/**
* The title to use
......
......@@ -38,6 +38,9 @@ import { IConfigurationService } from 'vs/platform/configuration/common/configur
import { IFileService, FileChangeType } from 'vs/platform/files/common/files';
import { IExtensionService } from 'vs/platform/extensions/common/extensions';
import { CommandsRegistry } from 'vs/platform/commands/common/commands';
import { KeybindingsRegistry } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ServicesAccessor } from 'vs/platform/instantiation/common/instantiation';
import { IModeService } from 'vs/editor/common/services/modeService';
import { IModelService } from 'vs/editor/common/services/modelService';
......@@ -76,116 +79,6 @@ import { IEnvironmentService } from 'vs/platform/environment/common/environment'
let $ = Builder.$;
let tasksCategory = nls.localize('tasksCategory', "Tasks");
class AbstractTaskAction extends Action {
protected taskService: ITaskService;
protected telemetryService: ITelemetryService;
protected messageService: IMessageService;
protected contextService: IWorkspaceContextService;
constructor(id: string, label: string, @ITaskService taskService: ITaskService,
@ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService,
@IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label);
this.taskService = taskService;
this.telemetryService = telemetryService;
this.messageService = messageService;
this.contextService = contextService;
}
protected canRun(): boolean {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('AbstractTaskAction.noWorkspace', 'Tasks are only available on a workspace folder.'));
return false;
}
return true;
}
}
class BuildAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.build';
public static TEXT = nls.localize('BuildAction.label', "Run Build Task");
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<ITaskSummary> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.taskService.build();
}
}
class TestAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.test';
public static TEXT = nls.localize('TestAction.label', "Run Test Task");
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<ITaskSummary> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.taskService.runTest();
}
}
class RebuildAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.rebuild';
public static TEXT = nls.localize('RebuildAction.label', 'Run Rebuild Task');
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<ITaskSummary> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.taskService.rebuild();
}
}
class CleanAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.clean';
public static TEXT = nls.localize('CleanAction.label', 'Run Clean Task');
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<ITaskSummary> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.taskService.clean();
}
}
class CommandAction extends AbstractTaskAction {
constructor(id: string, label: string, private taskId: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<ITaskSummary> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.taskService.run(this.taskId);
}
}
abstract class OpenTaskConfigurationAction extends Action {
private configurationService: IConfigurationService;
......@@ -354,91 +247,6 @@ class ViewTerminalAction extends Action {
}
}
class TerminateAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.terminate';
public static TEXT = nls.localize('TerminateAction.label', "Terminate Running Task");
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService,
@ITerminalService private terminalService: ITerminalService
) {
super(id, label, taskService, telemetryService, messageService, contextService);
}
public run(): TPromise<TerminateResponse> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
if (this.taskService.inTerminal()) {
this.messageService.show(Severity.Info, {
message: nls.localize('TerminateAction.terminalSystem', 'The tasks are executed in the integrated terminal. Use the terminal to manage the tasks.'),
actions: [new ViewTerminalAction(this.terminalService), new CloseMessageAction()]
});
return undefined;
} else {
return this.taskService.isActive().then((active) => {
if (active) {
return this.taskService.terminate().then((response) => {
if (response.success) {
return undefined;
} else if (response.code && response.code === TerminateResponseCode.ProcessNotFound) {
this.messageService.show(Severity.Error, nls.localize('TerminateAction.noProcess', 'The launched process doesn\'t exist anymore. If the task spawned background tasks exiting VS Code might result in orphaned processes.'));
return undefined;
} else {
return Promise.wrapError(nls.localize('TerminateAction.failed', 'Failed to terminate running task'));
}
});
}
return undefined;
});
}
}
}
class ShowLogAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.showLog';
public static TEXT = nls.localize('ShowLogAction.label', "Show Task Log");
private outputService: IOutputService;
constructor(id: string, label: string, @ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService,
@IOutputService outputService: IOutputService) {
super(id, label, taskService, telemetryService, messageService, contextService);
this.outputService = outputService;
}
public run(): TPromise<IEditor> {
if (!this.canRun()) {
return TPromise.as(undefined);
}
return this.outputService.getChannel(TaskService.OutputChannelId).show(true);
}
}
class RunTaskAction extends AbstractTaskAction {
public static ID = 'workbench.action.tasks.runTask';
public static TEXT = nls.localize('RunTaskAction.label', "Run Task");
private quickOpenService: IQuickOpenService;
constructor(id: string, label: string, @IQuickOpenService quickOpenService: IQuickOpenService,
@ITaskService taskService: ITaskService, @ITelemetryService telemetryService: ITelemetryService,
@IMessageService messageService: IMessageService, @IWorkspaceContextService contextService: IWorkspaceContextService) {
super(id, label, taskService, telemetryService, messageService, contextService);
this.quickOpenService = quickOpenService;
}
public run(event?: any): Promise {
if (!this.canRun()) {
return TPromise.as(undefined);
}
this.quickOpenService.show('task ');
return TPromise.as(null);
}
}
class StatusBarItem implements IStatusbarItem {
private panelService: IPanelService;
......@@ -737,18 +545,45 @@ class TaskService extends EventEmitter implements ITaskService {
});
lifecycleService.onWillShutdown(event => event.veto(this.beforeShutdown()));
this.registerCommands();
}
private registerCommands(): void {
CommandsRegistry.registerCommand('workbench.action.tasks.runTask', (accessor, arg) => {
if (Types.isString(arg)) {
this.tasks().then(tasks => {
for (let task of tasks) {
if (task.name === arg) {
this.run(task.id);
}
}
});
} else {
this.quickOpenService.show('task ');
this.runTaskCommand(accessor, arg);
});
CommandsRegistry.registerCommand('workbench.action.tasks.terminate', (accessor, arg) => {
this.runTerminateCommand();
});
CommandsRegistry.registerCommand('workbench.action.tasks.showLog', () => {
if (!this.canRunCommand()) {
return;
}
this.showOutput();
});
CommandsRegistry.registerCommand('workbench.action.tasks.build', () => {
if (!this.canRunCommand()) {
return;
}
this.build();
});
KeybindingsRegistry.registerKeybindingRule({
id: 'workbench.action.tasks.build',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(),
when: undefined,
primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_B
});
CommandsRegistry.registerCommand('workbench.action.tasks.test', () => {
if (!this.canRunCommand()) {
return;
}
this.runTest();
});
}
......@@ -1124,6 +959,7 @@ class TaskService extends EventEmitter implements ITaskService {
return this.configureAction();
}
}
private handleError(err: any): void {
let showOutput = true;
if (err instanceof TaskError) {
......@@ -1132,10 +968,12 @@ class TaskService extends EventEmitter implements ITaskService {
let needsTerminate = buildError.code === TaskErrors.RunningTask;
if (needsConfig || needsTerminate) {
let closeAction = new CloseMessageAction();
let action = needsConfig
let action: Action = needsConfig
? this.getConfigureAction(buildError.code)
: new TerminateAction(TerminateAction.ID, TerminateAction.TEXT, this, this.telemetryService, this.messageService, this.contextService, this.terminalService);
: new Action(
'workbench.action.tasks.terminate',
nls.localize('TerminateAction.label', "Terminate Running Task"),
undefined, true, () => { this.runTerminateCommand(); return TPromise.as<void>(undefined); });
closeAction.closeFunction = this.messageService.show(buildError.severity, { message: buildError.message, actions: [action, closeAction] });
} else {
this.messageService.show(buildError.severity, buildError.message);
......@@ -1152,20 +990,71 @@ class TaskService extends EventEmitter implements ITaskService {
this.outputChannel.show(true);
}
}
private canRunCommand(): boolean {
if (!this.contextService.hasWorkspace()) {
this.messageService.show(Severity.Info, nls.localize('TaskService.noWorkspace', 'Tasks are only available on a workspace folder.'));
return false;
}
return true;
}
private runTaskCommand(accessor: ServicesAccessor, arg: any): void {
if (!this.canRunCommand()) {
return;
}
if (Types.isString(arg)) {
this.tasks().then(tasks => {
for (let task of tasks) {
if (task.name === arg || (task.bindTo && task.bindTo.identifier === arg)) {
this.run(task.id);
}
}
});
} else {
this.quickOpenService.show('task ');
}
}
private runTerminateCommand(): void {
if (!this.canRunCommand()) {
return;
}
if (this.inTerminal()) {
this.messageService.show(Severity.Info, {
message: nls.localize('TerminateAction.terminalSystem', 'The tasks are executed in the integrated terminal. Use the terminal to manage the tasks.'),
actions: [new ViewTerminalAction(this.terminalService), new CloseMessageAction()]
});
} else {
this.isActive().then((active) => {
if (active) {
this.terminate().then((response) => {
if (response.success) {
return undefined;
} else if (response.code && response.code === TerminateResponseCode.ProcessNotFound) {
this.messageService.show(Severity.Error, nls.localize('TerminateAction.noProcess', 'The launched process doesn\'t exist anymore. If the task spawned background tasks exiting VS Code might result in orphaned processes.'));
return undefined;
} else {
return Promise.wrapError(nls.localize('TerminateAction.failed', 'Failed to terminate running task'));
}
});
}
});
}
}
}
let workbenchActionsRegistry = <IWorkbenchActionRegistry>Registry.as(WorkbenchActionExtensions.WorkbenchActions);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ConfigureTaskRunnerAction, ConfigureTaskRunnerAction.ID, ConfigureTaskRunnerAction.TEXT), 'Tasks: Configure Task Runner', tasksCategory);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(BuildAction, BuildAction.ID, BuildAction.TEXT, { primary: KeyMod.CtrlCmd | KeyMod.Shift | KeyCode.KEY_B }), 'Tasks: Run Build Task', tasksCategory);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(TestAction, TestAction.ID, TestAction.TEXT), 'Tasks: Run Test Task', tasksCategory);
// workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(RebuildAction, RebuildAction.ID, RebuildAction.TEXT), tasksCategory);
// workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(CleanAction, CleanAction.ID, CleanAction.TEXT), tasksCategory);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(TerminateAction, TerminateAction.ID, TerminateAction.TEXT), 'Tasks: Terminate Running Task', tasksCategory);
workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(ShowLogAction, ShowLogAction.ID, ShowLogAction.TEXT), 'Tasks: Show Task Log', tasksCategory);
// workbenchActionsRegistry.registerWorkbenchAction(new SyncActionDescriptor(RunTaskAction, RunTaskAction.ID, RunTaskAction.TEXT), 'Tasks: Run Task', tasksCategory);
MenuRegistry.addCommand({ id: 'workbench.action.tasks.showLog', title: nls.localize('ShowLogAction.label', "Show Task Log"), category: tasksCategory });
MenuRegistry.addCommand({ id: 'workbench.action.tasks.runTask', title: nls.localize('RunTaskAction.label', "Run Task"), category: tasksCategory });
MenuRegistry.addCommand({ id: 'workbench.action.tasks.terminate', title: nls.localize('TerminateAction.label', "Terminate Running Task"), category: tasksCategory });
MenuRegistry.addCommand({ id: 'workbench.action.tasks.build', title: nls.localize('BuildAction.label', "Run Build Task"), category: tasksCategory });
MenuRegistry.addCommand({ id: 'workbench.action.tasks.test', title: nls.localize('TestAction.label', "Run Test Task"), category: tasksCategory });
// MenuRegistry.addCommand( { id: 'workbench.action.tasks.rebuild', title: nls.localize('RebuildAction.label', 'Run Rebuild Task'), category: tasksCategory });
// MenuRegistry.addCommand( { id: 'workbench.action.tasks.clean', title: nls.localize('CleanAction.label', 'Run Clean Task'), category: tasksCategory });
// Task Service
registerSingleton(ITaskService, TaskService);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册