mirror of
https://github.com/dnlbauer/obsidian-dataview-autocompletion.git
synced 2026-09-10 22:25:29 +00:00
feat: add some trigger tests
This commit is contained in:
@@ -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[]> {
|
||||
|
||||
@@ -1,5 +0,0 @@
|
||||
describe("testing setup", () => {
|
||||
test("testing setup", () => {
|
||||
expect(1).toBe(1)
|
||||
})
|
||||
})
|
||||
19
src/trigger.test.ts
Normal file
19
src/trigger.test.ts
Normal file
@@ -0,0 +1,19 @@
|
||||
import { getTriggerText } from "./trigger"
|
||||
|
||||
describe("test trigger", () => {
|
||||
test("testing trigger regex on braces", () => {
|
||||
expect(getTriggerText("test string () testing", 13)).toEqual(["", 13, 13])
|
||||
expect(getTriggerText("test string (person) testing", 16)).toEqual(["person", 13, 19])
|
||||
expect(getTriggerText("test string (person:) testing", 16)).toEqual(["person:", 13, 20])
|
||||
expect(getTriggerText("test string (person::) testing", 16)).toEqual(["person::", 13, 21])
|
||||
expect(getTriggerText("test string (person::Bob) testing", 16)).toEqual(["person::Bob", 13, 24])
|
||||
expect(getTriggerText("test string (person:: Bob) testing", 16)).toEqual(["person:: Bob", 13, 25])
|
||||
expect(getTriggerText("test string (person:: Bob) testing", 16)).toEqual(["person:: Bob", 13, 27])
|
||||
})
|
||||
|
||||
test("test multiple regex matches in trigger", () => {
|
||||
expect(getTriggerText("test() string () testing", 5)).toEqual(["", 5, 5])
|
||||
expect(getTriggerText("test() string () testing", 15)).toEqual(["", 15, 15])
|
||||
expect(getTriggerText("test() string () testing", 17)).toEqual(null)
|
||||
})
|
||||
})
|
||||
17
src/trigger.ts
Normal file
17
src/trigger.ts
Normal file
@@ -0,0 +1,17 @@
|
||||
const triggerRegex = /\((.*?)\)/g
|
||||
|
||||
export function getTriggerText(line: string, cursorPos: number): [string, number, number] | null {
|
||||
triggerRegex.lastIndex = 0 // resetting global regex
|
||||
|
||||
let match = null
|
||||
while ((match = triggerRegex.exec(line)) !== null) {
|
||||
const cursorInMatch = cursorPos > match.index && cursorPos <= match.index + match[1].length + 1
|
||||
|
||||
if (cursorInMatch) {
|
||||
return [match[1], match.index+1, match.index + match[1].length + 1]
|
||||
}
|
||||
|
||||
}
|
||||
return null
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user