Skip to content

Commit

Permalink
#7295 Theia API is incompatible with VSCode fixed theia.commands.reg…
Browse files Browse the repository at this point in the history
…isterCommand to be compatible with VSCodeSigned-off-by: Jonas Helming <[email protected]>

Signed-off-by: Jonas Helming <[email protected]>
  • Loading branch information
JonasHelming committed Mar 8, 2020
1 parent 842e717 commit e259887
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 17 deletions.
15 changes: 0 additions & 15 deletions packages/plugin-ext-vscode/src/node/plugin-vscode-init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,21 +39,6 @@ export enum ExtensionKind {
export const doInitialization: BackendInitializationFn = (apiFactory: PluginAPIFactory, plugin: Plugin) => {
const vscode = Object.assign(apiFactory(plugin), { ExtensionKind });

// replace command API as it will send only the ID as a string parameter
const registerCommand = vscode.commands.registerCommand;
vscode.commands.registerCommand = function (command: theia.CommandDescription | string, handler?: <T>(...args: any[]) => T | Thenable<T>, thisArg?: any): any {
// use of the ID when registering commands
if (typeof command === 'string') {
const rawCommands = plugin.rawModel.contributes && plugin.rawModel.contributes.commands;
const commands = rawCommands ? Array.isArray(rawCommands) ? rawCommands : [rawCommands] : undefined;
if (handler && commands && commands.some(item => item.command === command)) {
return vscode.commands.registerHandler(command, handler, thisArg);
}
return registerCommand({ id: command }, handler, thisArg);
}
return registerCommand(command, handler, thisArg);
};

// use Theia plugin api instead vscode extensions
(<any>vscode).extensions = {
get all(): any[] {
Expand Down
11 changes: 10 additions & 1 deletion packages/plugin-ext/src/plugin/plugin-context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,16 @@ export function createAPIFactory(
return function (plugin: InternalPlugin): typeof theia {
const commands: typeof theia.commands = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
registerCommand(command: theia.CommandDescription, handler?: <T>(...args: any[]) => T | Thenable<T | undefined>, thisArg?: any): Disposable {
registerCommand(command: theia.CommandDescription | string, handler?: <T>(...args: any[]) => T | Thenable<T | undefined>, thisArg?: any): Disposable {
// use of the ID when registering commands
if (typeof command === 'string') {
const rawCommands = plugin.rawModel.contributes && plugin.rawModel.contributes.commands;
const contributedCommands = rawCommands ? Array.isArray(rawCommands) ? rawCommands : [rawCommands] : undefined;
if (handler && contributedCommands && contributedCommands.some(item => item.command === command)) {
return commandRegistry.registerHandler(command, handler, thisArg);
}
return commandRegistry.registerCommand({ id: command }, handler, thisArg);
}
return commandRegistry.registerCommand(command, handler, thisArg);
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/theia.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2239,7 +2239,7 @@ declare module '@theia/plugin' {
*
* Throw if a command is already registered for the given command identifier.
*/
export function registerCommand(command: CommandDescription, handler?: (...args: any[]) => any, thisArg?: any): Disposable;
export function registerCommand(command: CommandDescription | string, handler?: (...args: any[]) => any, thisArg?: any): Disposable;

/**
* Register the given handler for the given command identifier.
Expand Down Expand Up @@ -9170,3 +9170,4 @@ declare module '@theia/plugin' {
provideCallHierarchyOutgoingCalls(item: CallHierarchyItem, token: CancellationToken): ProviderResult<CallHierarchyOutgoingCall[]>;
}
}

0 comments on commit e259887

Please sign in to comment.