From a36516d46355b86ad408e372c2bbb0ca931aaeaa Mon Sep 17 00:00:00 2001 From: l0ng-ai Date: Wed, 2 Dec 2026 14:28:15 +0800 Subject: [PATCH] refactor(database): take plugin table menu entries from a workbench registry (#7423) The Database tree imported pluginDatabaseTableMenuItems from features/plugins directly (from #7372). The table context type and a provider registry now live in workbench/contributions; features/plugins registers its provider. --- src/features/database/treeMenu.ts | 5 ++-- .../plugins/__tests__/databaseMenus.test.tsx | 3 ++- src/features/plugins/uiPoints.tsx | 20 ++++++--------- src/workbench/contributions.ts | 25 +++++++++++++++++++ 4 files changed, 37 insertions(+), 16 deletions(-) diff --git a/src/features/database/treeMenu.ts b/src/features/database/treeMenu.ts index c79c9a9a1..560d4eb3c 100644 --- a/src/features/database/treeMenu.ts +++ b/src/features/database/treeMenu.ts @@ -1,9 +1,8 @@ /** Menus of the Database tool window, laid out like IDEA's (New ▸, Copy/Paste ▸, SQL Scripts ▸ …). */ import { choosePath } from "@/platform/choosePath"; -import { pluginDatabaseTableMenuItems } from "@/features/plugins/uiPoints"; import { tx } from "@/platform/i18n"; import type { MenuItem } from "@/ui"; -import { notifications } from "@/workbench/contributions"; +import { databaseTableMenuItems, notifications } from "@/workbench/contributions"; import { fileSourceName, parseSourceUrl } from "./model"; import { VENDORS, vendorById, vendorOf, vendorPreset, type DbVendor } from "./vendors"; import { MAX_RECENT_VENDORS, readRecentVendors } from "./recentVendors"; @@ -208,7 +207,7 @@ export function nodeMenu(n: DbNode | null, selection: readonly DbNode[] = []): M kind: node.table.kind, driver: sourceById(node.id)?.driver ?? "", })); - const plugins = pluginDatabaseTableMenuItems(tables); + const plugins = databaseTableMenuItems(tables); return plugins.length ? [...builtin, sep, ...plugins] : builtin; } diff --git a/src/features/plugins/__tests__/databaseMenus.test.tsx b/src/features/plugins/__tests__/databaseMenus.test.tsx index b61307c12..e805e9add 100644 --- a/src/features/plugins/__tests__/databaseMenus.test.tsx +++ b/src/features/plugins/__tests__/databaseMenus.test.tsx @@ -6,7 +6,8 @@ import { useDatabase } from "@/features/database/store"; import type { DbNode } from "@/features/database/nodes"; import { defineBuiltinUiPoints } from "../points"; import { clearUiContributions, syncUiContributions } from "../projection"; -import { pluginDatabaseTableMenuItems, type DatabaseTableContext } from "../uiPoints"; +import type { DatabaseTableContext } from "@/workbench/contributions"; +import { pluginDatabaseTableMenuItems } from "../uiPoints"; const initialDatabase = useDatabase.getState(); let stop: () => void; diff --git a/src/features/plugins/uiPoints.tsx b/src/features/plugins/uiPoints.tsx index 61c7cee3f..4c32c1f36 100644 --- a/src/features/plugins/uiPoints.tsx +++ b/src/features/plugins/uiPoints.tsx @@ -24,7 +24,11 @@ import { evaluateWhen, getContext, useContextKeys } from "@/platform/context"; import { tx } from "@/platform/i18n"; import { isMacHost, registerExtensionBindings } from "@/platform/keymap"; import { addFileIconProvider, bumpFileIconRevision, fileIconName, registerExternalIcons, type MenuItem } from "@/ui"; -import { addContributedFileTemplate } from "@/workbench/contributions"; +import { + addContributedFileTemplate, + registerDatabaseTableMenuProvider, + type DatabaseTableContext, +} from "@/workbench/contributions"; import { addContributedSnippets, registerEditorKind, @@ -188,17 +192,7 @@ export function pluginViewItemMenuItems(pluginId: string, view: string, key: str return flattenSingleGroups(out); } -/** Non-secret table context passed to Database table menu commands (API 1.20). */ -export interface DatabaseTableContext { - sourceId: string; - catalog: string | null; - schema: string; - table: string; - kind: string; - driver: string; -} - -/** The right-clicked table is included in selection by the Database tree. */ +/** Database table menu commands (API 1.20); the right-clicked table is included in selection by the Database tree. */ export function pluginDatabaseTableMenuItems(tables: readonly DatabaseTableContext[]): MenuItem[] { if (!tables.length) return []; const out: MenuItem[] = []; @@ -235,6 +229,8 @@ export function pluginDatabaseTableMenuItems(tables: readonly DatabaseTableConte return flattenSingleGroups(out); } +registerDatabaseTableMenuProvider(pluginDatabaseTableMenuItems); + /** The when keys of one selected service. */ export function serviceWhenContext(service: ServiceContext, count: number) { return { diff --git a/src/workbench/contributions.ts b/src/workbench/contributions.ts index 307baf127..ad0bc77f8 100644 --- a/src/workbench/contributions.ts +++ b/src/workbench/contributions.ts @@ -1152,6 +1152,31 @@ export interface CommitCheckProvider { export const useCommitCheckProviders = create<{ providers: CommitCheckProvider[] }>(() => ({ providers: [] })); +/** The non-secret identity of a table or view selected in the Database tool window. */ +export interface DatabaseTableContext { + sourceId: string; + catalog: string | null; + schema: string; + table: string; + kind: string; + driver: string; +} + +/** Contributed entries of a table's context menu in the Database tool window (plugins'). */ +export type DatabaseTableMenuProvider = (tables: readonly DatabaseTableContext[]) => MenuItem[]; + +const databaseTableMenuProviders = new Set(); + +export function registerDatabaseTableMenuProvider(provider: DatabaseTableMenuProvider): () => void { + databaseTableMenuProviders.add(provider); + return () => void databaseTableMenuProviders.delete(provider); +} + +/** The contributed entries for the selected `tables`, in registration order. */ +export function databaseTableMenuItems(tables: readonly DatabaseTableContext[]): MenuItem[] { + return [...databaseTableMenuProviders].flatMap((provider) => provider(tables)); +} + export function registerCommitCheckProvider(p: CommitCheckProvider): () => void { useCommitCheckProviders.setState((s) => ({ providers: [...s.providers, p] })); return () => useCommitCheckProviders.setState((s) => ({ providers: s.providers.filter((x) => x !== p) }));