From 55507576d726c7350644f051eec548d5658963ec Mon Sep 17 00:00:00 2001 From: Anton Kosyakov Date: Wed, 6 Feb 2019 07:15:02 +0000 Subject: [PATCH] fix #4230: complete implementation of command VS Code API Signed-off-by: Anton Kosyakov --- packages/plugin-ext/src/plugin/command-registry.ts | 11 +++++++---- packages/plugin-ext/src/plugin/plugin-context.ts | 8 ++++---- packages/plugin/src/theia.d.ts | 4 ++-- 3 files changed, 13 insertions(+), 10 deletions(-) diff --git a/packages/plugin-ext/src/plugin/command-registry.ts b/packages/plugin-ext/src/plugin/command-registry.ts index 720d31ef5e43b..373e3cecbb38a 100644 --- a/packages/plugin-ext/src/plugin/command-registry.ts +++ b/packages/plugin-ext/src/plugin/command-registry.ts @@ -57,7 +57,8 @@ export class CommandRegistryImpl implements CommandRegistryExt { } } - registerCommand(command: theia.Command, handler?: Handler): Disposable { + // tslint:disable-next-line:no-any + registerCommand(command: theia.Command, handler?: Handler, thisArg?: any): Disposable { if (this.commands.has(command.id)) { throw new Error(`Command ${command.id} already exist`); } @@ -66,7 +67,7 @@ export class CommandRegistryImpl implements CommandRegistryExt { const toDispose: Disposable[] = []; if (handler) { - toDispose.push(this.registerHandler(command.id, handler)); + toDispose.push(this.registerHandler(command.id, handler, thisArg)); } toDispose.push(Disposable.create(() => { this.commands.delete(command.id); @@ -75,12 +76,14 @@ export class CommandRegistryImpl implements CommandRegistryExt { return Disposable.from(...toDispose); } - registerHandler(commandId: string, handler: Handler): Disposable { + // tslint:disable-next-line:no-any + registerHandler(commandId: string, handler: Handler, thisArg?: any): Disposable { if (this.handlers.has(commandId)) { throw new Error(`Command "${commandId}" already has handler`); } this.proxy.$registerHandler(commandId); - this.handlers.set(commandId, handler); + // tslint:disable-next-line:no-any + this.handlers.set(commandId, (...args: any[]) => handler.apply(thisArg, args)); return Disposable.create(() => { this.handlers.delete(commandId); this.proxy.$unregisterHandler(commandId); diff --git a/packages/plugin-ext/src/plugin/plugin-context.ts b/packages/plugin-ext/src/plugin/plugin-context.ts index cabbd0405603c..d049691820708 100644 --- a/packages/plugin-ext/src/plugin/plugin-context.ts +++ b/packages/plugin-ext/src/plugin/plugin-context.ts @@ -154,8 +154,8 @@ export function createAPIFactory( return function (plugin: InternalPlugin): typeof theia { const commands: typeof theia.commands = { // tslint:disable-next-line:no-any - registerCommand(command: theia.Command, handler?: (...args: any[]) => T | Thenable): Disposable { - return commandRegistry.registerCommand(command, handler); + registerCommand(command: theia.Command, handler?: (...args: any[]) => T | Thenable, thisArg?: any): Disposable { + return commandRegistry.registerCommand(command, handler, thisArg); }, // tslint:disable-next-line:no-any executeCommand(commandId: string, ...args: any[]): PromiseLike { @@ -182,8 +182,8 @@ export function createAPIFactory( }); }, // tslint:disable-next-line:no-any - registerHandler(commandId: string, handler: (...args: any[]) => any): Disposable { - return commandRegistry.registerHandler(commandId, handler); + registerHandler(commandId: string, handler: (...args: any[]) => any, thisArg?: any): Disposable { + return commandRegistry.registerHandler(commandId, handler, thisArg); }, getKeyBinding(commandId: string): PromiseLike { return commandRegistry.getKeyBinding(commandId); diff --git a/packages/plugin/src/theia.d.ts b/packages/plugin/src/theia.d.ts index da32829da62af..c053cc07ea6c8 100644 --- a/packages/plugin/src/theia.d.ts +++ b/packages/plugin/src/theia.d.ts @@ -1996,7 +1996,7 @@ declare module '@theia/plugin' { * * Throw if a command is already registered for the given command identifier. */ - export function registerCommand(command: Command, handler?: (...args: any[]) => any): Disposable; + export function registerCommand(command: Command, handler?: (...args: any[]) => any, thisArg?: any): Disposable; /** * Register the given handler for the given command identifier. @@ -2006,7 +2006,7 @@ declare module '@theia/plugin' { * * Throw if a handler for the given command identifier is already registered. */ - export function registerHandler(commandId: string, handler: (...args: any[]) => any): Disposable; + export function registerHandler(commandId: string, handler: (...args: any[]) => any, thisArg?: any): Disposable; /** * Registers a text editor command that can be invoked via a keyboard shortcut,