Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(search): complete a value and an operator the parser will accept
Elian Dorana3cb3347ba8b
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.tsindex e61aa18ebe..f6584c4947 100644--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts@@ -51,6 +51,29 @@ describe("searchCompletionSource", () => {expect(result?.options.every((option) => option.detail)).toBe(true);});+ it("offers only the operators the operand accepts", async () => {+ // `note.text` is matched, never ordered or compared exactly.+ expect(labelsOf(await complete("note.text >"))).toEqual([ "*=*" ]);+ expect(labelsOf(await complete("~author.text ="))).toEqual([ "*=*" ]);++ // Content is matched too, but with every matching operator.+ const content = labelsOf(await complete("note.content ="));+ expect(content).toContain("%=");+ expect(content).not.toContain(">=");+ expect(labelsOf(await complete("note.rawContent ="))).toEqual(content);++ // A relation is compared only through a property of the note it names.+ expect(await complete("~author =")).toBeNull();+ expect(await complete("note.relations.author >")).toBeNull();+ // An explicit request past one is left with the keywords alone.+ expect(labelsOf(await complete("~author ", { explicit: true })))+ .toEqual([ "note", "and", "or", "not", "orderBy", "limit" ]);++ // The last segment decides, and a name the user chose after `labels.` restricts nothing.+ expect(labelsOf(await complete("note.parents.title >"))).toContain(">");+ expect(labelsOf(await complete("note.labels.text ="))).toContain("=");+ });+it("offers path segments after a dot, anchored at the segment being typed", async () => {const root = await complete("note.");@@ -146,6 +169,17 @@ describe("searchCompletionSource", () => {expect(optionFor(result, "science fiction")?.apply).toBe("\"science fiction\"");});+ it("quotes a value spelled like a reserved operand, whatever its case", async () => {+ vi.mocked(server.get).mockResolvedValue([ "note", "Today", "monthly" ]);++ const result = await complete("#genre = t");++ // Bare, `note` is rejected as a keyword and `today` resolves to a date.+ expect(optionFor(result, "note")?.apply).toBe("\"note\"");+ expect(optionFor(result, "Today")?.apply).toBe("\"Today\"");+ expect(optionFor(result, "monthly")?.apply).toBeUndefined();+ });+it("closes a quote the user opened rather than adding another", async () => {const result = await complete("#genre = \"science f");diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.tsindex e80204b0fe..dec6fb4d87 100644--- a/apps/client/src/widgets/ribbon/search_completions.ts+++ b/apps/client/src/widgets/ribbon/search_completions.ts@@ -40,7 +40,9 @@ export function searchCompletionSource(context: CompletionContext): CompletionOuconst operator = context.matchBefore(OPERATOR_PREFIX);if (operator) {- return { from: operator.from, options: operatorOptions(), validFor: OPERATOR_PREFIX };+ const options = operatorOptions(context);++ return options.length ? { from: operator.from, options, validFor: OPERATOR_PREFIX } : null;}// `asc` and `desc` order an `orderBy` key, so they are worth offering only once one is open.@@ -53,7 +55,7 @@ export function searchCompletionSource(context: CompletionContext): CompletionOu// Ctrl-Space on empty space asks for everything on offer.if (context.explicit) {- return { from: context.pos, options: [ ...wordOptions(isOrdering), ...operatorOptions() ] };+ return { from: context.pos, options: [ ...wordOptions(isOrdering), ...operatorOptions(context) ] };}return null;@@ -71,6 +73,11 @@ const SEGMENT_TYPED = new RegExp(`${SEGMENT}*`);const ORDER_BY_BEFORE = /orderby[^]*/i;/** A `#label` or `~relation`, either optionally negated with `!`. */const ATTRIBUTE_PREFIX = new RegExp(`[#~]!?${SEGMENT}*`);+/**+ * The attribute or property path an operator is being typed against. The operator characters are+ * matched along with it so the pattern ends at the cursor, and only the operand is captured.+ */+const COMPARISON_OPERAND = new RegExp(`((?:[#~]!?)?${SEGMENT}+(?:\\.${SEGMENT}+)*)\\s*[=!*<>%~]*`);/*** A label compared with an operator, and whatever stands between that operator and the cursor. The* tail stops at the characters that open another clause, so in a query holding several comparisons@@ -82,6 +89,12 @@ const QUOTES = [ "\"", "'", "`" ];const VALUE_TYPED = /[^\s#~()'"`]*/;/** What the lexer reads as structure, so a value holding one of these only survives in quotes. */const VALUE_NEEDS_QUOTES = /[\s"'`\\#~().=*<>!%+,-]/;+/**+ * Operands the parser reads as something other than the text they spell: `now`, `today`, `month`+ * and `year` resolve to a date, and `note` is rejected as a keyword. The lexer lowercases the+ * query, so the spelling does not matter.+ */+const RESERVED_VALUES = new Set([ "note", "now", "today", "month", "year" ]);/*** Fetches the label or relation names the database holds, through the same call the sidebar's@@ -185,14 +198,17 @@ async function valueCompletions(name: string, from: number, quote: string): Prom/*** The text a value is inserted as: closing the quote the user opened, or adding a pair of them- * around a value the lexer would otherwise split into several tokens.+ * around a value the parser would otherwise read as something else — one the lexer splits into+ * several tokens, or one spelled like a reserved operand.*/function applyValue(value: string, quote: string) {if (quote) {return escapeInQuotes(value, quote) + quote;}- return VALUE_NEEDS_QUOTES.test(value) ? `"${escapeInQuotes(value, "\"")}"` : value;+ const needsQuotes = VALUE_NEEDS_QUOTES.test(value) || RESERVED_VALUES.has(value.toLowerCase());++ return needsQuotes ? `"${escapeInQuotes(value, "\"")}"` : value;}/** The lexer reads the character after a backslash literally, the quote and the backslash included. */@@ -293,7 +309,10 @@ const SEGMENT_DETAILS: Record<string, string> = {revisionCount: "order_by.revision_count"};-function operatorOptions(): Completion[] {+/** The operators the operand standing before the cursor can be compared with. */+function operatorOptions(context: CompletionContext): Completion[] {+ const allowed = allowedOperators(context);+return [{ label: "=", detail: t("search_completion.operator_equal") },{ label: "!=", detail: t("search_completion.operator_not_equal") },@@ -307,5 +326,48 @@ function operatorOptions(): Completion[] {{ label: "%=", detail: t("search_completion.operator_regex") },{ label: "~=", detail: t("search_completion.operator_fuzzy_equal") },{ label: "~*", detail: t("search_completion.operator_fuzzy_contains") }- ].map((option) => ({ ...option, type: "keyword" }));+ ]+ .filter(({ label }) => !allowed || allowed.has(label))+ .map((option) => ({ ...option, type: "keyword" }));}++/**+ * What the operand restricts the comparison to, `undefined` standing for no restriction. Offering+ * more would build a query the parser rejects.+ */+function allowedOperators(context: CompletionContext): ReadonlySet<string> | undefined {+ const comparison = context.matchBefore(COMPARISON_OPERAND);+ const operand = comparison && COMPARISON_OPERAND.exec(comparison.text)?.[1];+ if (!operand) {+ return undefined;+ }++ const segments = operand.split(".");+ const property = (segments[segments.length - 1] ?? "").toLowerCase();+ const previous = (segments[segments.length - 2] ?? "").toLowerCase();++ // The name after `labels.` is the user's own, and says nothing about the comparison.+ if (previous === "labels") {+ return undefined;+ }++ // A relation names a note; only a `.`, walking into a property of that note, continues it.+ if (previous === "relations" || (operand.startsWith("~") && segments.length === 1)) {+ return NO_OPERATORS;+ }++ if (property === "text") {+ return TEXT_OPERATORS;+ }++ if (property === "content" || property === "rawcontent") {+ return CONTENT_OPERATORS;+ }++ return undefined;+}++const NO_OPERATORS: ReadonlySet<string> = new Set();+const TEXT_OPERATORS: ReadonlySet<string> = new Set([ "*=*" ]);+/** Matches `ALLOWED_OPERATORS` in `note_content_fulltext.ts`: content is matched, never ordered. */+const CONTENT_OPERATORS: ReadonlySet<string> = new Set([ "=", "!=", "*=*", "*=", "=*", "%=", "~=", "~*" ]);