Skip to content

Build your personal knowledge base with Trilium Notes

fix(search): offer the search keywords only on Ctrl+Space

Elian Dorandf01d7995656
diff --git a/apps/client/src/widgets/ribbon/search_completions.spec.ts b/apps/client/src/widgets/ribbon/search_completions.spec.ts
index 05d99d9537..c8e4d93f7c 100644
--- a/apps/client/src/widgets/ribbon/search_completions.spec.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.spec.ts
@@ -21,8 +21,11 @@ beforeEach(() => {
});
describe("searchCompletionSource", () => {
- it("offers the note object and the keywords while a word is being typed, anchored at its start", async () => {
- const result = await complete("#book AND no");
+ it("offers the note object and the keywords for a word only when asked, anchored at its start", async () => {
+ // A plain word is most often a search term, so typing one opens nothing.
+ expect(await complete("#book AND no")).toBeNull();
+
+ const result = await complete("#book AND no", { explicit: true });
expect(result?.from).toBe(10);
expect(labelsOf(result)).toEqual([ "#", "#!", "~", "~!", "note", "and", "or", "not", "orderBy", "limit" ]);
@@ -33,9 +36,9 @@ describe("searchCompletionSource", () => {
});
it("offers the sort directions only once an orderBy is open", async () => {
- expect(labelsOf(await complete("#book de"))).not.toContain("desc");
+ expect(labelsOf(await complete("#book de", { explicit: true }))).not.toContain("desc");
- const ordering = await complete("#book orderBy #year de");
+ const ordering = await complete("#book orderBy #year de", { explicit: true });
expect(labelsOf(ordering)).toContain("desc");
expect(optionFor(ordering, "asc")?.detail).toBe("order_by.asc");
@@ -43,23 +46,23 @@ describe("searchCompletionSource", () => {
it("narrows an orderBy to what can follow the key being written", async () => {
// A key is a property path or an attribute name, which the branches above answer for.
- expect(labelsOf(await complete("#book orderBy n"))).toEqual([ "#", "~", "note" ]);
+ expect(labelsOf(await complete("#book orderBy n", { explicit: true }))).toEqual([ "#", "~", "note" ]);
expect(labelsOf(await complete("#book orderBy ", { explicit: true }))).toEqual([ "#", "~", "note" ]);
// Once a key stands there it can be sorted, followed by another, or cut short.
- expect(labelsOf(await complete("#book orderBy note.title d")))
+ expect(labelsOf(await complete("#book orderBy note.title d", { explicit: true })))
.toEqual([ "asc", "desc", "limit" ]);
- expect(labelsOf(await complete("#book orderBy note.title desc, note.dateCreated a")))
+ expect(labelsOf(await complete("#book orderBy note.title desc, note.dateCreated a", { explicit: true })))
.toEqual([ "asc", "desc", "limit" ]);
// A comma opens the next key, which has no direction of its own yet.
- expect(labelsOf(await complete("#book orderBy note.title, n"))).toEqual([ "#", "~", "note" ]);
+ expect(labelsOf(await complete("#book orderBy note.title, n", { explicit: true }))).toEqual([ "#", "~", "note" ]);
// An ordering names a key and sorts on it; nothing in it is compared.
expect(await complete("#book orderBy note.title =")).toBeNull();
// Outside an ordering the keywords are untouched.
- expect(labelsOf(await complete("#book n")))
+ expect(labelsOf(await complete("#book n", { explicit: true })))
.toEqual([ "#", "#!", "~", "~!", "note", "and", "or", "not", "orderBy", "limit" ]);
});
@@ -229,8 +232,8 @@ describe("searchCompletionSource", () => {
});
it("stops once the value is finished, so the keywords follow it", async () => {
- expect(labelsOf(await complete("#genre = fiction an"))).toContain("and");
- expect(labelsOf(await complete("#genre = \"science fiction\" an"))).toContain("and");
+ expect(labelsOf(await complete("#genre = fiction an", { explicit: true }))).toContain("and");
+ expect(labelsOf(await complete("#genre = \"science fiction\" an", { explicit: true }))).toContain("and");
expect(server.get).not.toHaveBeenCalled();
});
@@ -281,9 +284,11 @@ describe("searchCompletionSource", () => {
expect(labelsOf(await complete("note.type = code and note.mime = pyt"))).toContain("text/x-python");
expect(labelsOf(await complete("~author.type = co"))).toContain("code");
- // 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")))
+ // A property whose values nothing can enumerate falls through to the keywords, which
+ // wait to be asked for, and a label that happens to share a property's name is still a
+ // label.
+ expect(await complete("note.title = so")).toBeNull();
+ expect(labelsOf(await complete("note.title = so", { explicit: true })))
.toEqual([ "#", "#!", "~", "~!", "note", "and", "or", "not", "orderBy", "limit" ]);
expect(labelsOf(await complete("note.labels.type = fic"))).toEqual([ "fiction", "science fiction" ]);
});
@@ -326,12 +331,12 @@ describe("searchCompletionIcon", () => {
});
it("leaves everything else undrawn", async () => {
- expect(iconFor(await complete("no"), "note")).toBeUndefined();
- expect(iconFor(await complete("or"), "orderBy")).toBeUndefined();
+ expect(iconFor(await complete("no", { explicit: true }), "note")).toBeUndefined();
+ expect(iconFor(await complete("or", { explicit: true }), "orderBy")).toBeUndefined();
expect(iconFor(await complete("note."), "title")).toBeUndefined();
// The marker is the icon; drawing one beside it reads as noise.
- expect(iconFor(await complete("no"), "#")).toBeUndefined();
- expect(iconFor(await complete("no"), "~")).toBeUndefined();
+ expect(iconFor(await complete("no", { explicit: true }), "#")).toBeUndefined();
+ expect(iconFor(await complete("no", { explicit: true }), "~")).toBeUndefined();
expect(iconFor(await complete("#year >"), ">=")).toBeUndefined();
});
});
diff --git a/apps/client/src/widgets/ribbon/search_completions.ts b/apps/client/src/widgets/ribbon/search_completions.ts
index 109f06a03e..0b77df0377 100644
--- a/apps/client/src/widgets/ribbon/search_completions.ts
+++ b/apps/client/src/widgets/ribbon/search_completions.ts
@@ -68,8 +68,10 @@ export function searchCompletionSource(context: CompletionContext): CompletionOu
return options.length ? { from: operator.from, options, validFor: OPERATOR_PREFIX } : null;
}
+ // A plain word is most often a search term, so the keywords wait for Ctrl-Space.
const word = context.matchBefore(WORD_PREFIX);
if (word) {
+ if (!context.explicit) return null;
return { from: word.from, options: wordOptions(ordering), validFor: WORD_PREFIX };
}
diff --git a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
index 4f79044360..69690baeeb 100644
--- a/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
+++ b/apps/server/src/assets/doc_notes/en/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.html
@@ -91,6 +91,11 @@
<h3>Autocomplete</h3>
<p>To help with the syntax, Trilium offers an autocomplete functionality
which can be triggered by pressing <kbd>Ctrl</kbd>+<kbd>Space</kbd>.</p>
+<p>Some suggestions also open on their own as you type: label and relation
+ names after <code>#</code> and <code>~</code>, fields after <code>.</code>,
+ operators, values, and notes after <code>@</code>. Keywords such as <code>and</code>, <code>or</code>, <code>not</code>, <code>orderBy</code> and <code>limit</code> appear
+ only on <kbd>Ctrl</kbd>+<kbd>Space</kbd>, since a plain word is usually
+ a search term.</p>
<p>No suggestion is selected when the list opens, so <kbd>Enter</kbd> runs
the search as typed. To insert a suggestion, select it with <kbd>Down</kbd> and
press <kbd>Enter</kbd>.</p>
diff --git a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
index 1d9c6c8feb..5d482b0425 100644
--- a/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
+++ b/docs/User Guide/User Guide/Basic Concepts and Features/Navigation/Search.md
@@ -50,6 +50,8 @@ To search for notes, click on the magnifying glass icon on the toolbar or press
To help with the syntax, Trilium offers an autocomplete functionality which can be triggered by pressing <kbd>Ctrl</kbd>+<kbd>Space</kbd>.
+Some suggestions also open on their own as you type: label and relation names after `#` and `~`, fields after `.`, operators, values, and notes after `@`. Keywords such as `and`, `or`, `not`, `orderBy` and `limit` appear only on <kbd>Ctrl</kbd>+<kbd>Space</kbd>, since a plain word is usually a search term.
+
No suggestion is selected when the list opens, so <kbd>Enter</kbd> runs the search as typed. To insert a suggestion, select it with <kbd>Down</kbd> and press <kbd>Enter</kbd>.
The autocomplete offers: