Skip to content

Build your personal knowledge base with Trilium Notes

fix(codemirror): restore full branch coverage of the search highlighter

Elian Doran9b8499793dd7
diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.ts
index c4c71fbe72..c6f07a8d37 100644
--- a/packages/codemirror/src/extensions/trilium_search_highlighter.ts
+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.ts
@@ -57,6 +57,8 @@ const OPERATOR = /\*=\*|~=|~\*|!=|>=|<=|%=|\*=|=\*|[=><]|[+\-](?=\d)/y;
const BRACKET = /[()]/y;
const NUMBER = /\d+(?:\.\d+)?/y;
const WORD = /[^\s#~().,=<>*!%+\-'"`]+/y;
+// Matches text that ends at the start of a word, or after grouping `(`s that open one.
+const WORD_START = /(?:^|\s)\(*$/;
/** Splits a search query into the tokens worth colouring, skipping over everything else. */
export function tokenizeSearchQuery(text: string): SearchToken[] {
@@ -102,7 +104,7 @@ export function tokenizeSearchQuery(text: string): SearchToken[] {
}
const attribute = match(ATTRIBUTE);
- if (attribute && !fulltextEnded && !/^\(*$/.test(wordBefore(text, pos))) {
+ if (attribute && !fulltextEnded && !WORD_START.test(text.slice(0, pos))) {
// Inside a full-text word, `#` and `~` are text: `c#`, `towers#book`.
pos++;
continue;
@@ -158,11 +160,6 @@ export function tokenizeSearchQuery(text: string): SearchToken[] {
return tokens;
}
-/** The non-whitespace run that ends at `pos`. */
-function wordBefore(text: string, pos: number) {
- return /\S*$/.exec(text.slice(0, pos))?.[0] ?? "";
-}
-
const MARKS: Record<SearchTokenKind, Decoration> = {
label: Decoration.mark({ class: "cm-search-label" }),
relation: Decoration.mark({ class: "cm-search-relation" }),