From 24b00c3db1153299ddecf72da70e677b132c32eb Mon Sep 17 00:00:00 2001 From: RuyimgByCN <45452808+RuyimgByCN@users.noreply.github.com> Date: Sat, 5 Dec 2026 02:27:16 +0800 Subject: [PATCH] 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 <45452808+RuyimgByCN@users.noreply.github.com> --- plugins/README.md | 4 ++ src/features/plugins/__tests__/views.test.tsx | 70 +++++++++++++++++++ src/features/plugins/uiPoints.tsx | 29 +++++++- 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/plugins/README.md b/plugins/README.md index a70c97d3cc..8dc06152a1 100644 --- a/plugins/README.md +++ b/plugins/README.md @@ -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 diff --git a/src/features/plugins/__tests__/views.test.tsx b/src/features/plugins/__tests__/views.test.tsx index bc3897aec9..418c5c282b 100644 --- a/src/features/plugins/__tests__/views.test.tsx +++ b/src/features/plugins/__tests__/views.test.tsx @@ -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( diff --git a/src/features/plugins/uiPoints.tsx b/src/features/plugins/uiPoints.tsx index 98077c9664..8c233db8fc 100644 --- a/src/features/plugins/uiPoints.tsx +++ b/src/features/plugins/uiPoints.tsx @@ -159,16 +159,42 @@ function flattenSingleGroups(items: MenuItem[]): MenuItem[] { ); } +function resourceWhenContext( + paths: readonly string[], + dirs?: readonly boolean[], +): Record | 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(); + 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