Add completion, browser integration, and workspace improvements

This commit is contained in:
Knotty Wyvern
2026-10-07 11:25:17 -07:00
parent dd9fc64676
commit a8b1defb9d
86 changed files with 3524 additions and 203 deletions
+49 -9
View File
@@ -1,4 +1,6 @@
import { pluginHost, type ContributionRegistry } from "./registry.ts";
import { completionHub } from "../completion/hub.ts";
import { registerCompletionInput } from "../completion/input.ts";
import type { Dispose, PluginApi, PluginCommand, PluginModule, PluginView, PluginDocumentType, PluginDocumentContext } from "./sdk.ts";
export function deadline<T>(work: Promise<T>, milliseconds = 5000): Promise<T> {
@@ -21,14 +23,17 @@ export class ExternalSession {
private contributions: Dispose[] = [];
private registry: ContributionRegistry;
readonly id: string;
private services: Pick<PluginApi, "workspace" | "settings" | "assets">;
private services: Pick<PluginApi, "workspace" | "settings" | "assets"> & { completionTransport?: NonNullable<PluginApi["completion"]>["transport"] };
private changed: () => void;
private show: (id: string) => void;
private show: PluginApi["openView"];
private close?: PluginApi["closeView"];
private receiveTab?: (data: string) => void | Promise<void>;
private openDocument: (path: string) => Promise<void>;
private renameDocument?: PluginApi["renameDocument"];
constructor(id: string, services: Pick<PluginApi, "workspace" | "settings" | "assets">, changed: () => void, show: (id: string) => void, registry: ContributionRegistry = pluginHost, openDocument: (path: string) => Promise<void> = async () => { throw new Error("Document views are unavailable."); }, renameDocument?: PluginApi["renameDocument"]) {
constructor(id: string, services: Pick<PluginApi, "workspace" | "settings" | "assets"> & { completionTransport?: NonNullable<PluginApi["completion"]>["transport"] }, changed: () => void, show: PluginApi["openView"], registry: ContributionRegistry = pluginHost, openDocument: (path: string) => Promise<void> = async () => { throw new Error("Document views are unavailable."); }, renameDocument?: PluginApi["renameDocument"], close?: PluginApi["closeView"]) {
this.openDocument = openDocument;
this.renameDocument = renameDocument;
this.close = close;
this.registry = registry; this.id = id; this.services = services; this.changed = changed; this.show = show;
}
private check() { if (this.stopped) throw new Error("This plugin is disabled."); }
@@ -40,9 +45,30 @@ export class ExternalSession {
};
const api: PluginApi = {
id: this.id, signal: this.controller.signal,
completion: {
version: 1,
registerProvider: provider => { this.check(); const off = completionHub.registerProvider({ ...provider, id: this.id + ":" + provider.id }); this.contributions.push(off); return off; },
registerSurface: surface => { this.check(); const handle = completionHub.registerSurface({ ...surface, id: this.id + ":" + surface.id }); this.contributions.push(handle.dispose); return handle; },
registerInput: (element, options) => { this.check(); const handle = registerCompletionInput(element, { ...options, id: this.id + ":" + options.id }); this.contributions.push(handle.dispose); return handle; },
invalidate: () => { this.check(); completionHub.invalidate(); },
...(this.services.completionTransport ? { transport: (request, signal, onLine) => {
this.check(); return this.services.completionTransport!(request, AbortSignal.any([signal, this.controller.signal]), onLine);
} } : {}),
},
contribute: (key, value) => { this.check(); this.contributions.push(this.registry.register(this.id, key, value)); },
registerCommand: item => { if (typeof item.run !== "function") throw new Error("A command needs a run function."); register(this.commands, item); },
registerView: item => { if (item.placement !== undefined && !["dialog", "editor"].includes(item.placement)) throw new Error("Unknown plugin view placement."); if (typeof item.mount !== "function") throw new Error("A view needs a mount function."); register(this.views, item); },
registerCommand: item => { if (item.category !== undefined && !["open", "action"].includes(item.category)) throw new Error("Unknown plugin command category."); if (typeof item.run !== "function") throw new Error("A command needs a run function."); register(this.commands, item); },
registerView: item => { if ([item.settings, item.launcher].some(value => value !== undefined && typeof value !== "boolean")) throw new Error("Plugin view access options must be boolean."); if (item.placement !== undefined && !["dialog", "editor"].includes(item.placement)) throw new Error("Unknown plugin view placement."); if (typeof item.mount !== "function") throw new Error("A view needs a mount function."); register(this.views, item); },
registerTabDrop: receive => { this.check(); if (typeof receive !== "function" || this.receiveTab) throw new Error("Invalid or duplicate tab receiver."); this.receiveTab = receive; },
updateViewTitle: (id, title) => {
this.check(); const view = this.views.get(id);
if (!view || typeof title !== "string" || !title.trim() || title.length > 200) throw new Error("Invalid view title.");
if (view.title !== title) { view.title = title; this.changed(); }
},
updateViewIcon: (id, image) => {
this.check(); const view = this.views.get(id);
if (!view || (image !== null && (typeof image !== "string" || image.length > 90_000 || !/^data:image\/(?:png|jpeg|gif|webp|x-icon|vnd\.microsoft\.icon|svg\+xml);base64,[A-Za-z0-9+/]+={0,2}$/.test(image)))) throw new Error("Invalid view icon.");
if (view.iconImage !== image) { view.iconImage = image; this.changed(); }
},
registerDocumentType: item => {
if (item.content !== undefined && !["text", "binary"].includes(item.content)) throw new Error("Invalid document content type.");
if (typeof item.identify !== "function" || typeof item.mount !== "function" || !Array.isArray(item.extensions) || !item.extensions.length || item.extensions.some(ext => !/^[a-z0-9]+$/.test(ext)) || (item.icon && !["kanban", "file"].includes(item.icon))) throw new Error("Invalid document type.");
@@ -51,7 +77,8 @@ export class ExternalSession {
},
openDocument: path => { this.check(); return this.openDocument(path); },
...(this.renameDocument ? { renameDocument: (document: PluginDocumentContext, name: string) => { this.check(); return this.renameDocument!(document, name); } } : {}),
openView: id => { this.check(); if (!this.views.has(id)) throw new Error("Unknown plugin view."); this.show(id); },
openView: (id, options) => { this.check(); if (!this.views.has(id)) throw new Error("Unknown plugin view."); this.show(id, options); },
...(this.close ? { closeView: (id: string) => { this.check(); this.close!(id); } } : {}),
onDispose: cleanup => { this.check(); if (typeof cleanup !== "function") throw new Error("Cleanup must be a function."); this.cleanups.push(cleanup); },
workspace: {
...(this.services.workspace.subscribe ? { subscribe: (listener: () => void) => {
@@ -65,6 +92,7 @@ export class ExternalSession {
this.cleanups.push(cleanup); return cleanup;
} } : {}),
list: path => { this.check(); return this.services.workspace.list(path); },
...(this.services.workspace.listMarkdown ? { listMarkdown: () => { this.check(); return this.services.workspace.listMarkdown!(); } } : {}),
read: path => { this.check(); return this.services.workspace.read(path); },
...(this.services.workspace.readBytes ? { readBytes: (path: string) => { this.check(); return this.services.workspace.readBytes!(path); } } : {}),
...(this.services.workspace.readOptional ? { readOptional: (path: string) => { this.check(); return this.services.workspace.readOptional!(path); } } : {}),
@@ -72,6 +100,13 @@ export class ExternalSession {
write: (path, value, expected) => { this.check(); if (typeof expected !== "string") throw new Error("Supply the original file text before writing."); return this.services.workspace.write(path, value, expected); },
},
settings: {
...(this.services.settings.subscribe ? { subscribe: (listener: () => void) => {
this.check();
const off = this.services.settings.subscribe!(() => { if (!this.stopped) listener(); });
let active = true;
const cleanup: Dispose = () => { if (!active) return; active = false; this.cleanups = this.cleanups.filter(item => item !== cleanup); return off(); };
this.cleanups.push(cleanup); return cleanup;
} } : {}),
read: () => { this.check(); return this.services.settings.read(); },
write: value => { this.check(); return this.services.settings.write(value); },
},
@@ -88,10 +123,15 @@ export class ExternalSession {
await deadline(this.activation);
}
async run(id: string) { this.check(); const command = this.commands.get(id); if (!command) throw new Error("Unknown plugin command."); await command.run(); }
async mount(id: string, element: HTMLElement, document?: PluginDocumentContext): Promise<Dispose> {
async dropTab(data: string) {
this.check();
if (!this.receiveTab) throw new Error("This plugin cannot receive tabs.");
await this.receiveTab(data);
}
async mount(id: string, element: HTMLElement, document?: PluginDocumentContext, surface: "settings" | "view" = "view"): Promise<Dispose> {
this.check(); const view = document ? this.documentTypes.get(id) : this.views.get(id); if (!view) throw new Error("Unknown plugin view.");
let cleaning: Promise<void> | undefined;
const pending = Promise.resolve().then(() => { this.check(); return document ? (view as PluginDocumentType).mount(element, document) : (view as PluginView).mount(element); });
const pending = Promise.resolve().then(() => { this.check(); return document ? (view as PluginDocumentType).mount(element, document) : (view as PluginView).mount(element, { surface }); });
const cleanup = () => {
cleaning ??= (async () => {
const dispose = await pending;
@@ -107,7 +147,7 @@ export class ExternalSession {
}
stop(): Promise<void> {
if (this.stopping) return this.stopping;
this.stopped = true; this.controller.abort(); this.commands.clear(); this.views.clear(); this.documentTypes.clear(); this.changed();
this.stopped = true; this.controller.abort(); this.commands.clear(); this.views.clear(); this.documentTypes.clear(); this.receiveTab = undefined; this.changed();
for (const remove of this.contributions.splice(0)) remove();
this.stopping = (async () => {
const errors: unknown[] = [];