2022-04-25 08:33:38 +00:00
|
|
|
import { Hook, Manifest } from "@plugos/plugos/types";
|
|
|
|
import { System } from "@plugos/plugos/system";
|
|
|
|
import {
|
|
|
|
Completion,
|
|
|
|
CompletionContext,
|
|
|
|
CompletionResult,
|
|
|
|
} from "@codemirror/autocomplete";
|
|
|
|
import { safeRun } from "../../common/util";
|
2022-03-29 09:21:32 +00:00
|
|
|
import { Editor } from "../editor";
|
2022-07-04 13:07:27 +00:00
|
|
|
import { syntaxTree } from "@codemirror/language";
|
2022-03-29 09:21:32 +00:00
|
|
|
|
|
|
|
export type SlashCommandDef = {
|
|
|
|
name: string;
|
2022-07-04 13:07:27 +00:00
|
|
|
description?: string;
|
2022-03-29 09:21:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
export type AppSlashCommand = {
|
|
|
|
slashCommand: SlashCommandDef;
|
|
|
|
run: () => Promise<void>;
|
|
|
|
};
|
|
|
|
|
|
|
|
export type SlashCommandHookT = {
|
|
|
|
slashCommand?: SlashCommandDef;
|
|
|
|
};
|
|
|
|
|
2022-08-02 11:22:10 +00:00
|
|
|
const slashCommandRegexp = /([^\w]|^)\/[\w\-]*/;
|
|
|
|
|
2022-03-29 09:21:32 +00:00
|
|
|
export class SlashCommandHook implements Hook<SlashCommandHookT> {
|
|
|
|
slashCommands = new Map<string, AppSlashCommand>();
|
|
|
|
private editor: Editor;
|
|
|
|
|
|
|
|
constructor(editor: Editor) {
|
|
|
|
this.editor = editor;
|
|
|
|
}
|
|
|
|
|
|
|
|
buildAllCommands(system: System<SlashCommandHookT>) {
|
|
|
|
this.slashCommands.clear();
|
|
|
|
for (let 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: () => {
|
2022-07-04 13:07:27 +00:00
|
|
|
return plug.invoke(name, [cmd]);
|
2022-03-29 09:21:32 +00:00
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Completer for CodeMirror
|
|
|
|
public slashCommandCompleter(
|
|
|
|
ctx: CompletionContext
|
|
|
|
): CompletionResult | null {
|
|
|
|
let prefix = ctx.matchBefore(slashCommandRegexp);
|
|
|
|
if (!prefix) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-08-02 11:22:10 +00:00
|
|
|
console.log("Match", prefix);
|
2022-03-29 09:21:32 +00:00
|
|
|
let options: Completion[] = [];
|
2022-07-04 13:07:27 +00:00
|
|
|
|
|
|
|
// No slash commands in comment blocks (queries and such)
|
|
|
|
let currentNode = syntaxTree(ctx.state).resolveInner(ctx.pos);
|
|
|
|
if (currentNode.type.name === "CommentBlock") {
|
|
|
|
return null;
|
|
|
|
}
|
2022-03-29 09:21:32 +00:00
|
|
|
for (let [name, def] of this.slashCommands.entries()) {
|
|
|
|
options.push({
|
|
|
|
label: def.slashCommand.name,
|
2022-07-04 13:07:27 +00:00
|
|
|
detail: def.slashCommand.description,
|
2022-03-29 09:21:32 +00:00
|
|
|
apply: () => {
|
|
|
|
// Delete slash command part
|
|
|
|
this.editor.editorView?.dispatch({
|
|
|
|
changes: {
|
|
|
|
from: prefix!.from,
|
|
|
|
to: ctx.pos,
|
|
|
|
insert: "",
|
|
|
|
},
|
|
|
|
});
|
|
|
|
// Replace with whatever the completion is
|
|
|
|
safeRun(async () => {
|
|
|
|
await def.run();
|
2022-07-04 13:07:27 +00:00
|
|
|
this.editor.focus();
|
2022-03-29 09:21:32 +00:00
|
|
|
});
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
// + 1 because of the '/'
|
2022-08-02 11:22:10 +00:00
|
|
|
from: prefix.from + prefix.text.indexOf("/") + 1,
|
2022-03-29 09:21:32 +00:00
|
|
|
options: options,
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
apply(system: System<SlashCommandHookT>): void {
|
|
|
|
this.buildAllCommands(system);
|
|
|
|
system.on({
|
|
|
|
plugLoaded: () => {
|
|
|
|
this.buildAllCommands(system);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
validateManifest(manifest: Manifest<SlashCommandHookT>): string[] {
|
|
|
|
let 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 [];
|
|
|
|
}
|
|
|
|
}
|