Skip to content

Commit

Permalink
GH-6598: Exposed all handlers for a command.
Browse files Browse the repository at this point in the history
Closes #6598.

Signed-off-by: Akos Kitta <kittaakos@typefox.io>
  • Loading branch information
Akos Kitta authored and kittaakos committed Nov 21, 2019
1 parent 76f6faa commit 0860ab1
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 1 deletion.
25 changes: 25 additions & 0 deletions packages/core/src/common/command.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import * as chai from 'chai';
const expect = chai.expect;
let commandRegistry: CommandRegistry;

/* tslint:disable:no-unused-expression */

describe('Commands', () => {

beforeEach(() => {
Expand Down Expand Up @@ -120,6 +122,25 @@ describe('Commands', () => {
commandRegistry.clearCommandHistory();
expect(commandRegistry.recent.length).equal(0);
});

it('should return with an empty array of handlers if the command is not registered', () => {
expect(commandRegistry.getCommand('missing')).to.be.undefined;
expect(commandRegistry.getAllHandlers('missing')).to.be.empty;
});

it('should return with an empty array of handlers if the command has no registered handlers', () => {
commandRegistry.registerCommand({ id: 'id' });
expect(commandRegistry.getCommand('id')).to.be.not.undefined;
expect(commandRegistry.getAllHandlers('id')).to.be.empty;
});

it('should return all handlers including the non active ones', () => {
commandRegistry.registerCommand({ id: 'id' });
commandRegistry.registerHandler('id', new StubCommandHandler());
commandRegistry.registerHandler('id', new NeverActiveStubCommandHandler());
expect(commandRegistry.getAllHandlers('id').length).to.be.equal(2);
});

});

class EmptyContributionProvider implements ContributionProvider<CommandContribution> {
Expand All @@ -141,3 +162,7 @@ class ConcatCommandHandler implements CommandHandler {
class StubCommandHandler implements CommandHandler {
execute(...args: string[]): undefined { return undefined; }
}

class NeverActiveStubCommandHandler extends StubCommandHandler {
isEnabled(): boolean { return false; }
}
11 changes: 10 additions & 1 deletion packages/core/src/common/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export interface CommandService {
// tslint:disable-next-line:no-any
executeCommand<T>(command: string, ...args: any[]): Promise<T | undefined>;
/**
* An event is emmited when a command is about to be executed.
* An event is emitted when a command is about to be executed.
*
* It can be used to install or activate a command handler.
*/
Expand Down Expand Up @@ -348,6 +348,15 @@ export class CommandRegistry implements CommandService {
return undefined;
}

/**
* Returns with all handlers for the given command. If the command does not have any handlers,
* or the command is not registered, returns an empty array.
*/
getAllHandlers(commandId: string): CommandHandler[] {
const handlers = this._handlers[commandId];
return handlers ? handlers.slice() : [];
}

/**
* Get all registered commands.
*/
Expand Down

0 comments on commit 0860ab1

Please sign in to comment.