Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): complete the keywords that join and order a query

Elian Doran2fd6752924fc
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index 5d8821cee8..897c5aedbe 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4328,6 +4328,11 @@
"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"
+ "property_target_relation_count_including_links": "Number of relations targeting the note, counting the ones made by links",
+ "keyword_and": "Both conditions must match",
+ "keyword_or": "Either condition must match",
+ "keyword_not": "Excludes the notes the condition in parentheses matches",
+ "keyword_order_by": "Sorts the results by a property or label",
+ "keyword_limit": "Keeps only the first results"
}
}
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index d67d5ca0a0..74b2f8aeb5 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -7,14 +7,24 @@ import { searchCompletionSource } from "./search_completions";
vi.mock("../../services/i18n", () => ({ t: (key: string) => key }));
describe("searchCompletionSource", () => {
- it("offers the note object while a word is being typed, anchored at its start", () => {
+ it("offers the note object and the keywords while a word is being typed, anchored at its start", () => {
const result = searchCompletionSource(contextAt("#book AND no"));
expect(result?.from).toBe(10);
- expect(result?.options.map((option) => option.label)).toEqual([ "note" ]);
- // A bare `note` is never a complete clause, so the dot comes with it.
- expect(result?.options[0].apply).toBe("note.");
- expect(result?.options[0].detail).toBe("search_completion.note");
+ expect(labelsOf(result)).toEqual([ "note", "and", "or", "not", "orderBy", "limit" ]);
+ // Both complete with what has to follow them.
+ expect(optionFor(result, "note")?.apply).toBe("note.");
+ expect(optionFor(result, "not")?.apply).toBe("not(");
+ expect(optionFor(result, "note")?.detail).toBe("search_completion.note");
+ });
+
+ it("offers the sort directions only once an orderBy is open", () => {
+ expect(labelsOf(searchCompletionSource(contextAt("#book de")))).not.toContain("desc");
+
+ const ordering = searchCompletionSource(contextAt("#book orderBy #year de"));
+
+ expect(labelsOf(ordering)).toContain("desc");
+ expect(optionFor(ordering, "asc")?.detail).toBe("order_by.asc");
});
it("offers every operator once one of their characters is typed", () => {
@@ -62,7 +72,8 @@ describe("searchCompletionSource", () => {
const explicit = searchCompletionSource(contextAt("#book ", { explicit: true }));
expect(explicit?.from).toBe(6);
- expect(explicit?.options).toHaveLength(13);
+ // The six words, then every operator.
+ expect(explicit?.options).toHaveLength(18);
});
});
@@ -70,6 +81,10 @@ function labelsOf(result: CompletionResult | null): string[] {
return (result?.options ?? []).map((option) => option.label);
}
+function optionFor(result: CompletionResult | null, label: string) {
+ return result?.options.find((option) => option.label === 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;
@@ -78,7 +93,8 @@ function contextAt(text: string, { explicit = false } = {}): CompletionContext {
pos,
explicit,
matchBefore(expr: RegExp) {
- const match = new RegExp(`(?:${expr.source})$`).exec(text);
+ // Flags carry over, the way CodeMirror's own `ensureAnchor` keeps them.
+ const match = new RegExp(`(?:${expr.source})$`, expr.flags).exec(text);
return match ? { from: pos - match[0].length, to: pos, text: match[0] } : null;
}
} as unknown as CompletionContext;
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index 9e79030dd8..3d7113e381 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -22,14 +22,17 @@ export function searchCompletionSource(context: CompletionContext): CompletionRe
return { from: operator.from, options: operatorOptions(), validFor: OPERATOR_PREFIX };
}
+ // `asc` and `desc` order an `orderBy` key, so they are worth offering only once one is open.
+ const isOrdering = !!context.matchBefore(ORDER_BY_BEFORE);
+
const word = context.matchBefore(WORD_PREFIX);
if (word) {
- return { from: word.from, options: objectOptions(), validFor: WORD_PREFIX };
+ return { from: word.from, options: wordOptions(isOrdering), validFor: WORD_PREFIX };
}
// Ctrl-Space on empty space asks for everything on offer.
if (context.explicit) {
- return { from: context.pos, options: [ ...objectOptions(), ...operatorOptions() ] };
+ return { from: context.pos, options: [ ...wordOptions(isOrdering), ...operatorOptions() ] };
}
return null;
@@ -43,6 +46,8 @@ 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}*`);
+/** Matches once an `orderBy` has been opened anywhere before the cursor. */
+const ORDER_BY_BEFORE = /orderby[^]*/i;
/**
* Completes the segment being typed at the end of `path`, where the grammar allows one. Nothing is
@@ -68,12 +73,30 @@ function pathCompletions(path: string, pos: number): CompletionResult | null {
}
/**
- * The options are built per request rather than once, so they read the catalogue after i18n has
+ * Everything spelled as a word: the `note` object and the keywords that join, order and cut down a
+ * query. Built per request rather than once, so the options read the catalogue after i18n has
* loaded and follow a language switched while the app is running.
*/
-function objectOptions(): Completion[] {
- // `note.` completes with the dot, since a bare `note` is never a complete clause.
- return [ { label: "note", apply: "note.", type: "namespace", detail: t("search_completion.note") } ];
+function wordOptions(isOrdering: boolean): Completion[] {
+ const options: Completion[] = [
+ // Both complete with what has to follow them: a bare `note`, or a `not` without its
+ // parenthesised sub-expression, is never a clause on its own.
+ { label: "note", apply: "note.", type: "namespace", detail: t("search_completion.note") },
+ { label: "and", type: "keyword", detail: t("search_completion.keyword_and") },
+ { label: "or", type: "keyword", detail: t("search_completion.keyword_or") },
+ { label: "not", apply: "not(", type: "keyword", detail: t("search_completion.keyword_not") },
+ { label: "orderBy", type: "keyword", detail: t("search_completion.keyword_order_by") },
+ { label: "limit", type: "keyword", detail: t("search_completion.keyword_limit") }
+ ];
+
+ if (isOrdering) {
+ options.push(
+ { label: "asc", type: "keyword", detail: t("order_by.asc") },
+ { label: "desc", type: "keyword", detail: t("order_by.desc") }
+ );
+ }
+
+ return options;
}
function segmentOptions(): Completion[] {