Skip to content

Commit

Permalink
[plugin] fix #4077: support menus.commandPalette contribution point
Browse files Browse the repository at this point in the history
Signed-off-by: Anton Kosyakov <anton.kosyakov@typefox.io>
  • Loading branch information
akosyakov committed Jan 17, 2019
1 parent 743eb49 commit f16185d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 2 deletions.
16 changes: 15 additions & 1 deletion packages/core/src/browser/quick-open/quick-command-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import { Keybinding, KeybindingRegistry } from '../keybinding';
import { QuickOpenModel, QuickOpenItem, QuickOpenMode } from './quick-open-model';
import { QuickOpenOptions } from './quick-open-service';
import { QuickOpenContribution, QuickOpenHandlerRegistry, QuickOpenHandler } from './prefix-quick-open-service';
import { ContextKeyService } from '../context-key-service';

@injectable()
export class QuickCommandService implements QuickOpenModel, QuickOpenHandler {
Expand All @@ -36,14 +37,27 @@ export class QuickCommandService implements QuickOpenModel, QuickOpenHandler {
@inject(KeybindingRegistry)
protected readonly keybindings: KeybindingRegistry;

@inject(ContextKeyService)
protected readonly contextKeyService: ContextKeyService;

protected readonly contexts = new Map<string, string[]>();
pushCommandContext(commandId: string, when: string) {
const contexts = this.contexts.get(commandId) || [];
contexts.push(when);
this.contexts.set(commandId, contexts);
}

/** Initialize this quick open model with the commands. */
init(): void {
// let's compute the items here to do it in the context of the currently activeElement
this.items = [];
const filteredAndSortedCommands = this.commands.commands.filter(a => a.label).sort((a, b) => Command.compareCommands(a, b));
for (const command of filteredAndSortedCommands) {
if (command.label) {
this.items.push(new CommandQuickOpenItem(command, this.commands, this.keybindings));
const contexts = this.contexts.get(command.id);
if (!contexts || contexts.some(when => this.contextKeyService.match(when))) {
this.items.push(new CommandQuickOpenItem(command, this.commands, this.keybindings));
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import { MenusContributionPointHandler } from './menus-contribution-handler';
import 'mocha';
import * as sinon from 'sinon';
import { ContextKeyService } from '@theia/core/lib/browser/context-key-service';
import { QuickCommandService } from '@theia/core/lib/browser';

disableJSDOM();

Expand All @@ -54,6 +55,8 @@ before(() => {
bind(CommandRegistry).toSelf().inSingletonScope();
bind(ContextKeyService).toSelf().inSingletonScope();
bind(MenusContributionPointHandler).toSelf();
// tslint:disable-next-line:no-any mock QuickCommandService
bind(QuickCommandService).toConstantValue({} as any);
});

testContainer.load(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { MenuPath, ILogger, CommandRegistry } from '@theia/core';
import { EDITOR_CONTEXT_MENU } from '@theia/editor/lib/browser';
import { MenuModelRegistry } from '@theia/core/lib/common';
import { NAVIGATOR_CONTEXT_MENU } from '@theia/navigator/lib/browser/navigator-contribution';
import { QuickCommandService } from '@theia/core/lib/browser/quick-open/quick-command-service';
import { VIEW_ITEM_CONTEXT_MENU } from '../view/tree-views-main';
import { PluginContribution, Menu } from '../../../common';

Expand All @@ -34,6 +35,9 @@ export class MenusContributionPointHandler {
@inject(ILogger)
protected readonly logger: ILogger;

@inject(QuickCommandService)
protected readonly quickCommandService: QuickCommandService;

// menu location to command IDs
protected readonly registeredMenus: Map<string, Set<string>> = new Map();

Expand All @@ -49,7 +53,13 @@ export class MenusContributionPointHandler {
return;
}
for (const location in allMenus) {
if (allMenus.hasOwnProperty(location)) {
if (location === 'commandPalette') {
for (const menu of allMenus[location]) {
if (menu.when) {
this.quickCommandService.pushCommandContext(menu.command, menu.when);
}
}
} else if (allMenus.hasOwnProperty(location)) {
const menuPath = MenusContributionPointHandler.parseMenuPath(location);
if (!menuPath) {
this.logger.warn(`Plugin contributes items to a menu with invalid identifier: ${location}`);
Expand Down

0 comments on commit f16185d

Please sign in to comment.