Skip to content

Commit

Permalink
fix #4230: complete implementation of command VS Code API
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Feb 7, 2019
1 parent 36380db commit 5550757
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
11 changes: 7 additions & 4 deletions packages/plugin-ext/src/plugin/command-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`);
}
Expand All @@ -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);
Expand All @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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?: <T>(...args: any[]) => T | Thenable<T>): Disposable {
return commandRegistry.registerCommand(command, handler);
registerCommand(command: theia.Command, handler?: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any): Disposable {
return commandRegistry.registerCommand(command, handler, thisArg);
},
// tslint:disable-next-line:no-any
executeCommand<T>(commandId: string, ...args: any[]): PromiseLike<T | undefined> {
Expand All @@ -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<theia.CommandKeyBinding[] | undefined> {
return commandRegistry.getKeyBinding(commandId);
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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,
Expand Down

0 comments on commit 5550757

Please sign in to comment.