Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GH-6598: Exposed all handlers for a command. #6599

Merged
merged 1 commit into from
Nov 21, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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