From 29ca94cc1e73f4a53edc064668c179c8d5e04612 Mon Sep 17 00:00:00 2001 From: Myles Scolnick Date: Tue, 24 Nov 2026 22:00:52 -0500 Subject: [PATCH] perf: share language server themes across editors --- demo/shared/theme.test.ts | 37 +++++++++++++++++++++ demo/shared/theme.ts | 8 +++-- src/__tests__/theme-sharing.test.ts | 26 +++++++++++++++ src/plugin.ts | 50 ++++++++++++++--------------- 4 files changed, 94 insertions(+), 27 deletions(-) create mode 100644 demo/shared/theme.test.ts create mode 100644 src/__tests__/theme-sharing.test.ts diff --git a/demo/shared/theme.test.ts b/demo/shared/theme.test.ts new file mode 100644 index 0000000..72d0e14 --- /dev/null +++ b/demo/shared/theme.test.ts @@ -0,0 +1,37 @@ +import { EditorState } from "@codemirror/state"; +import { EditorView } from "@codemirror/view"; +import { expect, it } from "vitest"; +import { registerEditor, setThemeMode, themeExtension } from "./theme.js"; + +it("reuses demo styles across editors and theme toggles", () => { + const views = [0, 1].map( + () => + new EditorView({ + state: EditorState.create({ extensions: themeExtension() }), + }), + ); + const unregister = views.map(registerEditor); + const modules = () => + views.map((view) => view.state.facet(EditorView.styleModule)); + try { + setThemeMode("light"); + const light = modules(); + setThemeMode("dark"); + const dark = modules(); + setThemeMode("light"); + const restored = modules(); + for (const pair of [light, dark, restored]) { + expect(pair[0].length).toBeGreaterThan(0); + expect(pair[1]).toHaveLength(pair[0].length); + pair[0].forEach((module, index) => + expect(pair[1][index]).toBe(module), + ); + } + light[0].forEach((module, index) => + expect(restored[0][index]).toBe(module), + ); + } finally { + for (const dispose of unregister) dispose(); + for (const view of views) view.destroy(); + } +}); diff --git a/demo/shared/theme.ts b/demo/shared/theme.ts index f7c912d..38b0434 100644 --- a/demo/shared/theme.ts +++ b/demo/shared/theme.ts @@ -529,9 +529,13 @@ function lspTheme(p: Palette): Extension { }); } +const themes: Record = { + light: [editorTheme(LIGHT), highlightStyle(LIGHT), lspTheme(LIGHT)], + dark: [editorTheme(DARK), highlightStyle(DARK), lspTheme(DARK)], +}; + function themeFor(mode: ThemeMode): Extension { - const p = mode === "dark" ? DARK : LIGHT; - return [editorTheme(p), highlightStyle(p), lspTheme(p)]; + return themes[mode]; } // --------------------------------------------------------------------------- diff --git a/src/__tests__/theme-sharing.test.ts b/src/__tests__/theme-sharing.test.ts new file mode 100644 index 0000000..68f68d9 --- /dev/null +++ b/src/__tests__/theme-sharing.test.ts @@ -0,0 +1,26 @@ +import { EditorState } from "@codemirror/state"; +import { EditorView } from "@codemirror/view"; +import { expect, it } from "vitest"; +import { languageServerWithClient } from "../plugin.js"; +import { createFakeClient } from "./test-utils.js"; + +it.each([ + { diagnosticsEnabled: true, completionEnabled: false }, + { diagnosticsEnabled: false, completionEnabled: true }, + { diagnosticsEnabled: true, completionEnabled: true }, +])("shares style modules with %j", (features) => { + const createModules = () => + EditorState.create({ + extensions: languageServerWithClient({ + client: createFakeClient(), + documentUri: "file:///test.py", + languageId: "python", + ...features, + }), + }).facet(EditorView.styleModule); + const first = createModules(); + const second = createModules(); + expect(first.length).toBeGreaterThan(0); + expect(second).toHaveLength(first.length); + first.forEach((module, index) => expect(second[index]).toBe(module)); +}); diff --git a/src/plugin.ts b/src/plugin.ts index 94f1f87..db21e56 100644 --- a/src/plugin.ts +++ b/src/plugin.ts @@ -2444,6 +2444,29 @@ export function languageServer(options: LanguageServerWebsocketOptions) { }); } +const diagnosticsTheme = EditorView.baseTheme({ + ".cm-lsp-unnecessary": { opacity: "0.6" }, + ".cm-lsp-deprecated": { textDecoration: "line-through" }, + ".cm-diagnostic-related": { + marginTop: "4px", + display: "flex", + flexDirection: "column", + gap: "2px", + }, + ".cm-diagnostic-related-clickable": { cursor: "pointer" }, + ".cm-diagnostic-related-location": { opacity: "0.7" }, + ".cm-diagnostic-code-link": { + display: "inline-block", + marginTop: "4px", + }, +}); + +const deprecatedCompletionTheme = EditorView.baseTheme({ + ".cm-tooltip-autocomplete li.cm-deprecated .cm-completionLabel": { + textDecoration: "line-through", + }, +}); + export function languageServerWithClient(options: LanguageServerOptions) { const shortcuts = { rename: "F2", @@ -2502,25 +2525,7 @@ export function languageServerWithClient(options: LanguageServerOptions) { // plus the field that keeps related-info click targets mapped through // edits. Hosts can override these classes via their own theme. if (featuresOptions.diagnosticsEnabled) { - extensions.push( - relatedLocationAnchors, - EditorView.baseTheme({ - ".cm-lsp-unnecessary": { opacity: "0.6" }, - ".cm-lsp-deprecated": { textDecoration: "line-through" }, - ".cm-diagnostic-related": { - marginTop: "4px", - display: "flex", - flexDirection: "column", - gap: "2px", - }, - ".cm-diagnostic-related-clickable": { cursor: "pointer" }, - ".cm-diagnostic-related-location": { opacity: "0.7" }, - ".cm-diagnostic-code-link": { - display: "inline-block", - marginTop: "4px", - }, - }), - ); + extensions.push(relatedLocationAnchors, diagnosticsTheme); } // Add shortcuts @@ -2750,12 +2755,7 @@ export function languageServerWithClient(options: LanguageServerOptions) { const userOptionClass = options.completionConfig?.optionClass; extensions.push( // Hosts can override via `.cm-tooltip-autocomplete li.cm-deprecated` - EditorView.baseTheme({ - ".cm-tooltip-autocomplete li.cm-deprecated .cm-completionLabel": - { - textDecoration: "line-through", - }, - }), + deprecatedCompletionTheme, autocompletion({ ...options.completionConfig, optionClass: (completion) =>