1
0
silverbullet/web/hooks/slash_command.ts

123 lines
3.2 KiB
TypeScript
Raw Normal View History

import { Hook, Manifest } from "../../plugos/types.ts";
import { System } from "../../plugos/system.ts";
2022-10-12 09:47:13 +00:00
import { Completion, CompletionContext, CompletionResult } from "../deps.ts";
import { safeRun } from "../../common/util.ts";
2023-07-14 14:56:20 +00:00
import { Client } from "../client.ts";
import { syntaxTree } from "../deps.ts";
export type SlashCommandDef = {
name: string;
description?: string;
boost?: number;
};
export type AppSlashCommand = {
slashCommand: SlashCommandDef;
run: () => Promise<void>;
};
export type SlashCommandHookT = {
slashCommand?: SlashCommandDef;
};
const slashCommandRegexp = /([^\w:]|^)\/[\w\-]*/;
export class SlashCommandHook implements Hook<SlashCommandHookT> {
slashCommands = new Map<string, AppSlashCommand>();
2023-07-14 14:56:20 +00:00
private editor: Client;
2023-07-14 14:56:20 +00:00
constructor(editor: Client) {
this.editor = editor;
}
buildAllCommands(system: System<SlashCommandHookT>) {
this.slashCommands.clear();
2022-10-15 17:02:56 +00:00
for (const plug of system.loadedPlugs.values()) {
for (
const [name, functionDef] of Object.entries(
plug.manifest!.functions,
)
) {
if (!functionDef.slashCommand) {
continue;
}
const cmd = functionDef.slashCommand;
this.slashCommands.set(cmd.name, {
slashCommand: cmd,
run: () => {
return plug.invoke(name, [cmd]);
},
});
}
}
}
// Completer for CodeMirror
public slashCommandCompleter(
ctx: CompletionContext,
): CompletionResult | null {
2022-10-15 17:02:56 +00:00
const prefix = ctx.matchBefore(slashCommandRegexp);
if (!prefix) {
return null;
}
const prefixText = prefix.text;
2022-10-15 17:02:56 +00:00
const options: Completion[] = [];
// No slash commands in comment blocks (queries and such)
2022-10-15 17:02:56 +00:00
const currentNode = syntaxTree(ctx.state).resolveInner(ctx.pos);
if (currentNode.type.name === "CommentBlock") {
return null;
}
2022-10-15 17:02:56 +00:00
for (const def of this.slashCommands.values()) {
options.push({
label: def.slashCommand.name,
detail: def.slashCommand.description,
boost: def.slashCommand.boost,
apply: () => {
// Delete slash command part
2023-07-27 09:41:44 +00:00
this.editor.editorView.dispatch({
changes: {
from: prefix!.from + prefixText.indexOf("/"),
to: ctx.pos,
insert: "",
},
});
// Replace with whatever the completion is
safeRun(async () => {
await def.run();
this.editor.focus();
});
},
});
}
return {
// + 1 because of the '/'
from: prefix.from + prefixText.indexOf("/") + 1,
options: options,
};
}
apply(system: System<SlashCommandHookT>): void {
this.buildAllCommands(system);
system.on({
plugLoaded: () => {
this.buildAllCommands(system);
},
});
}
validateManifest(manifest: Manifest<SlashCommandHookT>): string[] {
2022-10-15 17:02:56 +00:00
const errors = [];
for (const [name, functionDef] of Object.entries(manifest.functions)) {
if (!functionDef.slashCommand) {
continue;
}
const cmd = functionDef.slashCommand;
if (!cmd.name) {
errors.push(`Function ${name} has a command but no name`);
}
}
return [];
}
}