mirror of
https://github.com/fbarrca/obsidian-inlineAI.git
synced 2026-07-22 11:50:24 +00:00
Add the ability to set a custom command prefix
This commit is contained in:
parent
881e86c89b
commit
b8df88c06f
2 changed files with 28 additions and 18 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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)]
|
||||
})
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue