chore: remove empty bracked matching. We dont serve suggetions for empty brackets anyways

This commit is contained in:
Daniel Bauer
2024-12-13 11:14:55 +01:00
parent ec97c3a968
commit 0be91e7759
2 changed files with 10 additions and 56 deletions

View File

@@ -2,25 +2,19 @@ import { getTriggerText } from "./trigger";
describe("trigger", () => {
describe("empty", () => {
test("empty parantheses", () => {
expect(getTriggerText("() testing", 1)).toEqual(["", 1, 1]);
test("ignore empty parantheses", () => {
expect(getTriggerText("() testing", 1)).toEqual(null);
});
test("empty square brackets", () => {
expect(getTriggerText("[] testing", 1)).toEqual(["", 1, 1]);
test("ignore empty square brackets", () => {
expect(getTriggerText("[] testing", 1)).toEqual(null);
});
test("empty square brackets in text", () => {
expect(getTriggerText("test [] testing", 6)).toEqual(["", 6, 6]);
test("ignore empty square brackets in text", () => {
expect(getTriggerText("test [] testing", 6)).toEqual(null);
});
// test("not match empty double square brackets", () => {
// Obsidian overrides these matches anyways
// test("ignore empty double square brackets", () => {
// expect(getTriggerText("[[]] testing", 2)).toEqual(null)
// })
test("multiple empty brackets in text", () => {
expect(getTriggerText("test [] and [] testing", 13)).toEqual([
"",
13,
13,
]);
});
// });
});
describe("text in parantheses", () => {

View File

@@ -1,14 +1,3 @@
/**
* Matches single square bracket pair [] or parahtheses (), but not a double square bracket pair [[]]
* (?<!\[) # Negative lookbehind to make sure there is no second [
* \[] # Matches []
* (?![\]\(]) # Negative lookahead to make sure there is no second closing ] from wiki link or opening ( from a markdown link
* | # OR
* (?<!\]) # Negative lookbehind to make sure there is no closing ] from a markdown link
* \(\) # Matches ()
*/
const emptyRegex = /(?<!\[)\[](?![\]\(])|(?<!\])\(\)/g;
/**
* Maches single square brackets or parantheses with some text in them
* and captures the text
@@ -38,42 +27,13 @@ export function getTriggerText(
line: string,
cursorPos: number,
): [string, number, number] | null {
// Check for empty [] or ().
// If the user starts typing with no text in the field, this is the fastest way to find it.
let match = getEmptyTrigger(line, cursorPos);
if (match) {
return match;
}
// If the user types inside an existing field, this is the function to find it.
match = getTriggerTextFromRegex(line, cursorPos, filledRegex);
let match = getTriggerTextFromRegex(line, cursorPos, filledRegex);
if (match !== null) {
return match;
}
return null;
}
/**
* Matches an empty [] or empty (), but not [[]]
* Has its own function since there is no capture group resulting in a different index calculation
*/
function getEmptyTrigger(
line: string,
cursorPos: number,
): [string, number, number] | null {
let matches = Array.from(line.matchAll(emptyRegex));
for (const match of matches) {
if (match.index === undefined) {
continue;
}
if (cursorPos === match.index + 1) {
return ["", match.index + 1, match.index + 1];
}
}
return null;
}
/**
* Given a regex with a capture group, a line of text, and the users cursor position,
* this function finds the match of the regex in the line that the user is currently inside.