feat(plugins): expose resource when keys for projectView, editorTab, and openIn (#8107)

Context-menu items contributed by plugins to projectView.context, editorTab.context and openIn can now test the clicked file in their when clause: resourcePath, resourceFilename, resourceExtname, resourceDir, resourceIsDir and resourceSelectionCount are evaluated on top of the global context keys.

Co-authored-by: RuyimgByCN <[email protected]>
Цей коміт міститься в:
RuyimgByCN authored and GitHub committed 2026-12-05 02:27:16 +08:00
1 джерело 6a1cd830f5
коміт 24b00c3db1
3 files changed
+102 -1

No files matched your search

+4
Переглянути файл
@@ -451,6 +451,10 @@ matching SDK. Plugins for 1.20 and earlier keep working. New in 1.21:
and posts `{ "op": "clone-url", "url": … }` when one is picked; the dialog
fills its URL field and switches to its normal form, so git runs only when the
user confirms. See _Clone sources_.
- **File-level context `when` keys for `projectView.context`,
`editorTab.context`, and `openIn`.** Expressions in those menus receive
`resourcePath`, `resourceFilename`, `resourceExtname`, `resourceDir`,
`resourceIsDir`, and `resourceSelectionCount`.
### API 1.19
+70
Переглянути файл
@@ -234,6 +234,76 @@ describe("plugin menus", () => {
off();
});
it("evaluates resource when keys for projectView, editorTab, and openIn", () => {
const off = registerAction({ id: "plugin.res.gen", title: "Generate XML", run: () => {} });
const plugin = { id: "res", name: "Res", status: "active", enabled: true } as unknown as Parameters<
typeof syncUiContributions
>[1][number];
syncUiContributions(
[
{
pluginId: "res",
point: "menus",
key: "projectView.context/xml",
decl: {
menu: "projectView.context",
command: "gen",
when: "resourceExtname == .xml && !resourceIsDir && resourceSelectionCount == 1",
},
},
{
pluginId: "res",
point: "menus",
key: "editorTab.context/mapper",
decl: {
menu: "editorTab.context",
command: "gen",
when: "resourceFilename == UserMapper.xml && resourceDir == /p/mappers",
},
},
{
pluginId: "res",
point: "menus",
key: "openIn/mapper",
decl: {
menu: "openIn",
command: "gen",
when: "resourcePath == /p/mappers/UserMapper.xml",
},
},
],
[plugin],
);
expect(pluginMenuItems("projectView", ["/p/mappers/UserMapper.xml"], undefined, [false])).toEqual([
{
kind: "action",
action: "plugin.res.gen",
arg: { path: "/p/mappers/UserMapper.xml", paths: ["/p/mappers/UserMapper.xml"] },
},
]);
expect(pluginMenuItems("projectView", ["/p/mappers/UserMapper.xml"], undefined, [true])).toEqual([]);
expect(
pluginMenuItems("projectView", ["/p/mappers/UserMapper.xml", "/p/mappers/Other.xml"], undefined, [false, false]),
).toEqual([]);
expect(pluginMenuItems("editorTab", ["/p/mappers/UserMapper.xml"], undefined, [false])).toEqual([
{
kind: "action",
action: "plugin.res.gen",
arg: { path: "/p/mappers/UserMapper.xml", paths: ["/p/mappers/UserMapper.xml"] },
},
]);
expect(pluginMenuItems("editorTab", ["/p/other/UserMapper.xml"], undefined, [false])).toEqual([]);
expect(pluginMenuItems("openIn", ["/p/mappers/UserMapper.xml"], { line: 1, column: 0 }, [false])).toEqual([
{
kind: "action",
action: "plugin.res.gen",
arg: { path: "/p/mappers/UserMapper.xml", paths: ["/p/mappers/UserMapper.xml"], line: 1, column: 0 },
},
]);
expect(pluginMenuItems("openIn", ["/p/mappers/Other.xml"], { line: 1, column: 0 }, [false])).toEqual([]);
off();
});
it("projects view item menu entries with the item key as argument", () => {
const off = registerAction({ id: "plugin.k.logs", title: "Logs", run: () => {} });
syncUiContributions(
+28 -1
Переглянути файл
@@ -159,16 +159,42 @@ function flattenSingleGroups(items: MenuItem[]): MenuItem[] {
);
}
function resourceWhenContext(
paths: readonly string[],
dirs?: readonly boolean[],
): Record<string, string | boolean | number> | undefined {
const path = paths[0];
if (!path) return undefined;
const slash = Math.max(path.lastIndexOf("/"), path.lastIndexOf("\\"));
const resourceFilename = slash >= 0 ? path.slice(slash + 1) : path;
const resourceDir = slash > 0 ? path.slice(0, slash) : slash === 0 ? path.slice(0, 1) : "";
const resourceIsDir = Boolean(dirs?.[0]);
const dot = resourceIsDir ? -1 : resourceFilename.lastIndexOf(".");
const resourceExtname = dot > 0 ? resourceFilename.slice(dot) : "";
return {
resourcePath: path,
resourceFilename,
resourceExtname,
resourceDir,
resourceIsDir,
resourceSelectionCount: paths.length,
};
}
/** Plugin items of a context menu: their command's action with the menu's file as argument. */
export function pluginMenuItems(
menu: ContextMenuId,
paths: readonly string[],
position?: { line: number; column: number },
dirs?: readonly boolean[],
): MenuItem[] {
const out: MenuItem[] = [];
const groups = new Map<string, MenuItem[]>();
const resource =
menu === "projectView" || menu === "editorTab" || menu === "openIn" ? resourceWhenContext(paths, dirs) : undefined;
const evalCtx = resource ? { ...getContext(), ...resource } : undefined;
for (const { pluginId, decl } of contextItems.values()) {
if (CONTEXT_MENUS[decl.menu] !== menu || !evaluateWhen(decl.when ?? undefined)) continue;
if (CONTEXT_MENUS[decl.menu] !== menu || !evaluateWhen(decl.when ?? undefined, evalCtx)) continue;
const action = pluginActionId(pluginId, decl.command);
if (!getAction(action)) continue;
// The editor menu runs with the focused editor; the others name their file.
@@ -773,6 +799,7 @@ export function startViewUi(openInput: (input: ReturnType<typeof customEditorInp
menu,
ctx.paths,
ctx.line === undefined ? undefined : { line: ctx.line, column: ctx.column ?? 0 },
ctx.dirs,
);
const path = ctx.paths[0];
if ((menu === "projectView" || menu === "editorTab") && path && !ctx.dirs?.[0]) {