Skip to content

Commit

Permalink
...
Browse files Browse the repository at this point in the history
  • Loading branch information
TwIStOy committed Nov 23, 2023
1 parent 2594664 commit 69b4a8c
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 14 deletions.
49 changes: 39 additions & 10 deletions src/conf/ui/right-click.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function normalizeKeys(keys?: string | string[]): string[] {
}
}

function intoMenuItem(buffer: VimBuffer, item: RightClickMenuItem): MenuItem[] {
function intoMenuItem(item: RightClickMenuItem): MenuItem[] {
if ("actionId" in item) {
return [
new MenuItem(
Expand All @@ -58,7 +58,7 @@ function intoMenuItem(buffer: VimBuffer, item: RightClickMenuItem): MenuItem[] {
{
keys: normalizeKeys(item.keys),
alwaysInMenu: item.alwaysInMenu,
enabled: () => {
enabled: (buffer: VimBuffer) => {
let action = ActionRegistry.getInstance().get(item.actionId);
if (action === undefined) {
return false;
Expand All @@ -71,18 +71,51 @@ function intoMenuItem(buffer: VimBuffer, item: RightClickMenuItem): MenuItem[] {
} else if ("children" in item) {
return [
new MenuItem(item.title, () => {}, {
children: item.children.map((v) => intoMenuItem(buffer, v)).flat(),
children: item.children.map((v) => intoMenuItem(v)).flat(),
keys: normalizeKeys(item.keys),
}),
];
} else {
return [
new MenuItem("---", () => {}),
...item.items.map((v) => intoMenuItem(buffer, v)).flat(),
...item.items.map((v) => intoMenuItem(v)).flat(),
];
}
}

function filterMenuItems(buffer: VimBuffer, items: MenuItem[]): MenuItem[] {
return clearContiguousSeparators(
items.filter((v) => {
if (!v.enabled(buffer)) {
return false;
}
if (v.children.length > 0) {
v.children = clearContiguousSeparators(
filterMenuItems(buffer, v.children)
);
}
return true;
})
);
}

function clearContiguousSeparators(items: MenuItem[]): MenuItem[] {
let ret: MenuItem[] = [];
let lastIsSeparator = false;
for (let item of items) {
if (item.text.isSeparator()) {
if (lastIsSeparator) {
continue;
}
lastIsSeparator = true;
} else {
lastIsSeparator = false;
}
ret.push(item);
}
return ret;
}

const CppToolkitGroup: RightClickMenuGroup = {
items: [
{
Expand Down Expand Up @@ -164,13 +197,9 @@ export const rightClickMenu: RightClickMenuItem[] = [
CopilotGroup,
];

const _rightClickMenuCache = new Cache();

export function mountRightClickMenu(buffer: VimBuffer, opt?: any): void {
let items = _rightClickMenuCache.ensure(buffer.asCacheKey(), () => {
return rightClickMenu.map((v) => intoMenuItem(buffer, v)).flat();
});
let menu = new ContextMenu(items);
let items = rightClickMenu.map((v) => intoMenuItem(v)).flat();
let menu = new ContextMenu(filterMenuItems(buffer, items));
vim.schedule(() => {
menu.asNuiMenu(opt ?? {}).mount();
});
Expand Down
9 changes: 5 additions & 4 deletions src/core/components/menu-item.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { uniqueArray } from "@core/utils/array";
import { MenuText } from "./menu-text";
import { NuiMenuMod } from "@extra/nui";
import { ContextMenu, ContextMenuContext } from "./context-menu";
import { VimBuffer } from "@core/vim";

const builtinKeys = [
"j",
Expand Down Expand Up @@ -33,7 +34,7 @@ export class MenuItem {
public description?: string;
public callback: (this: void) => void;
public parent?: NuiMenu<MenuItemContext, ContextMenuContext>;
public _enabled: boolean | (() => boolean);
public _enabled: boolean | ((buffer: VimBuffer) => boolean);
public readonly alwaysInMenu: boolean;

private _keys: string[];
Expand All @@ -45,7 +46,7 @@ export class MenuItem {
children?: MenuItem[];
description?: string;
keys?: string[];
enabled?: boolean | (() => boolean);
enabled?: boolean | ((buffer: VimBuffer) => boolean);
alwaysInMenu?: boolean;
}
) {
Expand Down Expand Up @@ -77,9 +78,9 @@ export class MenuItem {
return this.text.length;
}

public get enabled(): boolean {
public enabled(buffer: VimBuffer): boolean {
if (typeof this._enabled === "function") {
return this._enabled();
return this._enabled(buffer);
}
return this._enabled;
}
Expand Down

0 comments on commit 69b4a8c

Please sign in to comment.