From f108e2690f32ea169c567cc819233856a5dc3e65 Mon Sep 17 00:00:00 2001 From: Simon Cambier Date: Sun, 19 Jun 2022 21:36:20 +0200 Subject: [PATCH] EditiorSuggest: extracting query from [@query] --- src/suggestions.ts | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/src/suggestions.ts b/src/suggestions.ts index c59d800..e9485d3 100644 --- a/src/suggestions.ts +++ b/src/suggestions.ts @@ -1,3 +1,5 @@ +// Parts of this code come from https://github.com/valentine195/obsidian-admonition/blob/e4aa52fe04bb68a5483421ef98414c2617d666b7/src/suggest/suggest.ts + import { Editor, EditorSuggest, @@ -13,15 +15,27 @@ export class OmnisearchSuggest extends EditorSuggest { editor: Editor, file: TFile, ): EditorSuggestTriggerInfo | null { - const last2Chars = editor.getLine(cursor.line).slice(-2, cursor.ch) - if (last2Chars === '@@') { - return { - start: cursor, - end: cursor, - query: 'foo', - } + const line = editor.getLine(cursor.line) + // not inside the bracket + if (/\[@.+\]/.test(line.slice(0, cursor.ch))) return null + if (!/\[@.*/.test(line)) return null + + const match = line.match(/\[@([^\]]*)\]?/) // [@(foo bar)] baz + if (!match) return null + + const [_, query] = match + if (!query) { + return null } - return null + const matchData = { + end: cursor, + start: { + ch: (match.index ?? 0) + 4, + line: cursor.line, + }, + query, + } + return matchData } getSuggestions(context: EditorSuggestContext): string[] | Promise {