2022-03-29 10:13:46 +00:00
|
|
|
import { Hook, Manifest } from "../../plugos/types";
|
|
|
|
import { System } from "../../plugos/system";
|
|
|
|
import { CompletionResult } from "@codemirror/autocomplete";
|
|
|
|
|
|
|
|
export type CompleterHookT = {
|
|
|
|
isCompleter?: boolean;
|
|
|
|
};
|
|
|
|
|
|
|
|
export class CompleterHook implements Hook<CompleterHookT> {
|
|
|
|
private system?: System<CompleterHookT>;
|
|
|
|
|
|
|
|
public async plugCompleter(): Promise<CompletionResult | null> {
|
|
|
|
let completerPromises = [];
|
|
|
|
// TODO: Can be optimized (cache all functions)
|
|
|
|
for (const plug of this.system!.loadedPlugs.values()) {
|
|
|
|
if (!plug.manifest) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
for (const [functionName, functionDef] of Object.entries(
|
2022-04-01 13:02:35 +00:00
|
|
|
plug.manifest.functions
|
2022-03-29 10:13:46 +00:00
|
|
|
)) {
|
|
|
|
if (functionDef.isCompleter) {
|
|
|
|
completerPromises.push(plug.invoke(functionName, []));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-04-01 13:02:35 +00:00
|
|
|
let actualResult = null;
|
|
|
|
for (const result of await Promise.all(completerPromises)) {
|
|
|
|
if (result) {
|
|
|
|
if (actualResult) {
|
|
|
|
console.error(
|
|
|
|
"Got completion results from multiple sources, cannot deal with that"
|
|
|
|
);
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
actualResult = result;
|
|
|
|
}
|
2022-03-29 10:13:46 +00:00
|
|
|
}
|
2022-04-01 13:02:35 +00:00
|
|
|
return actualResult;
|
2022-03-29 10:13:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
apply(system: System<CompleterHookT>): void {
|
|
|
|
this.system = system;
|
|
|
|
}
|
|
|
|
|
|
|
|
validateManifest(manifest: Manifest<CompleterHookT>): string[] {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
}
|