@devries-cameron20 / t3code-1 / commits / 2c0a78a0d0
Fork 0

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

fix(web): collapsed diff files defer highlighting until opened (#16027)

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

Stevan Borus committed Oct 20, 2026 at 02:10 UTC 2c0a78a0d0dfe90a3081466bdc638a9cc6076f39
3 files changed +221 -6
apps/web/src/components/diffs/StyledDiffCodeView.browser.test.tsx
+156
@@ -1,11 +1,16 @@
1 import {
2 + CodeView,
3 + FileDiff,
4 FileRenderer,
5 getSharedHighlighter,
6 + type CodeViewItem,
7 type CodeViewScrollTarget,
8 type FileContents,
9 } from "@pierre/diffs";
10 import { useWorkerPool, type CodeViewProps } from "@pierre/diffs/react";
11 import { WorkerPoolManager, type WorkerRequest, type WorkerResponse } from "@pierre/diffs/worker";
12 +import DiffsWorker from "@pierre/diffs/worker/worker.js?worker";
13 +import { getDiffLineStat, getRenderablePatch } from "../../lib/diffRendering";
14 import {
15 act,
16 StrictMode,
@@ -305,6 +310,157 @@ describe("code-view worker lifecycle", () => {
310 });
311 });
312
313 +describe("collapsed diff content", () => {
314 + let pool: WorkerPoolManager;
315 + let container: HTMLDivElement;
316 + let viewer: FileDiff<string> | CodeView<string> | undefined;
317 +
318 + const loadedDiff = () => {
319 + const parsed = getRenderablePatch(
320 + [
321 + "diff --git a/before.ts b/after.ts",
322 + "similarity index 50%",
323 + "rename from before.ts",
324 + "rename to after.ts",
325 + "--- a/before.ts",
326 + "+++ b/after.ts",
327 + "@@ -1,2 +1,2 @@",
328 + " const shared = 1;",
329 + "-const before = 2;",
330 + "+const after = 3;",
331 + ].join("\n"),
332 + "collapsed-content",
333 + );
334 + if (parsed?.kind !== "files" || !parsed.files[0]) throw new Error("Expected diff");
335 + return parsed.files[0];
336 + };
337 + const code = () =>
338 + [...container.querySelectorAll("diffs-container")]
339 + .flatMap((element) => [...(element.shadowRoot?.querySelectorAll("[data-line]") ?? [])])
340 + .map((line) => line.textContent)
341 + .join("\n");
342 +
343 + beforeEach(async () => {
344 + testState.requests = [];
345 + testState.failWorkers = false;
346 + testState.holdInitialization = false;
347 + pool = new WorkerPoolManager(
348 + { workerFactory: () => new DiffsWorker(), poolSize: 1 },
349 + { theme: "pierre-dark", langs: ["typescript"], preferredHighlighter: "shiki-wasm" },
350 + );
351 + await pool.initialize();
352 + container = document.createElement("div");
353 + container.style.cssText = "height: 400px; width: 800px; overflow: auto";
354 + document.body.append(container);
355 + });
356 +
357 + afterEach(() => {
358 + viewer?.cleanUp();
359 + viewer = undefined;
360 + pool.terminate();
361 + container.remove();
362 + vi.restoreAllMocks();
363 + });
364 +
365 + it("defers plain AST and highlight work across collapsed renders until expansion", async () => {
366 + const fileDiff = loadedDiff();
367 + const highlight = vi.spyOn(pool, "highlightDiffAST");
368 + const plainAST = vi.spyOn(pool, "getPlainDiffAST");
369 + const options = { theme: "pierre-dark", collapsed: true, disableErrorHandling: true } as const;
370 + const file = new FileDiff<string>(options, pool);
371 + viewer = file;
372 + file.render({ fileDiff, containerWrapper: container });
373 + file.render({ fileDiff, forceRender: true });
374 + expect.soft(highlight.mock.calls.length).toBe(0);
375 + expect.soft(plainAST.mock.calls.length).toBe(0);
376 + expect(code()).toBe("");
377 + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain(
378 + "after.ts",
379 + );
380 +
381 + file.setOptions({ ...options, collapsed: false });
382 + file.render({ fileDiff, forceRender: true });
383 + expect(highlight).toHaveBeenCalled();
384 + await pool.primeDiffHighlightCache(fileDiff);
385 + await vi.waitFor(() => expect(code()).toContain("const after = 3;"));
386 + expect(code()).toContain("const before = 2;");
387 + expect(testState.requests.filter((request) => request.type === "diff")).toHaveLength(1);
388 + });
389 +
390 + it("finds newly loaded collapsed lines and unfolds the match with its annotation and header", async () => {
391 + const fileDiff = loadedDiff();
392 + const item: CodeViewItem<string> = {
393 + type: "diff",
394 + id: "renamed",
395 + fileDiff,
396 + collapsed: true,
397 + annotations: [{ side: "additions", lineNumber: 2, metadata: "Review this change" }],
398 + };
399 + const view = new CodeView<string>(
400 + {
401 + theme: "pierre-dark",
402 + disableErrorHandling: true,
403 + onRevealSearchMatch: (match) =>
404 + view.setItems([{ ...match, collapsed: false, version: (match.version ?? 0) + 1 }]),
405 + renderHeaderMetadata: (file) => {
406 + if (!("hunks" in file)) return;
407 + const metadata = document.createElement("span");
408 + const { additions, deletions } = getDiffLineStat([file]);
409 + metadata.textContent = "+" + additions + " / -" + deletions;
410 + return metadata;
411 + },
412 + renderAnnotation: (annotation) => {
413 + const note = document.createElement("span");
414 + note.textContent = annotation.metadata ?? "";
415 + return note;
416 + },
417 + },
418 + pool,
419 + );
420 + viewer = view;
421 + view.setup(container);
422 + view.openSearch();
423 + const search = container.querySelector("[data-diffs-search]")!.shadowRoot!;
424 + const input = search.querySelector<HTMLInputElement>("input[data-search]")!;
425 + input.value = "const after";
426 + input.dispatchEvent(new Event("input", { bubbles: true }));
427 + view.setItems([item]);
428 + view.render(true);
429 + expect(code()).toBe("");
430 + expect(search.textContent).toContain("1 results");
431 + expect(container.textContent).toContain("+1 / -1");
432 +
433 + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
434 + view.render(true);
435 + await vi.waitFor(() => expect(code()).toContain("const after = 3;"));
436 + expect(search.textContent).toContain("1 of 1");
437 + expect(container.textContent).toContain("Review this change");
438 + expect(container.textContent).toContain("+1 / -1");
439 + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain(
440 + "after.ts",
441 + );
442 +
443 + await pool.primeDiffHighlightCache(fileDiff);
444 + view.render(true);
445 + const highlight = vi.spyOn(pool, "highlightDiffAST");
446 + const plainAST = vi.spyOn(pool, "getPlainDiffAST");
447 + const updatedDiff = { ...fileDiff, name: "updated.ts", cacheKey: "collapsed-update" };
448 + view.setItems([{ ...item, fileDiff: updatedDiff, version: 2 }]);
449 + view.render(true);
450 + expect(highlight).not.toHaveBeenCalled();
451 + expect(plainAST).not.toHaveBeenCalled();
452 + expect(code()).toBe("");
453 + expect(search.textContent).toContain("1 of 1");
454 + expect(container.querySelector("diffs-container")?.shadowRoot?.textContent).toContain(
455 + "updated.ts",
456 + );
457 + input.dispatchEvent(new KeyboardEvent("keydown", { key: "Enter", bubbles: true }));
458 + view.render(true);
459 + await vi.waitFor(() => expect(code()).toContain("const after = 3;"));
460 + expect(container.textContent).toContain("Review this change");
461 + });
462 +});
463 +
464 type RevealHandle = NonNullable<Parameters<typeof useCodeViewFileReveal>[0]>;
465 const revealCalls: {
466 scope: string;
patches/@pierre%2Fdiffs@1.5.2.patch
+59
@@ -694,6 +694,43 @@ index 0000000..29370ad
694 +export { CodeViewSearch, collectMatches };
695 +
696 +//# sourceMappingURL=CodeViewSearch.js.map
697 +diff --git a/dist/components/FileDiff.js b/dist/components/FileDiff.js
698 +index 6f66bd3..3125fac 100644
699 +--- a/dist/components/FileDiff.js
700 ++++ b/dist/components/FileDiff.js
701 +@@ -1,5 +1,6 @@
702 +-import { CUSTOM_HEADER_SLOT_ID, DEFAULT_THEMES, EMPTY_RENDER_RANGE, HEADER_FILENAME_SUFFIX_SLOT_ID, HEADER_METADATA_SLOT_ID, HEADER_PREFIX_SLOT_ID } from "../constants.js";
703 ++import { CUSTOM_HEADER_SLOT_ID, DEFAULT_THEMES, HEADER_FILENAME_SUFFIX_SLOT_ID, HEADER_METADATA_SLOT_ID, HEADER_PREFIX_SLOT_ID } from "../constants.js";
704 + import { getThemes } from "../utils/getThemes.js";
705 ++import { getHighlighterThemeStyles } from "../utils/getHighlighterThemeStyles.js";
706 + import { getHighlighterIfLoaded, getSharedHighlighter } from "../highlighter/shared_highlighter.js";
707 + import { dequeueRender, queueRender } from "../managers/UniversalRenderingManager.js";
708 + import { areThemesEqual } from "../utils/areThemesEqual.js";
709 +@@ -691,11 +692,20 @@ var FileDiff = class {
710 + this.removeRenderedCode();
711 + this.clearAuxiliaryNodes();
712 + try {
713 +- const hunksResult = this.hunksRenderer.renderDiff(latestDiff, EMPTY_RENDER_RANGE);
714 +- if (hunksResult != null) this.applyThemeState(fileContainer, hunksResult.themeStyles, themeType, hunksResult.baseThemeType);
715 +- if (hunksResult?.headerElement != null) this.applyHeaderToDOM(hunksResult.headerElement, fileContainer, hunksResult.fileDiff);
716 ++ // An empty render range still highlights the full diff. Headers only need the theme.
717 ++ const theme = this.getTheme();
718 ++ const highlighter = getHighlighterIfLoaded({ theme, lang: "text" });
719 ++ if (highlighter != null) this.applyThemeState(fileContainer, getHighlighterThemeStyles({ theme, highlighter }), themeType, typeof theme === "string" ? highlighter.getTheme(theme).type : void 0);
720 ++ else getSharedHighlighter({
721 ++ themes: getThemes(theme),
722 ++ langs: ["text"],
723 ++ preferredHighlighter: this.workerManager?.getPreferredHighlighter() ?? this.options.preferredHighlighter
724 ++ }).then(() => {
725 ++ if (this.enabled && this.fileContainer === fileContainer && this.options.collapsed && areThemesEqual(theme, this.getTheme())) this.rerender();
726 ++ }).catch((error) => console.error(error));
727 ++ if (!disableFileHeader) this.applyHeaderToDOM(this.hunksRenderer.renderHeader(latestDiff), fileContainer, latestDiff);
728 + this.renderSeparators([]);
729 +- this.renderedDiff = hunksResult?.fileDiff ?? latestDiff;
730 ++ this.renderedDiff = latestDiff;
731 + this.injectUnsafeCSS();
732 + } catch (error) {
733 + if (disableErrorHandling) throw error;
734 diff --git a/dist/components/VirtualizedFile.d.ts b/dist/components/VirtualizedFile.d.ts
735 index 5e68e35..79dcd88 100644
736 --- a/dist/components/VirtualizedFile.d.ts
@@ -773,6 +810,28 @@ index d1f57cd..5908ad7 100644
810 const file = this.getRenderedFile();
811 if (!this.isSimpleMode()) this.computeApproximateSize(true);
812 else if (shouldUpdateBuffer && previousRenderRange != null && file != null) {
813 +diff --git a/dist/components/VirtualizedFileDiff.js b/dist/components/VirtualizedFileDiff.js
814 +index 96f970f..1bb59c6 100644
815 +--- a/dist/components/VirtualizedFileDiff.js
816 ++++ b/dist/components/VirtualizedFileDiff.js
817 +@@ -552,7 +552,7 @@ var VirtualizedFileDiff = class extends FileDiff {
818 + rerender() {
819 + if (!this.enabled || this.fileDiff == null && this.additionFile == null && this.deletionFile == null) return;
820 + const latestDiff = this.getLatestDiff();
821 +- const nextRenderDiff = latestDiff == null ? void 0 : this.hunksRenderer.getDiffForNextRender(latestDiff);
822 ++ const nextRenderDiff = latestDiff == null || this.options.collapsed === true ? latestDiff : this.hunksRenderer.getDiffForNextRender(latestDiff);
823 + if (this.isSimpleMode()) this.pendingRender = void 0;
824 + this.forceRenderOverride = true;
825 + this.virtualizer.instanceChanged(this, !areDiffTargetsEqual(this.getRenderedDiff(), nextRenderDiff));
826 +@@ -724,7 +724,7 @@ var VirtualizedFileDiff = class extends FileDiff {
827 + const previousRenderedDiff = this.getRenderedDiff();
828 + const { pendingRender } = this;
829 + const previousLayoutDiff = pendingRender?.diff ?? previousRenderedDiff;
830 +- const pendingRenderDiff = this.hunksRenderer.getDiffForNextRender(latestDiff);
831 ++ const pendingRenderDiff = this.options.collapsed === true ? latestDiff : this.hunksRenderer.getDiffForNextRender(latestDiff);
832 + this.pendingRender = {
833 + latestDiff,
834 + diff: pendingRenderDiff
835 diff --git a/dist/components/web-components.js b/dist/components/web-components.js
836 index 6e7b486..5b7290f 100644
837 --- a/dist/components/web-components.js
pnpm-lock.yaml
+6 -6
@@ -170,7 +170,7 @@ patchedDependencies:
170 '@expo/ui@58.0.20': 5a26480373850c1a7943919aa21f904151b335a0cb01042109e0746def37923e
171 '@ff-labs/fff-node@0.11.0': cc71fcec015f4d39bc96ae2e2bf6b0ca678124c722c66a7841efce9d34b5983c
172 '@legendapp/list@3.3.5': 680cc6a5c5b4a4032e467e7b3fde22f89a84c0ee2e6eac6fda737d6277cc0806
173 - '@pierre/diffs@1.5.2': 298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7
173 + '@pierre/diffs@1.5.2': 2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc
174 '@react-native-ai/apple@0.12.0': 2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d
175 '@react-native-menu/menu@2.0.0': a4866ecfcd44f318043de839d20110c17e14909004900f002ffe12050376f2ba
176 '@react-navigation/native-stack@7.21.0': caae6552051bce91a85f567f3a08f9c8d25ce4c53f65a9391a67f016bb1d798e
@@ -355,7 +355,7 @@ importers:
355 version: 1.9.1
356 '@pierre/diffs':
357 specifier: 'catalog:'
358 - version: 1.5.2(patch_hash=298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
358 + version: 1.5.2(patch_hash=2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
359 '@react-native-ai/apple':
360 specifier: 0.12.0
361 version: 0.12.0(patch_hash=2d09870c2848d185cb05b53ed823a46e12dba519324d8dd8e584e28731990f9d)(react-native@0.88.0(@babel/core@7.29.7)(@react-native/metro-config@0.88.0(@babel/core@7.29.7)(bufferutil@4.1.0)(utf-8-validate@6.0.6))(@types/react@19.3.0)(bufferutil@4.1.0)(react@19.3.0)(utf-8-validate@6.0.6))
@@ -843,7 +843,7 @@ importers:
843 version: 1.8.0
844 '@pierre/diffs':
845 specifier: 'catalog:'
846 - version: 1.5.2(patch_hash=298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
846 + version: 1.5.2(patch_hash=2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
847 '@pierre/trees':
848 specifier: 1.0.0-beta.6
849 version: 1.0.0-beta.6(@pierre/theme@1.1.0)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0)
@@ -1137,7 +1137,7 @@ importers:
1137 dependencies:
1138 '@pierre/diffs':
1139 specifier: 'catalog:'
1140 - version: 1.5.2(patch_hash=298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
1140 + version: 1.5.2(patch_hash=2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)
1141 '@t3tools/contracts':
1142 specifier: workspace:*
1143 version: link:../contracts
@@ -16163,7 +16163,7 @@ snapshots:
16163 tslib: 2.8.1
16164 webcrypto-core: 1.9.2
16165
16166 - '@pierre/diffs@1.5.2(patch_hash=298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)':
16166 + '@pierre/diffs@1.5.2(patch_hash=2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)':
16167 dependencies:
16168 '@pierre/theme': 2.0.0
16169 '@pierre/theming': 1.0.1(@pierre/theme@2.0.0)(@shikijs/themes@4.2.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0)
@@ -16178,7 +16178,7 @@ snapshots:
16178 transitivePeerDependencies:
16179 - '@shikijs/themes'
16180
16181 - '@pierre/diffs@1.5.2(patch_hash=298e94372dbd51846a4e26ece2ecc3e22eb1f94b0450b1f1a9c6fec127809be7)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)':
16181 + '@pierre/diffs@1.5.2(patch_hash=2ec81ba1e8ce4681095a3473c2bf78e630417740c622720ef624febb467c25fc)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)':
16182 dependencies:
16183 '@pierre/theme': 2.0.0
16184 '@pierre/theming': 1.0.1(@pierre/theme@2.0.0)(@shikijs/themes@4.3.0)(react-dom@19.3.0(react@19.3.0))(react@19.3.0)(shiki@4.2.0)