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
client/code editor: highlight the current line number in the line gutter
Adorian Doran5f628df19448
diff --git a/apps/client/src/stylesheets/style.css b/apps/client/src/stylesheets/style.cssindex e2f84ba9ec..43fd9358ff 100644--- a/apps/client/src/stylesheets/style.css+++ b/apps/client/src/stylesheets/style.css@@ -1503,9 +1503,17 @@ body.mobile .modal-content-with-sidebar > .modal-main > .modal-header > .modal-tpadding: 0;}-pre.code-embed-editor.read-only .cm-activeLine,-pre.code-embed-editor > .cm-editor:not(.cm-focused) .cm-activeLine {- background-color: transparent;+pre.code-embed-editor.read-only,+pre.code-embed-editor > .cm-editor:not(.cm-focused) {+ .cm-activeLine {+ background-color: transparent;+ }++ .cm-activeLineGutter {+ background-color: transparent;+ color: inherit;+ font-weight: inherit;+ }}.include-note-content.type-code {diff --git a/packages/codemirror/src/index.spec.ts b/packages/codemirror/src/index.spec.tsindex 547750c762..d11e89aaf3 100644--- a/packages/codemirror/src/index.spec.ts+++ b/packages/codemirror/src/index.spec.ts@@ -361,6 +361,21 @@ describe("CodeMirror", () => {expect(hasLineNumbers(editor)).toBe(false);});+ it("highlights the number of the active line while the line numbers show", () => {+ const activeNumbers = (view: CodeMirror) => [+ ...view.dom.querySelectorAll(".cm-lineNumbers .cm-activeLineGutter")+ ].map((element) => element.textContent);+ editor = build();+ editor.loadText("one\ntwo\nthree");+ editor.dispatch({ selection: { anchor: editor.state.doc.line(2).from } });+ expect(activeNumbers(editor)).toEqual([ "2" ]);++ editor.setLineNumbers(false);+ expect(editor.dom.querySelector(".cm-activeLineGutter")).toBeNull();+ editor.setLineNumbers(true);+ expect(activeNumbers(editor)).toEqual([ "2" ]);+ });+it("blends an inset gutter into the code while the line numbers are off", async () => {const theme = getThemeById("github-light");expect(theme).not.toBeNull();diff --git a/packages/codemirror/src/index.ts b/packages/codemirror/src/index.tsindex ecf8382179..bda301bc5d 100644--- a/packages/codemirror/src/index.ts+++ b/packages/codemirror/src/index.ts@@ -1,5 +1,5 @@import { defaultKeymap, history, historyKeymap } from "@codemirror/commands";-import { EditorView, highlightActiveLine, keymap, lineNumbers, placeholder, tooltips, ViewPlugin, ViewUpdate, type EditorViewConfig, KeyBinding } from "@codemirror/view";+import { EditorView, highlightActiveLine, highlightActiveLineGutter, keymap, lineNumbers, placeholder, tooltips, ViewPlugin, ViewUpdate, type EditorViewConfig, KeyBinding } from "@codemirror/view";import { defaultHighlightStyle, StreamLanguage, syntaxHighlighting, indentUnit, bracketMatching, foldGutter, codeFolding } from "@codemirror/language";import {Compartment, countColumn, EditorSelection, EditorState, Prec, StateEffect, Transaction,@@ -176,9 +176,7 @@ export default class CodeMirror extends EditorView {typeCompletionCompartment.of([]),completionSourceCompartment.of([]),highlightActiveLine(),- lineNumbersCompartment.of(- config.lineNumbers === false ? hiddenLineNumbersTheme : lineNumbers()- ),+ lineNumbersCompartment.of(buildLineNumbers(config.lineNumbers !== false)),themeCompartment.of([syntaxHighlighting(defaultHighlightStyle, { fallback: true })]),@@ -428,11 +426,7 @@ export default class CodeMirror extends EditorView {this.hasLineNumbers = isShown;this.dispatch({- effects: [- this.lineNumbersCompartment.reconfigure(- isShown ? lineNumbers() : hiddenLineNumbersTheme- )- ]+ effects: [ this.lineNumbersCompartment.reconfigure(buildLineNumbers(isShown)) ]});}@@ -824,6 +818,14 @@ const hiddenCaretTheme = Prec.highest(EditorView.theme({/** The extension of each theme that an editor has loaded. */const loadedThemes = new WeakMap<ThemeDefinition, Extension>();+/**+ * The line numbers with the number of the active line highlighted, or a gutter merged into the+ * code area. The fold markers left in a hidden gutter take no highlight.+ */+function buildLineNumbers(isShown: boolean): Extension {+ return isShown ? [ lineNumbers(), highlightActiveLineGutter() ] : hiddenLineNumbersTheme;+}+/*** Merges the gutter, which still holds the fold markers, into the code area while the line* numbers are off, and keeps the markers off the edge of the editor. `Prec.highest` outranks the