@@ -243,14 +243,17 @@ export function createAPIFactory(
243
243
return authenticationExt . onDidChangeSessions ;
244
244
}
245
245
} ;
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
+ }
246
251
const commands : typeof theia . commands = {
247
252
// eslint-disable-next-line @typescript-eslint/no-explicit-any
248
253
registerCommand ( command : theia . CommandDescription | string , handler ?: < T > ( ...args : any [ ] ) => T | Thenable < T | undefined > , thisArg ?: any ) : Disposable {
249
254
// use of the ID when registering commands
250
255
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 ) ) {
254
257
return commandRegistry . registerHandler ( command , handler , thisArg ) ;
255
258
}
256
259
return commandRegistry . registerCommand ( { id : command } , handler , thisArg ) ;
@@ -262,7 +265,7 @@ export function createAPIFactory(
262
265
return commandRegistry . executeCommand < T > ( commandId , ...args ) ;
263
266
} ,
264
267
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 => {
266
269
const activeTextEditor = editors . getActiveEditor ( ) ;
267
270
if ( ! activeTextEditor ) {
268
271
console . warn ( 'Cannot execute ' + command + ' because there is no active text editor.' ) ;
@@ -279,7 +282,10 @@ export function createAPIFactory(
279
282
} , err => {
280
283
console . warn ( 'An error occurred while running command ' + command , err ) ;
281
284
} ) ;
282
- } ) ;
285
+ } ;
286
+ return commandIsDeclaredInPackage ( command , plugin . rawModel )
287
+ ? commandRegistry . registerHandler ( command , internalHandler )
288
+ : commandRegistry . registerCommand ( { id : command } , internalHandler ) ;
283
289
} ,
284
290
// eslint-disable-next-line @typescript-eslint/no-explicit-any
285
291
registerHandler ( commandId : string , handler : ( ...args : any [ ] ) => any , thisArg ?: any ) : Disposable {
0 commit comments