Skip to content

Commit

Permalink
make the internals fit for #9827
Browse files Browse the repository at this point in the history
  • Loading branch information
jrieken committed Nov 21, 2019
1 parent 8da81e5 commit 14928af
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
26 changes: 19 additions & 7 deletions src/vs/editor/contrib/contextmenu/contextmenu.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ import { ICodeEditor, IEditorMouseEvent, MouseTargetType } from 'vs/editor/brows
import { EditorAction, ServicesAccessor, registerEditorAction, registerEditorContribution } from 'vs/editor/browser/editorExtensions';
import { IEditorContribution, ScrollType } from 'vs/editor/common/editorCommon';
import { EditorContextKeys } from 'vs/editor/common/editorContextKeys';
import { IMenuService, MenuId } from 'vs/platform/actions/common/actions';
import { IMenuService, MenuId, SubmenuItemAction } from 'vs/platform/actions/common/actions';
import { IContextKeyService } from 'vs/platform/contextkey/common/contextkey';
import { IContextMenuService, IContextViewService } from 'vs/platform/contextview/browser/contextView';
import { IKeybindingService } from 'vs/platform/keybinding/common/keybinding';
import { KeybindingWeight } from 'vs/platform/keybinding/common/keybindingsRegistry';
import { ITextModel } from 'vs/editor/common/model';
import { IMouseWheelEvent } from 'vs/base/browser/mouseEvent';
import { EditorOption } from 'vs/editor/common/config/editorOptions';
import { ContextSubMenu } from 'vs/base/browser/contextmenu';

export class ContextMenuController implements IEditorContribution {

Expand Down Expand Up @@ -128,24 +129,35 @@ export class ContextMenuController implements IEditorContribution {
}

// Find actions available for menu
const menuActions = this._getMenuActions(this._editor.getModel());
const menuActions = this._getMenuActions(this._editor.getModel(), MenuId.EditorContext);

// Show menu if we have actions to show
if (menuActions.length > 0) {
this._doShowContextMenu(menuActions, anchor);
}
}

private _getMenuActions(model: ITextModel): ReadonlyArray<IAction> {
private _getMenuActions(model: ITextModel, menuId: MenuId): IAction[] {
const result: IAction[] = [];

let contextMenu = this._menuService.createMenu(MenuId.EditorContext, this._contextKeyService);
const groups = contextMenu.getActions({ arg: model.uri });
contextMenu.dispose();
// get menu groups
const menu = this._menuService.createMenu(menuId, this._contextKeyService);
const groups = menu.getActions({ arg: model.uri });
menu.dispose();

// translate them into other actions
for (let group of groups) {
const [, actions] = group;
result.push(...actions);
for (const action of actions) {
if (action instanceof SubmenuItemAction) {
const subActions = this._getMenuActions(model, action.item.submenu);
if (subActions.length > 0) {
result.push(new ContextSubMenu(action.label, subActions));
}
} else {
result.push(action);
}
}
result.push(new Separator());
}
result.pop(); // remove last separator
Expand Down
5 changes: 4 additions & 1 deletion src/vs/platform/actions/common/menuService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,10 @@ class Menu implements IMenu {
const activeActions: Array<MenuItemAction | SubmenuItemAction> = [];
for (const item of items) {
if (this._contextKeyService.contextMatchesRules(item.when)) {
const action = isIMenuItem(item) ? new MenuItemAction(item.command, item.alt, options, this._contextKeyService, this._commandService) : new SubmenuItemAction(item);
const action = isIMenuItem(item)
? new MenuItemAction(item.command, item.alt, options, this._contextKeyService, this._commandService)
: new SubmenuItemAction(item);

activeActions.push(action);
}
}
Expand Down

0 comments on commit 14928af

Please sign in to comment.