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 <[email protected]>
Цей коміт міститься в:
audichuang authored and GitHub committed 2026-11-26 12:51:14 +08:00
1 джерело 5886e055b4
коміт daee26e680
6 files changed
+90 -26

No files matched your search

+39
Переглянути файл
@@ -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);
});
+6 -7
Переглянути файл
@@ -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];
}
+17
Переглянути файл
@@ -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 = () => <CellEditor session={session} id="cell-a" source="typed text" type="code" frozen={false} />;
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();
});
});
+8 -5
Переглянути файл
@@ -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"),
+16 -13
Переглянути файл
@@ -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;
+4 -1
Переглянути файл
@@ -290,6 +290,9 @@ const inlineBlameField = StateField.define<DecorationSet>({
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,
];
}