|
| 1 | +/** |
| 2 | + * @module CreateApiFactory |
| 3 | + * @description The primary factory function that constructs the `vscode` API |
| 4 | + * object for a given extension. |
| 5 | + */ |
| 6 | + |
| 7 | +import type { IExtensionDescription } from "vs/platform/extensions/common/extensions.js"; |
| 8 | +import type * as Vscode from "vscode"; |
| 9 | + |
| 10 | +import type * as Service from "../../Service/mod.js"; |
| 11 | +import * as ExtHostType from "../../Type/ExtHostTypes.js"; |
| 12 | +import { AsExtensionEvent } from "./AsExtensionEvent.js"; |
| 13 | +import { CreateCommandsNamespace } from "./CreateCommandsNamespace.js"; |
| 14 | +import { CreateLanguagesNamespace } from "./CreateLanguagesNamespace.js"; |
| 15 | +import { CreateWindowNamespace } from "./CreateWindowNamespace.js"; |
| 16 | +import { CreateWorkspaceNamespace } from "./CreateWorkspaceNamespace.js"; |
| 17 | +import type { Interface as ApiFactory } from "./Service.js"; |
| 18 | + |
| 19 | +// Placeholders for other namespace creators |
| 20 | +// import { CreateDebugNamespace } from "./CreateDebugNamespace.js"; |
| 21 | +// import { CreateTasksNamespace } from "./CreateTasksNamespace.js"; |
| 22 | +// import { CreateWebviewNamespace } from "./CreateWebviewNamespace.js"; |
| 23 | + |
| 24 | +/** |
| 25 | + * Creates an `ApiFactory` instance. |
| 26 | + * |
| 27 | + * This function uses a dependency injection pattern, taking all necessary |
| 28 | + * extension host services as arguments. It returns a factory object with a |
| 29 | + * `Create` method, which is then used to construct the specific `vscode` API |
| 30 | + * object for each individual extension. |
| 31 | + * |
| 32 | + * @param LogService The service for logging messages. |
| 33 | + * @param DeprecationService The service for handling API deprecations. |
| 34 | + * @param CommandsService The service for command registration and execution. |
| 35 | + * @param WorkspaceService The service for workspace-related information and events. |
| 36 | + * @param WindowService The service for window-related UI and events. |
| 37 | + * @param LanguageFeaturesService The service for registering language providers. |
| 38 | + * @param StatusBarService The service for managing status bar items. |
| 39 | + * @param WebviewPanelService The service for creating and managing webview panels. |
| 40 | + * @param CustomEditorService The service for registering custom editors. |
| 41 | + * @param TreeViewService The service for creating and managing tree views. |
| 42 | + * @returns An `ApiFactory` object capable of creating `vscode` API instances. |
| 43 | + */ |
| 44 | +export const CreateApiFactory = ( |
| 45 | + LogService: Service.Log.Interface, |
| 46 | + DeprecationService: Service.ApiDeprecation.Interface, |
| 47 | + CommandsService: Service.Commands.Interface, |
| 48 | + WorkspaceService: Service.Workspace.Interface, |
| 49 | + WindowService: Service.Window.Interface, |
| 50 | + LanguageFeaturesService: Service.LanguageFeatures.Interface, |
| 51 | + StatusBarService: Service.StatusBar.Interface, |
| 52 | + WebviewPanelService: Service.WebviewPanel.Interface, |
| 53 | + CustomEditorService: Service.CustomEditor.Interface, |
| 54 | + TreeViewService: Service.TreeView.Interface, |
| 55 | + // Add other services like Debug, Tasks, etc. as they are implemented |
| 56 | +): ApiFactory => ({ |
| 57 | + /** |
| 58 | + * Creates a new, sandboxed `vscode` API object for a specific extension. |
| 59 | + * @param Extension The full description of the extension. |
| 60 | + * @returns A frozen `vscode` API object. |
| 61 | + */ |
| 62 | + Create: (Extension: IExtensionDescription): typeof Vscode => { |
| 63 | + // --- Create Namespaces --- |
| 64 | + const CommandsNamespace = CreateCommandsNamespace( |
| 65 | + CommandsService, |
| 66 | + Extension, |
| 67 | + ); |
| 68 | + const WorkspaceNamespace = CreateWorkspaceNamespace( |
| 69 | + WorkspaceService, |
| 70 | + DeprecationService, |
| 71 | + Extension, |
| 72 | + ); |
| 73 | + const WindowNamespace = CreateWindowNamespace( |
| 74 | + WindowService, |
| 75 | + WorkspaceService, |
| 76 | + StatusBarService, |
| 77 | + WebviewPanelService, |
| 78 | + CustomEditorService, |
| 79 | + TreeViewService, |
| 80 | + (Event) => |
| 81 | + AsExtensionEvent(Extension.identifier, LogService, Event), |
| 82 | + Extension, |
| 83 | + ); |
| 84 | + const LanguagesNamespace = CreateLanguagesNamespace( |
| 85 | + LanguageFeaturesService, |
| 86 | + Extension, |
| 87 | + ); |
| 88 | + // const DebugNamespace = CreateDebugNamespace(DebugService, Extension); |
| 89 | + // const TasksNamespace = CreateTasksNamespace(TasksService, Extension); |
| 90 | + |
| 91 | + // --- Assemble Final API Object --- |
| 92 | + const Api = { |
| 93 | + // This version should come from a centralized product service. |
| 94 | + version: "1.85.0", |
| 95 | + commands: CommandsNamespace, |
| 96 | + window: WindowNamespace, |
| 97 | + workspace: WorkspaceNamespace, |
| 98 | + languages: LanguagesNamespace, |
| 99 | + // Other namespaces would be added here. |
| 100 | + // debug: DebugNamespace, |
| 101 | + // tasks: TasksNamespace, |
| 102 | + |
| 103 | + // --- Static Types and Enums from VS Code --- |
| 104 | + ...ExtHostType, |
| 105 | + }; |
| 106 | + |
| 107 | + // --- Freeze API to prevent runtime modification by extensions --- |
| 108 | + Object.freeze(Api.commands); |
| 109 | + Object.freeze(Api.window); |
| 110 | + Object.freeze(Api.workspace); |
| 111 | + Object.freeze(Api.languages); |
| 112 | + // ... freeze other namespaces ... |
| 113 | + |
| 114 | + return Object.freeze(Api) as typeof Vscode; |
| 115 | + }, |
| 116 | +}); |
0 commit comments