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
feat(search): complete the property path after note. and a relation
Elian Doran1936f21cec47
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.jsonindex a18681f7f3..5d8821cee8 100644--- a/apps/client/src/translations/en/translation.json+++ b/apps/client/src/translations/en/translation.json@@ -4314,6 +4314,20 @@"operator_less_or_equal": "Less than or equal to","operator_regex": "Matches a regular expression","operator_fuzzy_equal": "Equals, tolerating typos",- "operator_fuzzy_contains": "Contains, tolerating typos"+ "operator_fuzzy_contains": "Contains, tolerating typos",+ "property_content": "The note's body text",+ "property_raw_content": "The note's body text, before its formatting is stripped",+ "property_text": "The title or the body text",+ "property_parents": "Continues on the note's parents",+ "property_children": "Continues on the note's children",+ "property_ancestors": "Continues on the note's ancestors",+ "property_labels": "Followed by a label name",+ "property_relations": "Followed by a relation name, then that note's own properties",+ "property_attribute_count": "Number of labels and relations, including inherited ones",+ "property_label_count": "Number of labels, including inherited ones",+ "property_relation_count": "Number of relations, including inherited ones",+ "property_relation_count_including_links": "Number of relations, counting the ones made by links",+ "property_owned_relation_count_including_links": "Number of the note's own relations, counting the ones made by links",+ "property_target_relation_count_including_links": "Number of relations targeting the note, counting the ones made by links"}}diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.tsindex b0a21b8050..d67d5ca0a0 100644--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts@@ -1,4 +1,4 @@-import type { CompletionContext } from "@triliumnext/codemirror/src/single_line";+import type { CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/single_line";import { describe, expect, it, vi } from "vitest";import { searchCompletionSource } from "./search_completions";@@ -27,6 +27,35 @@ describe("searchCompletionSource", () => {expect(result?.options.every((option) => option.detail)).toBe(true);});+ it("offers path segments after a dot, anchored at the segment being typed", () => {+ const root = searchCompletionSource(contextAt("note."));++ expect(root?.from).toBe(5);+ expect(labelsOf(root)).toContain("title");+ expect(labelsOf(root)).toContain("parents");+ expect(labelsOf(root)).toContain("content");++ const partial = searchCompletionSource(contextAt("#book AND note.date"));++ expect(partial?.from).toBe(15);+ expect(labelsOf(partial)).toEqual(labelsOf(root));+ });++ it("walks a path through a relation and through a traversal", () => {+ expect(labelsOf(searchCompletionSource(contextAt("~author.")))).toContain("title");+ expect(labelsOf(searchCompletionSource(contextAt("note.parents.")))).toContain("title");+ expect(labelsOf(searchCompletionSource(contextAt("~author.relations.son.")))).toContain("title");+ });++ it("stops where the grammar expects a name or ends the path", () => {+ // Both take an attribute name, which nothing can enumerate ahead of time.+ expect(searchCompletionSource(contextAt("note.labels."))).toBeNull();+ expect(searchCompletionSource(contextAt("note.relations."))).toBeNull();+ // A terminal property has nothing to walk onto.+ expect(searchCompletionSource(contextAt("note.title."))).toBeNull();+ expect(searchCompletionSource(contextAt("note.labels.publicationYear."))).toBeNull();+ });+it("stays quiet on empty space, and offers everything when asked explicitly", () => {expect(searchCompletionSource(contextAt("#book "))).toBeNull();@@ -37,6 +66,10 @@ describe("searchCompletionSource", () => {});});+function labelsOf(result: CompletionResult | null): string[] {+ return (result?.options ?? []).map((option) => option.label);+}+/** Stands in for the editor's context, whose `matchBefore` anchors the pattern at the cursor. */function contextAt(text: string, { explicit = false } = {}): CompletionContext {const pos = text.length;diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.tsindex a8cc042681..9e79030dd8 100644--- a/apps/client/src/widgets/ribbon/search_completions.ts+++ b/apps/client/src/widgets/ribbon/search_completions.ts@@ -1,16 +1,22 @@+import { SEARCH_NOTE_PATH, SEARCH_NOTE_PATH_SEGMENTS } from "@triliumnext/commons";import type { Completion, CompletionContext, CompletionResult } from "@triliumnext/codemirror/src/single_line";import { t } from "../../services/i18n";/**- * Offers the top level of Trilium's search syntax: the `note` object a property path hangs off,- * and the comparison operators, whose spellings (`*=*`, `=*`, `~=`) are the part of the syntax- * hardest to recall.+ * Offers Trilium's search syntax: the `note` object, the property path that hangs off it or off a+ * relation, and the comparison operators, whose spellings (`*=*`, `=*`, `~=`) are the part of the+ * syntax hardest to recall.*- * Attribute names, property names and values are not offered yet — reading those needs the note- * being searched, which a source built from static text cannot see.+ * Attribute names and values are not offered — reading those needs the notes being searched, which+ * a source built from static text cannot see.*/export function searchCompletionSource(context: CompletionContext): CompletionResult | null {+ const path = context.matchBefore(PROPERTY_PATH);+ if (path) {+ return pathCompletions(path.text, context.pos);+ }+const operator = context.matchBefore(OPERATOR_PREFIX);if (operator) {return { from: operator.from, options: operatorOptions(), validFor: OPERATOR_PREFIX };@@ -32,6 +38,34 @@ export function searchCompletionSource(context: CompletionContext): CompletionRe/** The characters an operator is spelled with, so typing any of them starts offering them. */const OPERATOR_PREFIX = /[=!*<>%~]+/;const WORD_PREFIX = /[a-zA-Z]+/;+/** A name runs until whitespace or a structural character, as it does in the lexer. */+const SEGMENT = "[^\\s#~().,=<>*!%+\\-'\"`]";+/** The `note` object or a relation, followed by the dotted path walked from it. */+const PROPERTY_PATH = new RegExp(`(?:^|[\\s(])(?:note|~${SEGMENT}+)(?:\\.${SEGMENT}*)+`);+const SEGMENT_TYPED = new RegExp(`${SEGMENT}*`);++/**+ * Completes the segment being typed at the end of `path`, where the grammar allows one. Nothing is+ * offered after `note.labels.`, which expects a label name, or after `note.title.`, which is the+ * end of the path — a terminal property has nothing to walk onto.+ */+function pathCompletions(path: string, pos: number): CompletionResult | null {+ const segments = path.split(".");+ const typed = segments[segments.length - 1];+ const previous = segments[segments.length - 2];+ const beforeThat = segments[segments.length - 3];++ // A segment follows the root, a traversal, or the relation name reached through `relations`.+ const isAllowed = segments.length === 2+ || (SEARCH_NOTE_PATH.traversals as readonly string[]).includes(previous)+ || beforeThat === "relations";++ if (!isAllowed) {+ return null;+ }++ return { from: pos - typed.length, options: segmentOptions(), validFor: SEGMENT_TYPED };+}/*** The options are built per request rather than once, so they read the catalogue after i18n has@@ -42,6 +76,44 @@ function objectOptions(): Completion[] {return [ { label: "note", apply: "note.", type: "namespace", detail: t("search_completion.note") } ];}+function segmentOptions(): Completion[] {+ return SEARCH_NOTE_PATH_SEGMENTS.map((segment) => ({+ label: segment,+ type: "property",+ detail: SEGMENT_DETAILS[segment] ? t(SEGMENT_DETAILS[segment]) : undefined+ }));+}++/**+ * Glosses for the segments whose name does not already say what they hold. Several are the labels+ * the order-by dropdown offers for the same properties, which read the same way here.+ */+const SEGMENT_DETAILS: Record<string, string> = {+ content: "search_completion.property_content",+ rawContent: "search_completion.property_raw_content",+ text: "search_completion.property_text",+ parents: "search_completion.property_parents",+ children: "search_completion.property_children",+ ancestors: "search_completion.property_ancestors",+ labels: "search_completion.property_labels",+ relations: "search_completion.property_relations",+ attributeCount: "search_completion.property_attribute_count",+ labelCount: "search_completion.property_label_count",+ relationCount: "search_completion.property_relation_count",+ relationCountIncludingLinks: "search_completion.property_relation_count_including_links",+ ownedRelationCountIncludingLinks: "search_completion.property_owned_relation_count_including_links",+ targetRelationCountIncludingLinks: "search_completion.property_target_relation_count_including_links",+ parentCount: "order_by.parent_count",+ childrenCount: "order_by.children_count",+ ownedLabelCount: "order_by.owned_label_count",+ ownedRelationCount: "order_by.owned_relation_count",+ targetRelationCount: "order_by.target_relation_count",+ contentSize: "order_by.content_size",+ contentAndAttachmentsSize: "order_by.content_and_attachments_size",+ contentAndAttachmentsAndRevisionsSize: "order_by.content_and_attachments_and_revisions_size",+ revisionCount: "order_by.revision_count"+};+function operatorOptions(): Completion[] {return [{ label: "=", detail: t("search_completion.operator_equal") },diff --git a/packages/commons/src/index.ts b/packages/commons/src/index.tsindex 558762485d..ad87483aed 100644--- a/packages/commons/src/index.ts+++ b/packages/commons/src/index.ts@@ -18,6 +18,7 @@ export * from "./lib/shared_constants.js";export * from "./lib/shared_types.js";export * from "./lib/ws_api.js";export * from "./lib/attribute_names.js";+export * from "./lib/search_syntax.js";export * from "./lib/sort_criteria.js";export * from "./lib/note_type_ids.js";export * from "./lib/promoted_attribute_definition_parser.js";diff --git a/packages/commons/src/lib/search_syntax.ts b/packages/commons/src/lib/search_syntax.tsnew file mode 100644index 0000000000..46f0830e9d--- /dev/null+++ b/packages/commons/src/lib/search_syntax.ts@@ -0,0 +1,51 @@+/**+ * The property path a search query walks from a note: `note.title`, `note.parents.title`,+ * `~author.relations.son.title`. The segments are the ones `parseNoteProperty()` accepts.+ *+ * The search engine keeps the same property names in its own `PROP_MAPPING` tables; a spec in+ * `trilium-core` holds this list against what the engine actually accepts.+ */+export const SEARCH_NOTE_PATH = {+ /** Compared against a value, so an operator follows: `note.title = x`. */+ properties: [+ "noteId",+ "title",+ "type",+ "mime",+ "isProtected",+ "isArchived",+ "dateCreated",+ "dateModified",+ "utcDateCreated",+ "utcDateModified",+ "parentCount",+ "childrenCount",+ "attributeCount",+ "labelCount",+ "ownedLabelCount",+ "relationCount",+ "ownedRelationCount",+ "relationCountIncludingLinks",+ "ownedRelationCountIncludingLinks",+ "targetRelationCount",+ "targetRelationCountIncludingLinks",+ "contentSize",+ "contentAndAttachmentsSize",+ "contentAndAttachmentsAndRevisionsSize",+ "revisionCount"+ ],+ /** Matched against the note's text through a full-text expression rather than a property. */+ contentProperties: [ "content", "rawContent", "text" ],+ /** Step onto another note, so a further segment follows: `note.parents.title`. */+ traversals: [ "parents", "children", "ancestors" ],+ /** Expect an attribute name next, which nothing can enumerate ahead of time. */+ attributeSegments: [ "labels", "relations" ]+} as const;++/** Every segment that can stand directly after a `.` in a property path. */+export const SEARCH_NOTE_PATH_SEGMENTS: readonly string[] = [+ ...SEARCH_NOTE_PATH.properties,+ ...SEARCH_NOTE_PATH.contentProperties,+ ...SEARCH_NOTE_PATH.traversals,+ ...SEARCH_NOTE_PATH.attributeSegments+];diff --git a/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts b/packages/trilium-core/src/services/search/expressions/property_comparison.spec.tsindex 39bc1c09b4..49c9a4065c 100644--- a/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts+++ b/packages/trilium-core/src/services/search/expressions/property_comparison.spec.ts@@ -1,3 +1,4 @@+import { SEARCH_NOTE_PATH } from "@triliumnext/commons";import { beforeEach, describe, expect, it } from "vitest";import becca from "../../../becca/becca.js";@@ -5,7 +6,8 @@ import BBranch from "../../../becca/entities/bbranch.js";import BNote from "../../../becca/entities/bnote.js";import { note, NoteBuilder } from "../../../test/becca_mocking.js";import NoteSet from "../note_set.js";-import PropertyComparisonExp from "./property_comparison.js";+import { PROP_MAPPING as EXTRACTOR_PROP_MAPPING } from "../value_extractor.js";+import PropertyComparisonExp, { PROP_MAPPING } from "./property_comparison.js";/** Build a NoteSet that contains every note currently registered in becca. */function allNotesSet() {@@ -22,6 +24,18 @@ function noteIds(noteSet: NoteSet) {let rootNote: NoteBuilder;+describe("the shared property list", () => {+ it("holds exactly the properties both engine tables accept", () => {+ // `SEARCH_NOTE_PATH.properties` is what the client completes after `note.`, so a property+ // added to one of these tables and not the other would be offered but not understood.+ const shared = [ ...SEARCH_NOTE_PATH.properties ].sort();++ expect(Object.values(PROP_MAPPING).sort()).toEqual(shared);+ expect(Object.values(EXTRACTOR_PROP_MAPPING).sort()).toEqual(shared);+ expect(SEARCH_NOTE_PATH.properties.every((name) => PropertyComparisonExp.isProperty(name.toLowerCase()))).toBe(true);+ });+});+describe("PropertyComparisonExp", () => {beforeEach(() => {becca.reset();diff --git a/packages/trilium-core/src/services/search/expressions/property_comparison.ts b/packages/trilium-core/src/services/search/expressions/property_comparison.tsindex 75e7a4611e..6890242b81 100644--- a/packages/trilium-core/src/services/search/expressions/property_comparison.ts+++ b/packages/trilium-core/src/services/search/expressions/property_comparison.ts@@ -8,7 +8,7 @@ import buildComparator from "../services/build_comparator.js";* Search string is lower cased for case-insensitive comparison. But when retrieving properties,* we need the case-sensitive form, so we have this translation object.*/-const PROP_MAPPING: Record<string, string> = {+export const PROP_MAPPING: Record<string, string> = {noteid: "noteId",title: "title",type: "type",diff --git a/packages/trilium-core/src/services/search/value_extractor.ts b/packages/trilium-core/src/services/search/value_extractor.tsindex e00a0d47b1..6788143111 100644--- a/packages/trilium-core/src/services/search/value_extractor.ts+++ b/packages/trilium-core/src/services/search/value_extractor.ts@@ -6,7 +6,7 @@ import type BNote from "../../becca/entities/bnote.js";* Search string is lower cased for case-insensitive comparison. But when retrieving properties,* we need a case-sensitive form, so we have this translation object.*/-const PROP_MAPPING: Record<string, string> = {+export const PROP_MAPPING: Record<string, string> = {noteid: "noteId",title: "title",type: "type",