Skip to content

Commit

Permalink
[vscode/menu] provide current selected resource as a command argument
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 30, 2019
1 parent 627c974 commit e5c8ff9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { enableJSDOM } from '@theia/core/lib/browser/test/jsdom';
const disableJSDOM = enableJSDOM();

import { Container, ContainerModule } from 'inversify';
import { ILogger, MessageClient, MessageService, MenuPath, MenuAction, CommandRegistry, bindContributionProvider, CommandContribution } from '@theia/core';
import { ILogger, MessageClient, MessageService, MenuPath, MenuAction, CommandRegistry, bindContributionProvider, CommandContribution, SelectionService } from '@theia/core';
import { MenuModelRegistry } from '@theia/core/lib/common';
import { MockLogger } from '@theia/core/lib/common/test/mock-logger';
import { MockMenuModelRegistry } from '@theia/core/lib/common/test/mock-menu';
Expand Down Expand Up @@ -63,6 +63,7 @@ before(() => {
bind(TabBarToolbarRegistry).toConstantValue({} as any);
// tslint:disable-next-line:no-any mock PluginSharedStyle
bind(PluginSharedStyle).toConstantValue({} as any);
bind(SelectionService).toSelf().inSingletonScope();
});

testContainer.load(module);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
// tslint:disable:no-any

import { injectable, inject } from 'inversify';
import CoreURI from '@theia/core/lib/common/uri';
import { MenuPath, ILogger, CommandRegistry, Command, Mutable, MenuAction } from '@theia/core';
import { MenuPath, ILogger, CommandRegistry, Command, Mutable, MenuAction, SelectionService, UriSelection } from '@theia/core';
import { EDITOR_CONTEXT_MENU, EditorWidget } from '@theia/editor/lib/browser';
import { MenuModelRegistry } from '@theia/core/lib/common';
import { TabBarToolbarRegistry, TabBarToolbarItem } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
Expand Down Expand Up @@ -47,6 +46,9 @@ export class MenusContributionPointHandler {
@inject(TabBarToolbarRegistry)
protected readonly tabBarToolbar: TabBarToolbarRegistry;

@inject(SelectionService)
protected readonly selectionService: SelectionService;

handle(contributions: PluginContribution): void {
const allMenus = contributions.menus;
if (!allMenus) {
Expand Down Expand Up @@ -111,12 +113,15 @@ export class MenusContributionPointHandler {
protected registerMenuAction(menuPath: MenuPath, menu: Menu): void {
const commandId = this.createSyntheticCommandId(menu, { prefix: '__plugin.menu.action.' });
const command: Command = { id: commandId };
// convert Core URI of a selected resource to a plugin (VS Code) URI format
const resolveArgs = (...args: any[]) => (args || []).map(arg => arg instanceof CoreURI ? arg['codeUri'] : arg);
const selectedResource = () => {
const selection = this.selectionService.selection;
const uri = UriSelection.getUri(selection);
return uri ? uri['codeUri'] : (typeof selection !== 'object' && typeof selection !== 'function') ? selection : undefined;
};
this.commands.registerCommand(command, {
execute: (...args: any[]) => this.commands.executeCommand(menu.command, ...resolveArgs(...args)),
isEnabled: (...args: any[]) => this.commands.isEnabled(menu.command, ...resolveArgs(...args)),
isVisible: (...args: any[]) => this.commands.isVisible(menu.command, ...resolveArgs(...args))
execute: () => this.commands.executeCommand(menu.command, selectedResource()),
isEnabled: () => this.commands.isEnabled(menu.command, selectedResource()),
isVisible: () => this.commands.isVisible(menu.command, selectedResource())
});

const { when } = menu;
Expand Down

0 comments on commit e5c8ff9

Please sign in to comment.