import type { FilesApi, PluginApi, PtyApi, ServicesApi, SettingsApi, UiApi, WorkspaceApi } from "./api.ts"; import type { BackgroundApi } from "./backgrounds.ts"; import type { CompletionApi } from "./completion.ts"; /** * The optional members of `PluginApi`, by path, and the type that `requireFeature` returns for each path. * Older RuneStone hosts can omit these members. Required members, such as `workspace.read`, are not listed. */ export interface PluginFeatures { pty: PtyApi; files: FilesApi; services: ServicesApi; backgrounds: BackgroundApi; completion: CompletionApi; "completion.transport": NonNullable; "ui.showActionMenu": UiApi["showActionMenu"]; registerTabDrop: NonNullable; updateViewTitle: NonNullable; updateViewIcon: NonNullable; openFile: NonNullable; renameDocument: NonNullable; closeView: NonNullable; replaceView: NonNullable; replaceDocumentWithView: NonNullable; "workspace.listMarkdown": NonNullable; "workspace.readMany": NonNullable; "workspace.readBytes": NonNullable; "workspace.readOptional": NonNullable; "workspace.create": NonNullable; "workspace.subscribe": NonNullable; "settings.subscribe": NonNullable; } export type PluginFeature = keyof PluginFeatures; const descriptions: { [K in PluginFeature]: string } = { pty: "the terminal API", files: "the folder grant API", services: "the plugin service API", backgrounds: "the background API", completion: "the completion API", "completion.transport": "the completion model transport", "ui.showActionMenu": "the shared action menu", registerTabDrop: "tab drops from other windows", updateViewTitle: "view title changes", updateViewIcon: "view tab icons", openFile: "opening files in the RuneStone editor", renameDocument: "document renames", closeView: "closing views", replaceView: "view replacement", replaceDocumentWithView: "replacing a document tab with a view", "workspace.listMarkdown": "the Markdown file list", "workspace.readMany": "reading many workspace files in one request", "workspace.readBytes": "reading workspace files as bytes", "workspace.readOptional": "reading optional workspace files", "workspace.create": "creating workspace files", "workspace.subscribe": "workspace change notifications", "settings.subscribe": "settings change notifications", }; /** All the feature paths that `hasFeature` and `requireFeature` accept. */ export const pluginFeatures = Object.freeze(Object.keys(descriptions) as PluginFeature[]); /** A readable name of a feature, with its API path, for messages to users. */ export function describeFeature(feature: PluginFeature): string { if (!Object.hasOwn(descriptions, feature)) throw new TypeError(`Unknown plugin feature: ${String(feature)}`); return `${descriptions[feature]} (api.${feature})`; } /** The error that `requireFeature` and `requireFeatures` throw when the host does not have a feature. */ export class MissingFeatureError extends Error { readonly features: readonly PluginFeature[]; constructor(features: readonly PluginFeature[]) { const names = features.map(describeFeature); super(`This plugin needs ${names.length > 1 ? names.slice(0, -1).join(", ") + " and " + names.at(-1) : names[0]}. Update RuneStone to a version that has ${names.length > 1 ? "these features" : "this feature"}.`); this.name = "MissingFeatureError"; this.features = Object.freeze([...features]); } } function lookup(api: PluginApi, feature: PluginFeature): { parent: unknown; value: unknown } { describeFeature(feature); let parent: unknown; let value: unknown = api; for (const key of feature.split(".")) { parent = value; value = parent !== null && typeof parent === "object" ? (parent as Record)[key] : undefined; } return { parent, value }; } /** Returns `true` when the host gives this optional member of the plugin API. */ export function hasFeature(api: PluginApi, feature: PluginFeature): boolean { const { value } = lookup(api, feature); return typeof value === "function" || (value !== null && typeof value === "object"); } /** Returns the features in the list that the host does not give, in list order. */ export function missingFeatures(api: PluginApi, features: readonly PluginFeature[]): PluginFeature[] { return features.filter(feature => !hasFeature(api, feature)); } /** * Returns the member at the feature path, or throws a `MissingFeatureError` that tells the user to update RuneStone. * A returned function stays bound to its parent object. */ export function requireFeature(api: PluginApi, feature: F): PluginFeatures[F] { const { parent, value } = lookup(api, feature); if (typeof value === "function") return value.bind(parent) as PluginFeatures[F]; if (value !== null && typeof value === "object") return value as PluginFeatures[F]; throw new MissingFeatureError([feature]); } /** Throws one `MissingFeatureError` that names every missing feature in the list. */ export function requireFeatures(api: PluginApi, features: readonly PluginFeature[]): void { const missing = missingFeatures(api, features); if (missing.length) throw new MissingFeatureError(missing); }