feat: add some trigger tests

This commit is contained in:
Daniel Bauer
2024-12-11 00:36:43 +01:00
parent f454ce9db7
commit 6c0cf3d2f8
4 changed files with 46 additions and 22 deletions

View File

@@ -1,4 +1,5 @@
import { App, Editor, Plugin, EditorPosition, EditorSuggest, EditorSuggestContext, EditorSuggestTriggerInfo, TFile } from 'obsidian';
import { getTriggerText } from './trigger';
export class DataviewSuggester extends EditorSuggest<String> {
@@ -12,24 +13,16 @@ export class DataviewSuggester extends EditorSuggest<String> {
file: TFile,
): EditorSuggestTriggerInfo | null {
const line = editor.getLine(cursor.line);
const regex = /\((.*?)\)/g // todo min characters for match? -> research on best practices
let match
while((match = regex.exec(line)) !== null) {
// check if cursor is positioned inside the match
const cursorInMatch = cursor.ch > match.index && cursor.ch <= match.index + match[1].length + 1
if (cursorInMatch) {
return {
start: { line: cursor.line, ch: match.index+1 },
end: { line: cursor.line, ch: match.index + match[1].length+1 },
query: match[1],
};
let trigger = getTriggerText(line, cursor.ch)
if (trigger !== null) {
return {
query: trigger[0],
start: { line: cursor.line, ch: trigger[1] },
end: { line: cursor.line, ch: trigger[2] },
}
}
return null;
}
return null
}
getSuggestions(context: EditorSuggestContext): string[] | Promise<string[]> {