diff --git a/src/modules/WidgetExtension.ts b/src/modules/WidgetExtension.ts index 50a36ad..27647d0 100644 --- a/src/modules/WidgetExtension.ts +++ b/src/modules/WidgetExtension.ts @@ -21,6 +21,7 @@ import { ChatApiManager } from "../api"; import { currentSelectionState, SelectionInfo } from "./SelectionState"; import { slashCommandAutocompletion } from "./commands/source"; +import { slashCommands } from "./commands/commands"; @@ -150,7 +151,10 @@ class FloatingWidget extends WidgetType { }, ]), // 3) Enable slash-command autocompletion - slashCommandAutocompletion + slashCommandAutocompletion({ + prefix: ':', + customCommands: slashCommands + }) ], }), parent: editorDom, diff --git a/src/modules/commands/source.ts b/src/modules/commands/source.ts index 2555a3b..3ada37a 100644 --- a/src/modules/commands/source.ts +++ b/src/modules/commands/source.ts @@ -1,25 +1,31 @@ import { autocompletion, CompletionContext } from "@codemirror/autocomplete" -import { EditorView } from "@codemirror/view" - -import { slashCommands } from "./commands" -function slashCommandCompletionSource(context: CompletionContext) { - let word = context.matchBefore(/^\/\w*/) - if (!word || (word.from == word.to && !context.explicit)) - return null - return { - from: word.from+1, - options: slashCommands.map(cmd => ({ - label: cmd.label, - type: "text" - })) +// Factory function that creates a completion source with custom parameters +function createSlashCommandSource(options: { + prefix?: string, + customCommands: Array<{ label: string, type: string }> +} = { + prefix: '/', + customCommands: [] +}) { + const { prefix, customCommands } = options; + console.log(prefix, customCommands) + return (context: CompletionContext) => { + let word = context.matchBefore(new RegExp(`^\\${prefix}\\w*`)) + if (!word || (word.from == word.to && !context.explicit)) + return null + return { + from: word.from+1, + options: (customCommands).map(cmd => ({ + label: cmd.label, + type: "text" + })) + } } } - // Create the extension that uses our custom completion source. -export const slashCommandAutocompletion = autocompletion({ - override: [slashCommandCompletionSource] +export const slashCommandAutocompletion = (options = { customCommands: [] }) => autocompletion({ + override: [createSlashCommandSource(options)] }) -