Skip to content

Commit 7f51450

Browse files
author
colin-grant-work
authored
Check whether editor commands are declared in package.json (#11764)
1 parent 86fe428 commit 7f51450

File tree

1 file changed

+11
-5
lines changed

1 file changed

+11
-5
lines changed

packages/plugin-ext/src/plugin/plugin-context.ts

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,17 @@ export function createAPIFactory(
243243
return authenticationExt.onDidChangeSessions;
244244
}
245245
};
246+
function commandIsDeclaredInPackage(id: string, model: PluginPackage): boolean {
247+
const rawCommands = model.contributes?.commands;
248+
if (!rawCommands) { return false; }
249+
return Array.isArray(rawCommands) ? rawCommands.some(candidate => candidate.command === id) : rawCommands.command === id;
250+
}
246251
const commands: typeof theia.commands = {
247252
// eslint-disable-next-line @typescript-eslint/no-explicit-any
248253
registerCommand(command: theia.CommandDescription | string, handler?: <T>(...args: any[]) => T | Thenable<T | undefined>, thisArg?: any): Disposable {
249254
// use of the ID when registering commands
250255
if (typeof command === 'string') {
251-
const rawCommands = plugin.rawModel.contributes && plugin.rawModel.contributes.commands;
252-
const contributedCommands = rawCommands ? Array.isArray(rawCommands) ? rawCommands : [rawCommands] : undefined;
253-
if (handler && contributedCommands && contributedCommands.some(item => item.command === command)) {
256+
if (handler && commandIsDeclaredInPackage(command, plugin.rawModel)) {
254257
return commandRegistry.registerHandler(command, handler, thisArg);
255258
}
256259
return commandRegistry.registerCommand({ id: command }, handler, thisArg);
@@ -262,7 +265,7 @@ export function createAPIFactory(
262265
return commandRegistry.executeCommand<T>(commandId, ...args);
263266
},
264267
registerTextEditorCommand(command: string, handler: (textEditor: theia.TextEditor, edit: theia.TextEditorEdit, ...arg: any[]) => void, thisArg?: any): Disposable {
265-
return commandRegistry.registerCommand({ id: command }, (...args: any[]): any => {
268+
const internalHandler = (...args: any[]): any => {
266269
const activeTextEditor = editors.getActiveEditor();
267270
if (!activeTextEditor) {
268271
console.warn('Cannot execute ' + command + ' because there is no active text editor.');
@@ -279,7 +282,10 @@ export function createAPIFactory(
279282
}, err => {
280283
console.warn('An error occurred while running command ' + command, err);
281284
});
282-
});
285+
};
286+
return commandIsDeclaredInPackage(command, plugin.rawModel)
287+
? commandRegistry.registerHandler(command, internalHandler)
288+
: commandRegistry.registerCommand({ id: command }, internalHandler);
283289
},
284290
// eslint-disable-next-line @typescript-eslint/no-explicit-any
285291
registerHandler(commandId: string, handler: (...args: any[]) => any, thisArg?: any): Disposable {

0 commit comments

Comments
 (0)