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): colour the negation apart from the attribute it negates
Elian Doran2930eee011f6
diff --git a/apps/client/src/stylesheets/theme-next-dark.css b/apps/client/src/stylesheets/theme-next-dark.cssindex 659d5719a4..4c2235306f 100644--- a/apps/client/src/stylesheets/theme-next-dark.css+++ b/apps/client/src/stylesheets/theme-next-dark.css@@ -293,6 +293,7 @@/* Search query syntax (the search string of a saved search) */--search-label-color: #79b8ff;--search-relation-color: #c2a8ff;+ --search-negation-color: #ff6b6b;--search-property-color: #5bc4b1;--search-keyword-color: #ff7eb6;--search-operator-color: #9aa4b0;diff --git a/apps/client/src/stylesheets/theme-next-light.css b/apps/client/src/stylesheets/theme-next-light.cssindex 3dea58dd7b..89b66060b9 100644--- a/apps/client/src/stylesheets/theme-next-light.css+++ b/apps/client/src/stylesheets/theme-next-light.css@@ -291,6 +291,7 @@/* Search query syntax (the search string of a saved search) */--search-label-color: #1a6fc4;--search-relation-color: #7d4bbd;+ --search-negation-color: #c62828;--search-property-color: #0f7b6c;--search-keyword-color: #b02a5b;--search-operator-color: #596573;diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.tsindex 60b4e5c499..591271542d 100644--- a/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.spec.ts@@ -85,6 +85,16 @@ describe("tokenizeSearchQuery", () => {});});+describe("negation in the token stream", () => {+ it("keeps a negated attribute one token, which the linter reads as the operand", () => {+ expect(tokenize("#!fiction ~!author = x")).toEqual([+ "label:#!fiction",+ "relation:~!author",+ "operator:="+ ]);+ });+});+describe("triliumSearchHighlighter", () => {it("marks the tokens in the rendered editor", () => {const parent = document.createElement("div");@@ -105,6 +115,24 @@ describe("triliumSearchHighlighter", () => {editor.destroy();}});+ it("draws the negation apart from the attribute it negates", () => {+ const parent = document.createElement("div");+ document.body.appendChild(parent);+ const editor = createFieldEditor({+ parent,+ doc: "#!fiction ~!author.title = x",+ extensions: [ triliumSearchHighlighter ]+ });++ try {+ // The marker and the name keep the attribute's own colour, split around the `!`.+ expect(textOf(parent, ".cm-search-negation")).toEqual([ "!", "!" ]);+ expect(textOf(parent, ".cm-search-label")).toEqual([ "#", "fiction" ]);+ expect(textOf(parent, ".cm-search-relation")).toEqual([ "~", "author" ]);+ } finally {+ editor.destroy();+ }+ });});/** Renders the tokens as `kind:text` pairs, which reads far better in an expectation than offsets. */diff --git a/packages/codemirror/src/extensions/trilium_search_highlighter.ts b/packages/codemirror/src/extensions/trilium_search_highlighter.tsindex 87393ef38d..36cf422693 100644--- a/packages/codemirror/src/extensions/trilium_search_highlighter.ts+++ b/packages/codemirror/src/extensions/trilium_search_highlighter.ts@@ -159,6 +159,13 @@ const MARKS: Record<SearchTokenKind, Decoration> = {bracket: Decoration.mark({ class: "cm-search-bracket" })};+/**+ * The `!` of `#!fiction`, drawn apart from the attribute it negates so the one character that+ * inverts a clause is not read as part of the name. It is a decoration rather than a token of its+ * own: the linter reads the same stream and takes an attribute to be one token.+ */+const NEGATION_MARK = Decoration.mark({ class: "cm-search-negation" });+/*** The Next themes own the palette (`--search-*` in theme-next-light.css / theme-next-dark.css).* The fallbacks below are what the legacy themes — which don't define those variables — render@@ -167,6 +174,7 @@ const MARKS: Record<SearchTokenKind, Decoration> = {const searchHighlightTheme = EditorView.baseTheme({".cm-search-label": { color: "var(--search-label-color, #539bf5)" },".cm-search-relation": { color: "var(--search-relation-color, #8957e5)" },+ ".cm-search-negation": { color: "var(--search-negation-color, #e5534b)" },".cm-search-property": { color: "var(--search-property-color, #268a8a)" },".cm-search-keyword": { color: "var(--search-keyword-color, #bf3989)", fontWeight: "bold" },".cm-search-operator": { color: "var(--search-operator-color, var(--muted-text-color))" },@@ -192,10 +200,23 @@ export const triliumSearchHighlighter: Extension = [ searchHighlightField, searcfunction buildSearchDecorations(state: EditorState): DecorationSet {const builder = new RangeSetBuilder<Decoration>();+ const text = state.doc.toString();++ for (const token of tokenizeSearchQuery(text)) {+ const negation = token.from + 1;++ if (ATTRIBUTE_KINDS.has(token.kind) && text[negation] === "!") {+ builder.add(token.from, negation, MARKS[token.kind]);+ builder.add(negation, negation + 1, NEGATION_MARK);+ builder.add(negation + 1, token.to, MARKS[token.kind]);+ continue;+ }- for (const token of tokenizeSearchQuery(state.doc.toString())) {builder.add(token.from, token.to, MARKS[token.kind]);}return builder.finish();}++/** The tokens spelled with a marker, which is the only place a `!` can negate. */+const ATTRIBUTE_KINDS: ReadonlySet<SearchTokenKind> = new Set([ "label", "relation" ]);