Skip to content

Commit

Permalink
Register VSCode items directly with corresponding internal menus (ecl…
Browse files Browse the repository at this point in the history
…ipse-theia#11741)

Fixes bug where inline items were not appearing in TreeViews.
  • Loading branch information
colin-grant-work authored and erezmus committed Sep 4, 2023
1 parent 79da24d commit c9271f3
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 46 deletions.
9 changes: 3 additions & 6 deletions packages/core/src/common/menu/composite-menu-node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,9 @@ export class CompositeMenuNode implements MenuNode, CompoundMenuNode, CompoundMe
* @param id node id.
*/
public removeNode(id: string): void {
const node = this._children.find(n => n.id === id);
if (node) {
const idx = this._children.indexOf(node);
if (idx >= 0) {
this._children.splice(idx, 1);
}
const idx = this._children.findIndex(n => n.id === id);
if (idx >= 0) {
this._children.splice(idx, 1);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
/* eslint-disable @typescript-eslint/no-explicit-any */

import { inject, injectable, optional } from '@theia/core/shared/inversify';
import { MenuPath, CommandRegistry, Disposable, DisposableCollection, ActionMenuNode, MenuCommandAdapterRegistry, Emitter, CompoundMenuNodeRole } from '@theia/core';
import { MenuPath, CommandRegistry, Disposable, DisposableCollection, ActionMenuNode, MenuCommandAdapterRegistry, Emitter } from '@theia/core';
import { MenuModelRegistry } from '@theia/core/lib/common';
import { TabBarToolbarRegistry } from '@theia/core/lib/browser/shell/tab-bar-toolbar';
import { DeployedPlugin, IconUrl, Menu } from '../../../common';
import { ScmWidget } from '@theia/scm/lib/browser/scm-widget';
import { PluginViewWidget } from '../view/plugin-view-widget';
import { QuickCommandService } from '@theia/core/lib/browser';
import {
CodeEditorWidgetUtil, codeToTheiaMappings, ContributionPoint, implementedVSCodeContributionPoints,
CodeEditorWidgetUtil, codeToTheiaMappings, ContributionPoint,
PLUGIN_EDITOR_TITLE_MENU, PLUGIN_SCM_TITLE_MENU, PLUGIN_VIEW_TITLE_MENU
} from './vscode-theia-menu-mappings';
import { PluginMenuCommandAdapter, ReferenceCountingSet } from './plugin-menu-command-adapter';
Expand Down Expand Up @@ -55,10 +55,6 @@ export class MenusContributionPointHandler {
private initialize(): void {
this.initialized = true;
this.commandAdapterRegistry.registerAdapter(this.commandAdapter);
for (const contributionPoint of implementedVSCodeContributionPoints) {
this.menuRegistry.registerIndependentSubmenu(contributionPoint, '');
this.getMatchingMenu(contributionPoint)!.forEach(([menu, when]) => this.menuRegistry.linkSubmenu(menu, contributionPoint, { role: CompoundMenuNodeRole.Flat, when }));
}
this.tabBarToolbar.registerMenuDelegate(PLUGIN_EDITOR_TITLE_MENU, widget => this.codeEditorWidgetUtil.is(widget));
this.tabBarToolbar.registerMenuDelegate(PLUGIN_SCM_TITLE_MENU, widget => widget instanceof ScmWidget);
this.tabBarToolbar.registerMenuDelegate(PLUGIN_VIEW_TITLE_MENU, widget => widget instanceof PluginViewWidget);
Expand All @@ -70,7 +66,7 @@ export class MenusContributionPointHandler {
});
}

private getMatchingMenu(contributionPoint: ContributionPoint): Array<[MenuPath] | [MenuPath, string]> | undefined {
private getMatchingMenu(contributionPoint: ContributionPoint): MenuPath[] | undefined {
return codeToTheiaMappings.get(contributionPoint);
}

Expand All @@ -96,20 +92,22 @@ export class MenusContributionPointHandler {
toDispose.push(this.registerCommandPaletteAction(item));
} else {
this.checkTitleContribution(contributionPoint, item, toDispose);
if (item.submenu) {
const targets = this.getMatchingMenu(contributionPoint as ContributionPoint) ?? [contributionPoint];
const { group, order } = this.parseGroup(item.group);
targets.forEach(([target]) => toDispose.push(this.menuRegistry.linkSubmenu(target, item.submenu!, { order, when: item.when }, group)));
} else if (item.command) {
toDispose.push(this.commandAdapter.addCommand(item.command));
const { group, order } = this.parseGroup(item.group);
const node = new ActionMenuNode({
commandId: item.command,
when: item.when,
order,
}, this.commands);
const parent = this.menuRegistry.getMenuNode(contributionPoint, group);
toDispose.push(parent.addNode(node));
const targets = this.getMatchingMenu(contributionPoint as ContributionPoint) ?? [contributionPoint];
const { group, order } = this.parseGroup(item.group);
const { submenu, command } = item;
if (submenu) {
targets.forEach(target => toDispose.push(this.menuRegistry.linkSubmenu(target, submenu!, { order, when: item.when }, group)));
} else if (command) {
toDispose.push(this.commandAdapter.addCommand(command));
targets.forEach(target => {
const node = new ActionMenuNode({
commandId: command,
when: item.when,
order,
}, this.commands);
const parent = this.menuRegistry.getMenuNode(target, group);
toDispose.push(parent.addNode(node));
});
}
}
} catch (error) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ export class PluginMenuCommandAdapter implements MenuCommandAdapter {
if (adapter) {
const paths = codeToTheiaMappings.get(contributionPoint);
if (paths) {
paths.forEach(([path]) => this.addArgumentAdapter(path, adapter));
paths.forEach(path => this.addArgumentAdapter(path, adapter));
}
}
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,23 +53,23 @@ export const implementedVSCodeContributionPoints = [

export type ContributionPoint = (typeof implementedVSCodeContributionPoints)[number];

/** The values are combinations of MenuPath and `when` clause, if any */
export const codeToTheiaMappings = new Map<ContributionPoint, Array<[MenuPath] | [MenuPath, string]>>([
['comments/comment/context', [[COMMENT_CONTEXT]]],
['comments/comment/title', [[COMMENT_TITLE]]],
['comments/commentThread/context', [[COMMENT_THREAD_CONTEXT]]],
['debug/callstack/context', [[DebugStackFramesWidget.CONTEXT_MENU], [DebugThreadsWidget.CONTEXT_MENU]]],
['editor/context', [[EDITOR_CONTEXT_MENU]]],
['editor/title', [[PLUGIN_EDITOR_TITLE_MENU]]],
['editor/title/context', [[SHELL_TABBAR_CONTEXT_MENU, 'resourceSet']]],
['explorer/context', [[NAVIGATOR_CONTEXT_MENU]]],
['scm/resourceFolder/context', [[ScmTreeWidget.RESOURCE_FOLDER_CONTEXT_MENU]]],
['scm/resourceGroup/context', [[ScmTreeWidget.RESOURCE_GROUP_CONTEXT_MENU]]],
['scm/resourceState/context', [[ScmTreeWidget.RESOURCE_CONTEXT_MENU]]],
['scm/title', [[PLUGIN_SCM_TITLE_MENU]]],
['timeline/item/context', [[TIMELINE_ITEM_CONTEXT_MENU]]],
['view/item/context', [[VIEW_ITEM_CONTEXT_MENU]]],
['view/title', [[PLUGIN_VIEW_TITLE_MENU]]],
/** The values are menu paths to which the VSCode contribution points correspond */
export const codeToTheiaMappings = new Map<ContributionPoint, MenuPath[]>([
['comments/comment/context', [COMMENT_CONTEXT]],
['comments/comment/title', [COMMENT_TITLE]],
['comments/commentThread/context', [COMMENT_THREAD_CONTEXT]],
['debug/callstack/context', [DebugStackFramesWidget.CONTEXT_MENU, DebugThreadsWidget.CONTEXT_MENU]],
['editor/context', [EDITOR_CONTEXT_MENU]],
['editor/title', [PLUGIN_EDITOR_TITLE_MENU]],
['editor/title/context', [SHELL_TABBAR_CONTEXT_MENU]],
['explorer/context', [NAVIGATOR_CONTEXT_MENU]],
['scm/resourceFolder/context', [ScmTreeWidget.RESOURCE_FOLDER_CONTEXT_MENU]],
['scm/resourceGroup/context', [ScmTreeWidget.RESOURCE_GROUP_CONTEXT_MENU]],
['scm/resourceState/context', [ScmTreeWidget.RESOURCE_CONTEXT_MENU]],
['scm/title', [PLUGIN_SCM_TITLE_MENU]],
['timeline/item/context', [TIMELINE_ITEM_CONTEXT_MENU]],
['view/item/context', [VIEW_ITEM_CONTEXT_MENU]],
['view/title', [PLUGIN_VIEW_TITLE_MENU]],
]);

type CodeEditorWidget = EditorWidget | WebviewWidget;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ import { WidgetDecoration } from '@theia/core/lib/browser/widget-decoration';

export const TREE_NODE_HYPERLINK = 'theia-TreeNodeHyperlink';
export const VIEW_ITEM_CONTEXT_MENU: MenuPath = ['view-item-context-menu'];
export const VIEW_ITEM_INLINE_MENU: MenuPath = ['view-item-inline-menu'];
export const VIEW_ITEM_INLINE_MENU: MenuPath = ['view-item-context-menu', 'inline'];

export interface SelectionEventHandler {
readonly node: SelectableTreeNode;
Expand Down Expand Up @@ -288,6 +288,7 @@ export class TreeViewWidget extends TreeViewWelcomeWidget {
this.model.onDidChangeWelcomeState(this.update, this);
this.toDispose.push(this.model.onDidChangeWelcomeState(this.update, this));
this.toDispose.push(this.onDidChangeVisibilityEmitter);
this.toDispose.push(this.contextKeyService.onDidChange(() => this.update()));
}

protected markdownItPlugin(): void {
Expand Down

0 comments on commit c9271f3

Please sign in to comment.