Skip to content

Build your personal knowledge base with Trilium Notes

feat(search): offer the negated attribute markers beside the plain ones

Elian Doran6833f40591b6
diff --git a/apps/client/src/translations/en/translation.json b/apps/client/src/translations/en/translation.json
index f2eb1da277..e3b8f92198 100644
--- a/apps/client/src/translations/en/translation.json
+++ b/apps/client/src/translations/en/translation.json
@@ -4350,6 +4350,8 @@
"date_month_offset": "Any number of months before this one",
"date_year_offset": "Any number of years before this one",
"label_marker": "Refers to a label. Press Enter to see the list of labels",
- "relation_marker": "Refers to a relation. Press Enter to see the list of relations"
+ "label_marker_negated": "Excludes a label. Press Enter to see the list of labels",
+ "relation_marker": "Refers to a relation. Press Enter to see the list of relations",
+ "relation_marker_negated": "Excludes a relation. Press Enter to see the list of relations"
}
}
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index dc11fa5e33..2cbdfda01d 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -25,7 +25,7 @@ describe("searchCompletionSource", () => {
const result = await complete("#book AND no");
expect(result?.from).toBe(10);
- expect(labelsOf(result)).toEqual([ "#", "~", "note", "and", "or", "not", "orderBy", "limit" ]);
+ 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(");
@@ -60,7 +60,7 @@ describe("searchCompletionSource", () => {
// Outside an ordering the keywords are untouched.
expect(labelsOf(await complete("#book n")))
- .toEqual([ "#", "~", "note", "and", "or", "not", "orderBy", "limit" ]);
+ .toEqual([ "#", "#!", "~", "~!", "note", "and", "or", "not", "orderBy", "limit" ]);
});
it("offers every operator once one of their characters is typed", async () => {
@@ -89,7 +89,7 @@ describe("searchCompletionSource", () => {
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" ]);
+ .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(">");
@@ -127,16 +127,19 @@ describe("searchCompletionSource", () => {
const explicit = await complete("#book ", { explicit: true });
expect(explicit?.from).toBe(6);
- // The words and the two attribute markers, then every operator.
- expect(explicit?.options).toHaveLength(20);
+ // The words and the four attribute markers, then every operator.
+ expect(explicit?.options).toHaveLength(22);
// CodeMirror sorts by score and ignores the order offered, so the markers are boosted
// to the top rather than merely listed first.
+ // Each negated marker sits directly under the one it negates.
expect(optionFor(explicit, "#")?.boost).toBe(99);
- expect(optionFor(explicit, "~")?.boost).toBe(98);
+ expect(optionFor(explicit, "#!")?.boost).toBe(98);
+ expect(optionFor(explicit, "~")?.boost).toBe(97);
+ expect(optionFor(explicit, "~!")?.boost).toBe(96);
// Everything that leaves a clause unfinished reopens the popup on what follows it.
const reopening = explicit?.options.filter(searchCompletionReactivates).map((o) => o.label);
- expect(reopening).toEqual([ "#", "~", "note", "not" ]);
+ expect(reopening).toEqual([ "#", "#!", "~", "~!", "note", "not" ]);
});
describe("attribute names", () => {
@@ -148,6 +151,9 @@ describe("searchCompletionSource", () => {
expect(labelsOf(result)).toEqual([ "book", "archived" ]);
expect((await complete("#!bo"))?.from).toBe(2);
+ // What the negated marker inserts on its own, which reopens the popup on the name.
+ expect(labelsOf(await complete("#!"))).toEqual([ "book", "archived" ]);
+ expect(labelsOf(await complete("~!"))).toEqual([ "book", "archived" ]);
});
it("ranks a built-in below a name of the user's own that matches as well", async () => {
@@ -278,7 +284,7 @@ describe("searchCompletionSource", () => {
// A property whose values nothing can enumerate falls through to the keywords, and a
// label that happens to share a property's name is still a label.
expect(labelsOf(await complete("note.title = so")))
- .toEqual([ "#", "~", "note", "and", "or", "not", "orderBy", "limit" ]);
+ .toEqual([ "#", "#!", "~", "~!", "note", "and", "or", "not", "orderBy", "limit" ]);
expect(labelsOf(await complete("note.labels.type = fic"))).toEqual([ "fiction", "science fiction" ]);
});
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index b6be5d0373..0746936a78 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -376,11 +376,16 @@ function wordOptions(ordering: OrderingPosition): Completion[] {
// takes them too. Drawn as keywords: the marker is already the icon.
const attributeMarkers: Completion[] = [
{ label: "#", apply: "#", type: "keyword", boost: MARKER_BOOST, detail: t("search_completion.label_marker") },
- { label: "~", apply: "~", type: "keyword", boost: MARKER_BOOST - 1, detail: t("search_completion.relation_marker") }
+ { label: "#!", apply: "#!", type: "keyword", boost: MARKER_BOOST - 1, detail: t("search_completion.label_marker_negated") },
+ { label: "~", apply: "~", type: "keyword", boost: MARKER_BOOST - 2, detail: t("search_completion.relation_marker") },
+ { label: "~!", apply: "~!", type: "keyword", boost: MARKER_BOOST - 3, detail: t("search_completion.relation_marker_negated") }
];
+ // A sort key names the value to order by, which a negated marker has none of: `ValueExtractor`
+ // reads `#!foo` as a label named `!foo` and would quietly order by one no note carries.
+ const sortKeyMarkers = attributeMarkers.filter(({ label }) => !label.endsWith("!"));
if (ordering === "key") {
- return [ ...attributeMarkers, noteObject ];
+ return [ ...sortKeyMarkers, noteObject ];
}
if (ordering === "sorted") {
@@ -404,7 +409,7 @@ function wordOptions(ordering: OrderingPosition): Completion[] {
/**
* Whether picking `completion` reopens the popup. Every option that inserts one of these leaves a
- * clause unfinished: `note.` and `#` are waiting for a name, `not(` for a sub-expression.
+ * clause unfinished: `note.`, `#` and `#!` are waiting for a name, `not(` for a sub-expression.
*/
export function searchCompletionReactivates(completion: Completion): boolean {
const applied = completion.apply;
@@ -412,7 +417,7 @@ export function searchCompletionReactivates(completion: Completion): boolean {
return typeof applied === "string" && OPENERS.includes(applied.slice(-1));
}
-const OPENERS = [ ".", "(", "#", "~" ];
+const OPENERS = [ ".", "(", "#", "~", "!" ];
/**
* Where the cursor stands in an `orderBy`, which decides what can follow it.