提交 e05566c9 编写于 作者: A Alex Dima

Command handlers are invoked with var-args

上级 fb467cc7
......@@ -556,6 +556,7 @@ export abstract class CommonCodeEditor extends EventEmitter implements IActionPr
}
public trigger(source:string, handlerId:string, payload:any): void {
payload = payload || {};
var candidate = this.getAction(handlerId);
if(candidate !== null) {
if (candidate.enabled) {
......
......@@ -83,7 +83,7 @@ function registerCoreDispatchCommand2(handlerId: string) {
id: 'default:' + handlerId,
handler: (accessor: ServicesAccessor, args: any) => {
withCodeEditorFromCommandHandler(handlerId, accessor, (editor) => {
editor.trigger('keyboard', handlerId, args[0]);
editor.trigger('keyboard', handlerId, args);
});
},
weight: KeybindingsRegistry.WEIGHT.editorCore(),
......
......@@ -93,11 +93,8 @@ export module CommonEditorRegistry {
export function registerLanguageCommand(id: string, handler: (accessor: ServicesAccessor, args: { [n: string]: any }) => any) {
KeybindingsRegistry.registerCommandDesc({
id,
handler(accessor, args: any[]) {
if (args && args.length > 1 || typeof args[0] !== 'object') {
throw illegalArgument();
}
return handler(accessor, args && args[0]);
handler(accessor, args: any) {
return handler(accessor, args || {});
},
weight: KeybindingsRegistry.WEIGHT.editorContrib(),
primary: undefined,
......@@ -255,7 +252,7 @@ function whenRule(needsTextFocus: boolean, needsKey: string): KbExpr {
function createCommandHandler(commandId: string, handler: IEditorCommandHandler): ICommandHandler {
return (accessor, args) => {
withCodeEditorFromCommandHandler(commandId, accessor, (editor) => {
handler(accessor, editor, args);
handler(accessor, editor, args||{});
});
};
}
......@@ -232,7 +232,7 @@ CommonEditorRegistry.registerEditorCommand('closeAccessibilityHelp', CommonEdito
});
KeybindingsRegistry.registerCommandDesc({
id: TOGGLE_EXPERIMENTAL_SCREEN_READER_SUPPORT_COMMAND_ID,
handler: (accessor: ServicesAccessor, args: any) => {
handler: (accessor: ServicesAccessor) => {
let currentValue = GlobalScreenReaderNVDA.getValue();
GlobalScreenReaderNVDA.setValue(!currentValue);
},
......
......@@ -79,7 +79,7 @@ class CodeLensContentWidget implements editorBrowser.IContentWidget {
let command = this._commands[element.id];
if (command) {
editor.focus();
keybindingService.executeCommand(command.id, command.arguments).done(undefined, err => {
keybindingService.executeCommand(command.id, ...command.arguments).done(undefined, err => {
messageService.show(Severity.Error, err);
});
}
......
......@@ -32,6 +32,7 @@ import {IPeekViewService, getOuterEditor} from 'vs/editor/contrib/zoneWidget/bro
import {findReferences} from '../common/referenceSearch';
import {EventType, ReferencesModel} from './referenceSearchModel';
import {ReferenceWidget} from './referenceSearchWidget';
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
export class FindReferencesController implements editorCommon.IEditorContribution {
......@@ -317,9 +318,8 @@ function metaTitle(references: IReference[]): string {
}
}
let findReferencesCommand: ICommandHandler = (accessor, args:[URI, editorCommon.IPosition]) => {
let findReferencesCommand: ICommandHandler = (accessor:ServicesAccessor, resource:URI, position:editorCommon.IPosition) => {
let [resource, position] = args;
if (!(resource instanceof URI)) {
throw new Error('illegal argument, uri');
}
......@@ -341,12 +341,12 @@ let findReferencesCommand: ICommandHandler = (accessor, args:[URI, editorCommon.
});
};
let showReferencesCommand: ICommandHandler = (accessor, args:[URI, editorCommon.IPosition, IReference[]]) => {
if (!(args[0] instanceof URI)) {
let showReferencesCommand: ICommandHandler = (accessor:ServicesAccessor, resource:URI, position:editorCommon.IPosition, references:IReference[]) => {
if (!(resource instanceof URI)) {
throw new Error('illegal argument, uri expected');
}
return accessor.get(IEditorService).openEditor({ resource: args[0] }).then(editor => {
return accessor.get(IEditorService).openEditor({ resource: resource }).then(editor => {
let control = <editorCommon.ICommonCodeEditor>editor.getControl();
if (!control || typeof control.getEditorType !== 'function') {
......@@ -354,8 +354,8 @@ let showReferencesCommand: ICommandHandler = (accessor, args:[URI, editorCommon.
}
let controller = FindReferencesController.getController(control);
let range = Position.asEmptyRange(args[1]);
return TPromise.as(controller.processRequest(Range.lift(range), TPromise.as(args[2]), metaTitle)).then(() => true);
let range = Position.asEmptyRange(position);
return TPromise.as(controller.processRequest(Range.lift(range), TPromise.as(references), metaTitle)).then(() => true);
});
};
......
......@@ -227,7 +227,7 @@ CommonEditorRegistry.registerEditorCommand(ACCEPT_SELECTED_SUGGESTION_CMD, weigh
});
KeybindingsRegistry.registerCommandDesc({
id: 'acceptSelectedSuggestionOnEnter',
handler(accessor, args) {
handler(accessor) {
withCodeEditorFromCommandHandler('acceptSelectedSuggestionOnEnter', accessor, (editor) => {
const controller = SuggestController.getSuggestController(editor);
controller.acceptSelectedSuggestion();
......
......@@ -21,6 +21,7 @@ import {KeybindingsRegistry} from 'vs/platform/keybinding/common/keybindingsRegi
import {IStatusbarService} from 'vs/platform/statusbar/common/statusbar';
import {IMessageService} from 'vs/platform/message/common/message';
import {IConfigurationService} from 'vs/platform/configuration/common/configuration';
import {ServicesAccessor} from 'vs/platform/instantiation/common/instantiation';
let KEYBINDING_CONTEXT_ATTR = 'data-keybinding-context';
......@@ -332,20 +333,20 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
e.preventDefault();
}
let commandId = resolveResult.commandId.replace(/^\^/, '');
this._invokeHandler(commandId, { context: contextValue }).done(undefined, err => {
this._invokeHandler(commandId, [{}]).done(undefined, err => {
this._messageService.show(Severity.Warning, err);
});
}
}
protected _invokeHandler(commandId: string, args: any): TPromise<any> {
protected _invokeHandler(commandId: string, args: any[]): TPromise<any> {
let handler = this._getCommandHandler(commandId);
if (!handler) {
return TPromise.wrapError(new Error(`No handler found for the command: '${commandId}'. An extension might be missing an activation event.`));
}
try {
let result = this._instantiationService.invokeFunction(handler, args);
let result = this._instantiationService.invokeFunction.apply(this._instantiationService, [handler].concat(args));
return TPromise.as(result);
} catch (err) {
return TPromise.wrapError(err);
......@@ -376,23 +377,21 @@ export abstract class KeybindingService extends AbstractKeybindingService implem
delete this._contexts[String(contextId)];
}
public executeCommand(commandId: string, args: any = {}): TPromise<any> {
// TODO@{Alex,Joh} we should spec what args should be.
// TODO@Alex - move this to its own command
if (commandId === SET_CONTEXT_COMMAND_ID) {
var contextKey = String(args[0]);
var contextValue = args[1];
this.setContext(contextKey, contextValue);
return TPromise.as(null);
}
public executeCommand(commandId: string, ...args: any[]): TPromise<any> {
return this._invokeHandler(commandId, args);
}
}
KeybindingsRegistry.registerCommandDesc({
id: SET_CONTEXT_COMMAND_ID,
handler: (accessor:ServicesAccessor, contextKey:any, contextValue:any) => {
accessor.get(IKeybindingService).createKey(String(contextKey), contextValue);
},
weight: 0,
primary: undefined,
when: null
});
class ScopedKeybindingService extends AbstractKeybindingService {
private _parent: AbstractKeybindingService;
......
......@@ -292,7 +292,7 @@ export interface IKeybindingItem {
}
export interface ICommandHandler {
(accessor: ServicesAccessor, args: any): void;
(accessor: ServicesAccessor, ...args: any[]): void;
description?: string | ICommandHandlerDescription;
}
......@@ -335,8 +335,8 @@ export interface IKeybindingService {
getHTMLLabelFor(keybinding: Keybinding): IHTMLContentElement[];
getElectronAcceleratorFor(keybinding: Keybinding): string;
executeCommand<T>(commandId: string, args?: any): TPromise<T>;
executeCommand(commandId: string, args?: any): TPromise<any>;
executeCommand<T>(commandId: string, ...args: any[]): TPromise<T>;
executeCommand(commandId: string, ...args: any[]): TPromise<any>;
hasCommand(commandId: string): boolean;
}
......
......@@ -33,7 +33,7 @@ export class MockKeybindingService implements IKeybindingService {
public serviceId = IKeybindingService;
public dispose(): void { }
public executeCommand(commandId: string, args: any): TPromise<any> { return; }
public executeCommand(commandId: string, ...args: any[]): TPromise<any> { return; }
public hasCommand(commandId) { return false; }
public createKey<T>(key: string, defaultValue: T): IKeybindingContextKey<T> {
......
......@@ -36,10 +36,13 @@ export class OpenerService implements IOpenerService {
let args: any;
try {
args = parse(query);
if (!Array.isArray(args)) {
args = [args];
}
} catch (e) {
//
}
promise = this._keybindingService.executeCommand(path, args);
promise = this._keybindingService.executeCommand(path, ...args);
} else {
promise = this._editorService.resolveEditorModel({ resource }).then(model => {
......
......@@ -157,7 +157,7 @@ export class MainThreadCommands {
}
$executeCommand<T>(id: string, args: any[]): Thenable<T> {
return this._keybindingService.executeCommand(id, args);
return this._keybindingService.executeCommand(id, ...args);
}
$getCommands(): Thenable<string[]> {
......
......@@ -169,7 +169,7 @@ export function triggerAndDisposeAction(instantitationService: IInstantiationSer
}
if (telemetryService) {
telemetryService.publicLog('workbenchActionExecuted', { id: actionInstance.id, from: args.from || 'keybinding' });
telemetryService.publicLog('workbenchActionExecuted', { id: actionInstance.id, from: args && args.from || 'keybinding' });
}
// run action when workbench is created
......
......@@ -469,10 +469,7 @@ export class CloseMessagesAction extends Action {
KeybindingsRegistry.registerCommandDesc({
id: '_workbench.ipc',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
handler(accessor: ServicesAccessor, args: [string, any[]]) {
const ipcMessage = args[0];
const ipcArgs = args[1];
handler(accessor: ServicesAccessor, ipcMessage: string, ipcArgs:any[]) {
if (ipcMessage && Array.isArray(ipcArgs)) {
ipc.send(ipcMessage, ...ipcArgs);
} else {
......
......@@ -28,9 +28,7 @@ import {SyncDescriptor} from 'vs/platform/instantiation/common/descriptors';
KeybindingsRegistry.registerCommandDesc({
id: '_workbench.previewHtml',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
handler(accessor: ServicesAccessor, args: [URI|string, EditorPosition]) {
let [resource, position] = args;
handler(accessor: ServicesAccessor, resource: URI|string, position:EditorPosition) {
let uri = resource instanceof URI ? resource : URI.parse(resource);
let input = accessor.get(IInstantiationService).createInstance(HtmlInput, uri.fsPath, undefined, uri);
......
......@@ -150,7 +150,7 @@ class Snapper {
KeybindingsRegistry.registerCommandDesc({
id: '_workbench.captureSyntaxTokens',
weight: KeybindingsRegistry.WEIGHT.workbenchContrib(0),
handler(accessor: ServicesAccessor, args: [URI]) {
handler(accessor: ServicesAccessor, resource:URI) {
let process = (resource: URI) => {
let filePath = resource.fsPath;
......@@ -160,10 +160,8 @@ KeybindingsRegistry.registerCommandDesc({
return pfs.readFile(filePath).then(content => {
return snapper.captureSyntaxTokens(fileName, content.toString());
});
}
};
let [resource] = args;
if (!resource) {
let editorService = accessor.get(IWorkbenchEditorService);
let fileEditorInput = asFileEditorInput(editorService.getActiveEditorInput());
......
......@@ -250,7 +250,7 @@ export class WorkbenchKeybindingService extends KeybindingService {
return commandAdded;
}
protected _invokeHandler(commandId: string, args: any): TPromise<any> {
protected _invokeHandler(commandId: string, args: any[]): TPromise<any> {
if (this._extensionService) {
return this._extensionService.activateByEvent('onCommand:' + commandId).then(_ => {
return super._invokeHandler(commandId, args);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册