From daee26e680b3dc4cc328fb43864a35252bbb39de Mon Sep 17 00:00:00 2001
From: audichuang <72485427+audichuang@users.noreply.github.com>
Date: Thu, 26 Nov 2026 12:51:14 +0800
Subject: [PATCH] fix(editor): stop adding theme rules for every editor tab and
notebook cell (#5995)
breakpointGutter, inlineBlame, indexedMarkers and the notebook CellEditor
built EditorView.theme / baseTheme inside their factories, so every editor or
notebook cell mounted a new style module whose rules CodeMirror never removes
(+12 rules per file editor, +1.4k while scrolling a 100-cell notebook). The
four themes are now module constants, like the other editor themes; the font
theme in setup.ts is already cached per key.
Co-authored-by: audichuang <72485427+audichuang@users.noreply.github.com>
---
src/editor/core/cm/themeOnce.test.ts | 39 +++++++++++++++++++
.../debugger/editor/breakpointGutter.ts | 13 +++----
src/features/notebook/CellEditor.test.tsx | 17 ++++++++
src/features/notebook/CellEditor.tsx | 13 ++++---
src/features/plugins/indexedMarkers.ts | 29 +++++++-------
src/features/vcs/inlineBlame.ts | 5 ++-
6 files changed, 90 insertions(+), 26 deletions(-)
create mode 100644 src/editor/core/cm/themeOnce.test.ts
diff --git a/src/editor/core/cm/themeOnce.test.ts b/src/editor/core/cm/themeOnce.test.ts
new file mode 100644
index 000000000..c9e3f4855
--- /dev/null
+++ b/src/editor/core/cm/themeOnce.test.ts
@@ -0,0 +1,39 @@
+// @vitest-environment jsdom
+import { afterEach, expect, it } from "vitest";
+import { EditorState, type Extension } from "@codemirror/state";
+import { EditorView } from "@codemirror/view";
+import type { EditorDocContext } from "@/editor/api";
+import { breakpointGutter } from "@/features/debugger/editor/breakpointGutter";
+import { indexedMarkers } from "@/features/plugins/indexedMarkers";
+import { inlineBlame } from "@/features/vcs/inlineBlame";
+import { iconAreaExtension } from "./iconArea";
+
+const ctx: EditorDocContext = { doc: 1, path: "/p/A.java", language: "java", uiMode: null, readOnly: false };
+const views: EditorView[] = [];
+afterEach(() => views.splice(0).forEach((v) => v.destroy()));
+
+function cssRules(): number {
+ let n = 0;
+ for (const sheet of [...document.styleSheets, ...(document.adoptedStyleSheets ?? [])]) n += sheet.cssRules.length;
+ return n;
+}
+
+function mount(extensions: Extension): void {
+ const parent = document.createElement("div");
+ document.body.append(parent);
+ views.push(new EditorView({ state: EditorState.create({ doc: "a\nb", extensions }), parent }));
+}
+
+// A theme built inside an extension factory mounts new CSS rules for every editor that calls it.
+// The rules are never removed, and each insertion makes the browser restyle the whole document.
+it.each([
+ ["breakpointGutter", () => breakpointGutter(ctx)],
+ ["inlineBlame", () => inlineBlame(ctx)],
+ ["indexedMarkers", () => [iconAreaExtension, indexedMarkers(ctx.path)]],
+])("%s mounts its theme once, however many editors use it", (_name, extensions) => {
+ mount(extensions());
+ const after = cssRules();
+ mount(extensions());
+ mount(extensions());
+ expect(cssRules()).toBe(after);
+});
diff --git a/src/features/debugger/editor/breakpointGutter.ts b/src/features/debugger/editor/breakpointGutter.ts
index c094b88dd..ae4f90a3b 100644
--- a/src/features/debugger/editor/breakpointGutter.ts
+++ b/src/features/debugger/editor/breakpointGutter.ts
@@ -136,6 +136,11 @@ function inBreakpointArea(e: MouseEvent): boolean {
return t.classList.contains("cm-gutterElement") && t.childElementCount === 0;
}
+/** Created once: every `EditorView.theme` call mounts new CSS rules into the document. */
+const BREAKPOINT_GUTTER_THEME = EditorView.theme({
+ ".cm-lineNumbers .cm-gutterElement": { cursor: "default" },
+});
+
export function breakpointGutter(ctx: EditorDocContext): Extension {
const reason = classFileBreakpointReason(ctx.path);
if (reason) {
@@ -311,11 +316,5 @@ export function breakpointGutter(ctx: EditorDocContext): Extension {
},
);
- return [
- field,
- plugin,
- EditorView.theme({
- ".cm-lineNumbers .cm-gutterElement": { cursor: "default" },
- }),
- ];
+ return [field, plugin, BREAKPOINT_GUTTER_THEME];
}
diff --git a/src/features/notebook/CellEditor.test.tsx b/src/features/notebook/CellEditor.test.tsx
index 1191d0015..e4cc0c585 100644
--- a/src/features/notebook/CellEditor.test.tsx
+++ b/src/features/notebook/CellEditor.test.tsx
@@ -87,3 +87,20 @@ describe("cell editor focus", () => {
await session.close();
});
});
+
+describe("cell editor theme", () => {
+ const cssRules = () => [...document.styleSheets].reduce((n, sheet) => n + sheet.cssRules.length, 0);
+
+ // Every cell mounting a freshly built theme would add CSS rules and restyle the whole notebook.
+ it("adds no CSS rules for each further cell", async () => {
+ const session = makeSession();
+ await session.load();
+ const cell = () => ;
+ const first = render(cell());
+ const after = cssRules();
+ const others = [render(cell()), render(cell())];
+ expect(cssRules()).toBe(after);
+ [first, ...others].forEach((r) => r.unmount());
+ await session.close();
+ });
+});
diff --git a/src/features/notebook/CellEditor.tsx b/src/features/notebook/CellEditor.tsx
index ea2f5a653..7b2f2bde6 100644
--- a/src/features/notebook/CellEditor.tsx
+++ b/src/features/notebook/CellEditor.tsx
@@ -31,6 +31,13 @@ function languageServices(type: string, doc: NotebookCellDoc | undefined) {
return cellEditorExtensions({ doc: doc.doc, path: doc.uri, language: "python", uiMode: "Python", readOnly: false });
}
+/** Created once: every `EditorView.theme` call mounts new CSS rules into the document. */
+const CELL_EDITOR_THEME = EditorView.theme({
+ "&.cm-editor": { height: "auto" },
+ // `auto`: past either end of a long cell the wheel scrolls the notebook.
+ "&.cm-editor .cm-scroller": { maxHeight: "640px", overflow: "auto", overscrollBehavior: "auto" },
+});
+
export function CellEditor({
session,
id,
@@ -106,11 +113,7 @@ export function CellEditor({
services.current.of(languageServices(type, initialDoc.current)),
readonly.current.of(EditorState.readOnly.of(session.getState().frozen)),
EditorView.lineWrapping,
- EditorView.theme({
- "&.cm-editor": { height: "auto" },
- // `auto`: past either end of a long cell the wheel scrolls the notebook.
- "&.cm-editor .cm-scroller": { maxHeight: "640px", overflow: "auto", overscrollBehavior: "auto" },
- }),
+ CELL_EDITOR_THEME,
adjacentEditor.of((direction, col) => session.revealAdjacent(id, direction, col)),
EditorView.contentAttributes.of({
"aria-label": tx(type === "code" ? "Python cell source" : "Markdown cell source"),
diff --git a/src/features/plugins/indexedMarkers.ts b/src/features/plugins/indexedMarkers.ts
index a1772c5a3..7f1caf4e4 100644
--- a/src/features/plugins/indexedMarkers.ts
+++ b/src/features/plugins/indexedMarkers.ts
@@ -83,6 +83,21 @@ export function markersOf(
);
}
+/** Created once: every `EditorView.baseTheme` call mounts new CSS rules into the document. */
+const INDEXED_MARKERS_THEME = EditorView.baseTheme({
+ ".cm-plugin-mapping-button": {
+ border: "none",
+ padding: "0 2px",
+ background: "transparent",
+ color: "inherit",
+ cursor: "pointer",
+ height: "100%",
+ display: "flex",
+ alignItems: "center",
+ },
+ ".cm-plugin-mapping-button:focus-visible": { outline: "1px solid var(--accent)" },
+});
+
export function indexedMarkers(path: string): Extension {
return [
markersField,
@@ -92,19 +107,7 @@ export function indexedMarkers(path: string): Extension {
markers: (view) => view.state.field(markersField),
reserve: (state) => state.field(markersField).size > 0,
}),
- EditorView.baseTheme({
- ".cm-plugin-mapping-button": {
- border: "none",
- padding: "0 2px",
- background: "transparent",
- color: "inherit",
- cursor: "pointer",
- height: "100%",
- display: "flex",
- alignItems: "center",
- },
- ".cm-plugin-mapping-button:focus-visible": { outline: "1px solid var(--accent)" },
- }),
+ INDEXED_MARKERS_THEME,
ViewPlugin.fromClass(
class {
last: FileMarkers | undefined;
diff --git a/src/features/vcs/inlineBlame.ts b/src/features/vcs/inlineBlame.ts
index a08cdef03..8796354c2 100644
--- a/src/features/vcs/inlineBlame.ts
+++ b/src/features/vcs/inlineBlame.ts
@@ -290,6 +290,9 @@ const inlineBlameField = StateField.define({
provide: (field) => EditorView.decorations.from(field),
});
+/** Created once: every `EditorView.baseTheme` call mounts new CSS rules into the document. */
+const INLINE_BLAME_THEME = EditorView.baseTheme(inlineBlameTheme);
+
export function inlineBlame(ctx: EditorDocContext): Extension {
if (ctx.virtual || ctx.readOnly) return [];
const plugin = ViewPlugin.fromClass(
@@ -488,6 +491,6 @@ export function inlineBlame(ctx: EditorDocContext): Extension {
hideOn: (tr) => tr.effects.some((effect) => effect.is(setInlineBlame)),
},
),
- EditorView.baseTheme(inlineBlameTheme),
+ INLINE_BLAME_THEME,
];
}