@devries-cameron20 / t3code Mirror · read-only / commits / 0979221336
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 22, 23:15 UTC

perf(web): long composer code blocks no longer re-highlight on every keystroke (#19439)

Co-authored-by: Julius Marminge <julius0216@outlook.com>

MohibShaikh committed Oct 20, 2026 at 09:40 UTC 09792213363d173480546cb012a9738ce9ef2b70
2 files changed +243 -28
apps/web/src/components/composerCodeBlockHighlight.test.ts
+182 -3
@@ -1,7 +1,8 @@
1 import { Editor } from "@tiptap/core";
2 -import type { EditorView } from "@tiptap/pm/view";
2 +import { EditorState, type Plugin, type Transaction } from "@tiptap/pm/state";
3 +import type { DecorationSet, EditorView } from "@tiptap/pm/view";
4 import StarterKit from "@tiptap/starter-kit";
4 -import { afterEach, describe, expect, it, vi } from "vite-plus/test";
5 +import { afterEach, describe, expect, it, onTestFinished, vi } from "vite-plus/test";
6
7 import { ComposerCodeBlockExtension } from "~/composer-rich-text-doc";
8 import type { DiffThemeName } from "~/lib/diffRendering";
@@ -16,7 +17,7 @@ import {
17
18 vi.mock("~/lib/syntaxHighlighting", () => ({ getSyntaxHighlighterPromise: vi.fn() }));
19
19 -const THEME = "github-dark" as DiffThemeName;
20 +const THEME = "pierre-dark" as DiffThemeName;
21
22 function fakeHighlighter(colorFor: (content: string) => string | undefined) {
23 return {
@@ -138,3 +139,181 @@ describe("a highlighter that fails to load", () => {
139 await expect(repainted).resolves.toBeUndefined();
140 });
141 });
142 +
143 +describe("highlighting edited fences", () => {
144 + afterEach(() => {
145 + vi.useRealTimers();
146 + vi.unstubAllGlobals();
147 + });
148 +
149 + async function mountFence(text: string, resolveTheme = () => THEME) {
150 + vi.useFakeTimers();
151 + const highlighter = fakeHighlighter((content) =>
152 + content.startsWith("x") ? "#ff0000" : undefined,
153 + );
154 + const tokenize = vi.spyOn(highlighter, "codeToTokens");
155 + vi.mocked(getSyntaxHighlighterPromise).mockResolvedValue(highlighter);
156 + const editor = new Editor({
157 + extensions: [
158 + StarterKit.configure({ codeBlock: false, trailingNode: false }),
159 + ComposerCodeBlockExtension,
160 + composerCodeBlockHighlight({ resolveTheme }),
161 + ],
162 + content: {
163 + type: "doc",
164 + content: [
165 + { type: "codeBlock", attrs: { language: "ts" }, content: [{ type: "text", text }] },
166 + ],
167 + },
168 + });
169 + vi.stubGlobal("document", { documentElement: {} });
170 + vi.stubGlobal(
171 + "MutationObserver",
172 + class {
173 + observe() {}
174 + disconnect() {}
175 + },
176 + );
177 + const plugin = editor.extensionManager.plugins.find((candidate) =>
178 + (candidate as unknown as { key: string }).key.startsWith("composerCodeBlockHighlight"),
179 + ) as Plugin<DecorationSet>;
180 + // A headless editor holds no plugins in its state, so give this one its own.
181 + let state = EditorState.create({ doc: editor.state.doc, plugins: [plugin] });
182 + // Like a real view, every dispatch updates the plugin's view.
183 + let pluginView: ReturnType<NonNullable<typeof plugin.spec.view>> | undefined;
184 + const view = {
185 + get state() {
186 + return state;
187 + },
188 + dispatch: (transaction: Transaction) => {
189 + const previous = state;
190 + state = state.apply(transaction);
191 + pluginView?.update?.(view, previous);
192 + },
193 + } as unknown as EditorView;
194 + pluginView = plugin.spec.view!(view);
195 + onTestFinished(() => {
196 + pluginView?.destroy?.();
197 + editor.destroy();
198 + });
199 + await vi.advanceTimersByTimeAsync(0);
200 + return {
201 + tokenize,
202 + ranges: () =>
203 + plugin
204 + .getState(state)!
205 + .find()
206 + .map(({ from, to }) => [from, to]),
207 + edit: async (change: (transaction: Transaction) => Transaction) => {
208 + view.dispatch(change(state.tr));
209 + await vi.advanceTimersByTimeAsync(0);
210 + },
211 + colored: () =>
212 + plugin
213 + .getState(state)!
214 + .find()
215 + .map((decoration) => state.doc.textBetween(decoration.from, decoration.to)),
216 + };
217 + }
218 +
219 + it("maps colors while typing above 2,000 characters and refreshes 300 ms after the last edit", async () => {
220 + const fence = await mountFence(`x ${"y".repeat(1_999)}`);
221 + expect(fence.tokenize).toHaveBeenCalledTimes(1);
222 +
223 + await fence.edit((tr) => tr.insertText("x ", 1));
224 + await vi.advanceTimersByTimeAsync(250);
225 + await fence.edit((tr) => tr.insertText("y ", 1));
226 + await vi.advanceTimersByTimeAsync(299);
227 +
228 + expect(fence.tokenize).toHaveBeenCalledTimes(1);
229 + expect(fence.colored()).toEqual(["x"]);
230 + expect(fence.ranges()).toEqual([[5, 6]]);
231 +
232 + await vi.advanceTimersByTimeAsync(1);
233 +
234 + expect(fence.tokenize).toHaveBeenCalledTimes(2);
235 + expect(fence.colored()).toEqual(["x", "x"]);
236 + });
237 +
238 + it("ignores a pending highlight when its text is edited away and restored", async () => {
239 + const fence = await mountFence(`x ${"y".repeat(1_999)}`);
240 + const staleHighlighter = fakeHighlighter(() => "#00ff00");
241 + const staleTokenize = vi.spyOn(staleHighlighter, "codeToTokens");
242 + let finishLoading!: () => void;
243 + vi.mocked(getSyntaxHighlighterPromise).mockImplementationOnce(
244 + () =>
245 + new Promise((resolve) => {
246 + finishLoading = () => resolve(staleHighlighter);
247 + }),
248 + );
249 +
250 + await fence.edit((tr) => tr.insertText("x ", 1));
251 + await vi.advanceTimersByTimeAsync(300);
252 + await fence.edit((tr) => tr.insertText("y ", 1));
253 + await fence.edit((tr) => tr.delete(1, 3));
254 + finishLoading();
255 + await vi.advanceTimersByTimeAsync(0);
256 +
257 + expect(staleTokenize).not.toHaveBeenCalled();
258 + expect(fence.colored()).toEqual(["x"]);
259 + await vi.advanceTimersByTimeAsync(300);
260 + expect(fence.tokenize).toHaveBeenCalledTimes(2);
261 + expect(fence.colored()).toEqual(["x", "x"]);
262 + });
263 +
264 + it("refreshes a long fence immediately when the theme changes during a pending edit", async () => {
265 + let theme = THEME;
266 + const fence = await mountFence(`x ${"y".repeat(1_999)}`, () => theme);
267 + await fence.edit((tr) => tr.insertText("x ", 1));
268 +
269 + theme = "pierre-light";
270 + await fence.edit((tr) => tr.setMeta("theme", true));
271 +
272 + expect(fence.tokenize).toHaveBeenCalledTimes(2);
273 + expect(fence.tokenize).toHaveBeenLastCalledWith(expect.any(String), { lang: "ts", theme });
274 + expect(fence.colored()).toEqual(["x", "x"]);
275 + await vi.advanceTimersByTimeAsync(300);
276 + expect(fence.tokenize).toHaveBeenCalledTimes(2);
277 + });
278 +
279 + it("refreshes immediately when an edit leaves the fence at 2,000 characters", async () => {
280 + const fence = await mountFence(`x ${"y".repeat(1_996)}`);
281 +
282 + await fence.edit((tr) => tr.insertText("x ", 1));
283 +
284 + expect(fence.tokenize).toHaveBeenCalledTimes(2);
285 + expect(fence.colored()).toEqual(["x", "x"]);
286 + });
287 +
288 + it("clips a split token to its fences without coloring the paragraph between them", async () => {
289 + const fence = await mountFence("x".repeat(6_000));
290 +
291 + await fence.edit((tr) =>
292 + tr
293 + .split(3_001)
294 + .insert(
295 + 3_002,
296 + tr.doc.type.schema.node("paragraph", null, [tr.doc.type.schema.text("plain")]),
297 + ),
298 + );
299 +
300 + expect(fence.tokenize).toHaveBeenCalledTimes(1);
301 + expect(fence.ranges()).toEqual([
302 + [1, 3_001],
303 + [3_010, 6_010],
304 + ]);
305 + expect(fence.colored()).toEqual(["x".repeat(3_000), "x".repeat(3_000)]);
306 + });
307 +
308 + it("clears retained colors when the new language highlighter fails to load", async () => {
309 + const fence = await mountFence(`x ${"y".repeat(1_999)}`);
310 + vi.mocked(getSyntaxHighlighterPromise).mockRejectedValue(new Error("Shiki failed to load"));
311 +
312 + await fence.edit((tr) => tr.setNodeMarkup(0, undefined, { language: "py" }));
313 + expect(fence.colored()).toEqual(["x"]);
314 +
315 + await vi.advanceTimersByTimeAsync(300);
316 +
317 + expect(fence.colored()).toEqual([]);
318 + });
319 +});
apps/web/src/components/composerCodeBlockHighlight.ts
+61 -25
@@ -14,13 +14,13 @@ interface HighlightedBlock {
14
15 const composerCodeBlockHighlightKey = new PluginKey<DecorationSet>("composerCodeBlockHighlight");
16
17 -/**
18 - * Any edit changes a block's signature, so each keystroke re-tokenizes the
19 - * whole block synchronously on the main thread: a few milliseconds for a couple
20 - * of hundred lines, but ~80ms at a thousand, which stalls typing.
21 - */
17 +// Tokenizing larger fences can stall the main thread.
18 export const MAX_HIGHLIGHTED_CODE_BLOCK_LENGTH = 20_000;
19
20 +// Keep mapped colors while typing in long fences, then refresh after a pause.
21 +const DEFERRED_HIGHLIGHT_CODE_BLOCK_LENGTH = 2_000;
22 +const DEFERRED_HIGHLIGHT_DELAY_MS = 300;
23 +
24 export function shouldHighlightCodeBlock(code: string): boolean {
25 return code.length <= MAX_HIGHLIGHTED_CODE_BLOCK_LENGTH;
26 }
@@ -63,10 +63,11 @@ export function composerCodeBlockHighlight(options: {
63 state: {
64 init: () => DecorationSet.empty,
65 apply(transaction, value, _oldState, newState) {
66 + const mapped = value.map(transaction.mapping, transaction.doc);
67 if (!transaction.docChanged && !transaction.getMeta(composerCodeBlockHighlightKey)) {
67 - return value.map(transaction.mapping, transaction.doc);
68 + return mapped;
69 }
69 - return buildDecorations(newState, cache, options.resolveTheme());
70 + return buildDecorations(newState, cache, options.resolveTheme(), mapped);
71 },
72 },
73
@@ -80,29 +81,41 @@ export function composerCodeBlockHighlight(options: {
81 let disposed = false;
82 let paintedTheme = options.resolveTheme();
83 let scannedDoc: ProseMirrorNode | null = null;
84 + let deferredRefresh: ReturnType<typeof setTimeout> | undefined;
85
84 - /**
85 - * Returning early when nothing is pending is what stops the
86 - * dispatch below from re-triggering this on its own update.
87 - */
88 - const refresh = () => {
86 + // Repaint dispatches and selection changes must not restart the debounce.
87 + const refresh = (deferLong: boolean) => {
88 const theme = options.resolveTheme();
89 const themeChanged = theme !== paintedTheme;
90 paintedTheme = theme;
91 // A selection change cannot alter what needs tokenizing, and
92 // building a signature means concatenating every block's text.
93 if (!themeChanged && view.state.doc === scannedDoc) return;
94 + clearTimeout(deferredRefresh);
95 scannedDoc = view.state.doc;
96 const blocks = collectCodeBlocks(view.state);
97 - live = new Set(blocks.map(({ node }) => blockSignature(node, theme)));
97 + const signatures = new Set(blocks.map(({ node }) => blockSignature(node, theme)));
98 + live = signatures;
99 for (const signature of cache.keys()) {
100 if (!live.has(signature)) cache.delete(signature);
101 }
101 - const pending = blocks.filter(
102 + const missing = blocks.filter(
103 ({ node }) =>
104 shouldHighlightCodeBlock(node.textContent) &&
105 !cache.has(blockSignature(node, theme)),
106 );
107 + const pending =
108 + deferLong && !themeChanged
109 + ? missing.filter(
110 + ({ node }) => node.textContent.length <= DEFERRED_HIGHLIGHT_CODE_BLOCK_LENGTH,
111 + )
112 + : missing;
113 + if (pending.length < missing.length) {
114 + deferredRefresh = setTimeout(() => {
115 + scannedDoc = null;
116 + refresh(false);
117 + }, DEFERRED_HIGHLIGHT_DELAY_MS);
118 + }
119 if (pending.length === 0) {
120 if (themeChanged) {
121 view.dispatch(view.state.tr.setMeta(composerCodeBlockHighlightKey, true));
@@ -117,31 +130,36 @@ export function composerCodeBlockHighlight(options: {
130 const language =
131 languageOfInfoString(String(node.attrs.language ?? "")) || "text";
132 const signature = blockSignature(node, theme);
120 - const highlighter = await getSyntaxHighlighterPromise(language);
121 - if (disposed || cache.has(signature) || !live.has(signature)) return;
133 + // Cache failure as plain text to clear retained colors.
134 + const highlighter = await getSyntaxHighlighterPromise(language).catch(() => null);
135 + // An older request must not bypass the debounce if its text returns.
136 + if (disposed || live !== signatures || cache.has(signature)) return;
137 cache.set(signature, {
138 signature,
124 - decorations: tokenizeBlock(highlighter, node.textContent, language, theme),
139 + decorations: highlighter
140 + ? tokenizeBlock(highlighter, node.textContent, language, theme)
141 + : [],
142 });
143 }),
144 ).then(() => {
128 - if (disposed) return;
145 + if (disposed || live !== signatures) return;
146 view.dispatch(view.state.tr.setMeta(composerCodeBlockHighlightKey, true));
147 });
148 };
149
133 - refresh();
150 + refresh(false);
151 // The theme lives on <html>, outside the editor, so nothing would
152 // otherwise tell the view a repaint is due.
136 - const themeObserver = new MutationObserver(refresh);
153 + const themeObserver = new MutationObserver(() => refresh(false));
154 themeObserver.observe(document.documentElement, {
155 attributeFilter: ["class"],
156 });
157
158 return {
142 - update: refresh,
159 + update: () => refresh(true),
160 destroy() {
161 disposed = true;
162 + clearTimeout(deferredRefresh);
163 themeObserver.disconnect();
164 },
165 };
@@ -187,12 +205,27 @@ function buildDecorations(
205 state: EditorState,
206 cache: Map<string, HighlightedBlock>,
207 theme: DiffThemeName,
208 + previous: DecorationSet,
209 ): DecorationSet {
210 const decorations: Decoration[] = [];
211 for (const { node, pos } of collectCodeBlocks(state)) {
212 if (!shouldHighlightCodeBlock(node.textContent)) continue;
213 const highlighted = cache.get(blockSignature(node, theme));
195 - if (!highlighted) continue;
214 + if (!highlighted) {
215 + // A mapped token can span split fences and the paragraph between them.
216 + const start = pos + 1;
217 + const end = start + node.content.size;
218 + for (const decoration of previous.find(start, end)) {
219 + if (decoration.from >= start && decoration.to <= end) {
220 + decorations.push(decoration);
221 + continue;
222 + }
223 + const from = Math.max(decoration.from, start);
224 + const to = Math.min(decoration.to, end);
225 + if (from < to) decorations.push(colorDecoration(from, to, decoration.spec.color));
226 + }
227 + continue;
228 + }
229 // Token offsets index the block's text, which lines up with document
230 // positions only because `codeBlock` is `text*` with no marks — one
231 // unmarked text node, so `content.size === textContent.length`. Allowing
@@ -201,11 +234,14 @@ function buildDecorations(
234 const start = pos + 1;
235 for (const decoration of highlighted.decorations) {
236 decorations.push(
204 - Decoration.inline(start + decoration.from, start + decoration.to, {
205 - style: `color:${decoration.color}`,
206 - }),
237 + colorDecoration(start + decoration.from, start + decoration.to, decoration.color),
238 );
239 }
240 }
241 return DecorationSet.create(state.doc, decorations);
242 }
243 +
244 +// Keep the color available when clipping mapped decorations to a fence.
245 +function colorDecoration(from: number, to: number, color: string): Decoration {
246 + return Decoration.inline(from, to, { style: `color:${color}` }, { color });
247 +}