Skip to content

Commit d228f1f

Browse files
authored
perf(web): highlight code with @pierre/highlights instead of Shiki (#23178)
1 parent edb2b2f commit d228f1f

14 files changed

Lines changed: 304 additions & 225 deletions

‎apps/web/src/components/chat/HighlightedCodeLines.tsx‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
1-
import type { DiffsHighlighter } from "@pierre/diffs";
21
import { toHtml } from "hast-util-to-html";
32
import { toJsxRuntime } from "hast-util-to-jsx-runtime";
43
import { cloneElement, isValidElement, memo, type DOMAttributes } from "react";
54
import { Fragment, jsx, jsxs } from "react/jsx-runtime";
65

7-
type HighlightedRoot = ReturnType<DiffsHighlighter["codeToHast"]>;
6+
import type { highlightCodeToHast } from "../../lib/incrementalHighlighting";
7+
8+
type HighlightedRoot = ReturnType<typeof highlightCodeToHast>;
89
type HighlightedNode = HighlightedRoot["children"][number];
910
const runtime = { Fragment, jsx, jsxs };
1011

‎apps/web/src/components/chat/MarkdownCodeBlock.tsx‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@ import React, { use, useEffect, useRef, useState, type ReactNode } from "react";
55

66
import { getClientSettings } from "../../hooks/useSettings";
77
import type { DiffThemeName } from "../../lib/diffRendering";
8-
import { createIncrementalHighlightedDocument } from "../../lib/incrementalHighlighting";
8+
import {
9+
createIncrementalHighlightedDocument,
10+
highlightCodeToHast,
11+
} from "../../lib/incrementalHighlighting";
912
import { LRUCache } from "../../lib/lruCache";
1013
import { getSyntaxHighlighterPromise } from "../../lib/syntaxHighlighting";
1114
import { cn } from "../../lib/utils";
@@ -353,19 +356,20 @@ function UncachedShikiCodeBlock({
353356
const incrementalHighlight = preserveLines
354357
? createIncrementalHighlightedDocument(highlighter, language, themeName)
355358
: null;
359+
useEffect(() => () => incrementalHighlight?.dispose(), [incrementalHighlight]);
356360
const highlighted = (() => {
357361
try {
358362
if (incrementalHighlight) return incrementalHighlight(code);
359363
return preserveLines
360-
? highlighter.codeToHast(code, { lang: language, theme: themeName })
364+
? highlightCodeToHast(highlighter, code, language, themeName)
361365
: highlighter.codeToHtml(code, { lang: language, theme: themeName });
362366
} catch (error) {
363367
console.warn(
364368
`Code highlighting failed for language "${language}", falling back to plain text.`,
365369
error instanceof Error ? error.message : error,
366370
);
367371
return preserveLines
368-
? highlighter.codeToHast(code, { lang: "text", theme: themeName })
372+
? highlightCodeToHast(highlighter, code, "text", themeName)
369373
: highlighter.codeToHtml(code, { lang: "text", theme: themeName });
370374
}
371375
})();

‎apps/web/src/components/diffs/AnnotatableCodeView.browser.test.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ vi.mock("./StyledDiffCodeView", () => ({
4040
) : null}
4141
{item.annotations?.map((annotation) => (
4242
<div key={`${annotation.side}:${annotation.lineNumber}`}>
43-
{props.renderAnnotation?.(annotation, item)}
43+
{props.renderAnnotation?.(annotation, item.fileDiff, item)}
4444
</div>
4545
))}
4646
</section>

‎apps/web/src/components/diffs/AnnotatableCodeView.tsx‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ export function AnnotatableCodeView({
285285
? renderHeaderPrefix(item.fileDiff, item.id, item.collapsed === true)
286286
: null
287287
}
288-
renderAnnotation={(annotation) => {
288+
renderAnnotation={(...[annotation]) => {
289289
const hasDraft = annotation.metadata.entries.some((entry) => entry.kind === "draft");
290290
return (
291291
<div

‎apps/web/src/components/diffs/useReviewFilePatches.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ export function useReviewFilePatches({
111111
hunks: [],
112112
additionLines: [],
113113
deletionLines: [],
114+
additionCharacterCount: 0,
115+
deletionCharacterCount: 0,
114116
splitLineCount: 0,
115117
unifiedLineCount: 0,
116118
isPartial: true,

‎apps/web/src/components/pullRequest/PullRequestCodeTab.tsx‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
sourceControlClients,
44
} from "@t3tools/client-runtime/source-control-clients";
55
import type { CodeViewItem, DiffLineAnnotation, SelectedLineRange } from "@pierre/diffs";
6-
import type { CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react";
6+
import type { CodeViewAnnotationArgs, CodeViewDiffItem, CodeViewHandle } from "@pierre/diffs/react";
77
import type {
88
EnvironmentId,
99
PullRequestDetailView,
@@ -934,7 +934,7 @@ function PullRequestCodeTab({
934934
/>
935935
);
936936

937-
const renderAnnotation = (annotation: ReviewAnnotation) => (
937+
const renderAnnotation = (...[annotation]: CodeViewAnnotationArgs<ReviewAnnotationGroup>) => (
938938
<div className="py-1 font-sans text-foreground">
939939
{annotation.metadata.threads.map(renderThreadCard)}
940940
{annotation.metadata.pending.map((comment) => (

‎apps/web/src/components/settings/CaptureShortcutConfig.tsx‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import * as Cause from "effect/Cause";
1212
import { useState } from "react";
1313
import { getDesktopSnapShotBridge } from "../../lib/desktopSnapShot";
1414
import { resolveDiffThemeName } from "../../lib/diffRendering";
15+
import { PREFERRED_HIGHLIGHTER } from "../../lib/syntaxHighlighting";
1516
import { useRenderedAppearance } from "../../hooks/useTheme";
1617
import { useCopyToClipboard } from "../../hooks/useCopyToClipboard";
1718
import { Button } from "../ui/button";
@@ -181,6 +182,7 @@ export function CaptureShortcutConfig({
181182
diffStyle: "unified",
182183
theme: resolveDiffThemeName(resolvedTheme),
183184
overflow: "wrap",
185+
preferredHighlighter: PREFERRED_HIGHLIGHTER,
184186
}}
185187
/>
186188
</div>

‎apps/web/src/index.css‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1721,7 +1721,7 @@ code {
17211721
inherited sizes on unrelated pre surfaces (terminal previews, approvals).
17221722
Inline code stays relative to its sentence so it never towers over prose;
17231723
diffs and file previews take the size through --diffs-font-size. */
1724-
.chat-markdown .chat-markdown-shiki .shiki,
1724+
.chat-markdown .chat-markdown-shiki :is(.shiki, .diffs),
17251725
.chat-markdown .chat-markdown-codeblock pre,
17261726
:is(.chat-markdown, .composer-tiptap) pre code {
17271727
font-size: var(--font-size-code, inherit);
@@ -2100,7 +2100,7 @@ code {
21002100
overflow-wrap: anywhere;
21012101
}
21022102

2103-
:is(.chat-markdown, .composer-tiptap) .chat-markdown-shiki .shiki {
2103+
:is(.chat-markdown, .composer-tiptap) .chat-markdown-shiki :is(.shiki, .diffs) {
21042104
background: transparent !important;
21052105
}
21062106

Lines changed: 82 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { toHtml } from "hast-util-to-html";
2-
import { getSharedHighlighter } from "@pierre/diffs";
3-
import { describe, expect, it } from "vite-plus/test";
2+
import { disposeHighlighter, getSharedHighlighter } from "@pierre/diffs";
3+
import { afterAll, beforeAll, describe, expect, it } from "vite-plus/test";
44

55
import { createIncrementalHighlightedDocument } from "./incrementalHighlighting";
66

@@ -18,65 +18,91 @@ const samples = {
1818
sql: "SELECT 'multi\nline'\nFROM table_name;\n",
1919
} as const;
2020

21-
const highlighterPromise = getSharedHighlighter({
21+
const highlighterOptions = {
2222
langs: Object.keys(samples) as Array<keyof typeof samples>,
2323
themes: ["pierre-dark", "pierre-light"],
24-
preferredHighlighter: "shiki-wasm",
25-
});
24+
};
2625

27-
describe("incremental code highlighting", () => {
28-
it.each(Object.entries(samples))(
29-
"matches full HTML at every streaming prefix in %s",
30-
async (language, code) => {
31-
const highlighter = await highlighterPromise;
32-
for (const theme of ["pierre-dark", "pierre-light"] as const) {
33-
const highlight = createIncrementalHighlightedDocument(highlighter, language, theme);
34-
for (let end = 0; end <= code.length; end++) {
35-
const text = code.slice(0, end);
36-
expect(toHtml(highlight(text)), `${theme}, prefix ${end}`).toBe(
37-
highlighter.codeToHtml(text, { lang: language, theme }),
38-
);
26+
describe.each(["shiki-wasm", "highlights"] as const)(
27+
"incremental code highlighting (%s)",
28+
(engine) => {
29+
beforeAll(() => disposeHighlighter());
30+
afterAll(() => disposeHighlighter());
31+
it.each(Object.entries(samples))(
32+
"matches full HTML at every streaming prefix in %s",
33+
async (language, code) => {
34+
const highlighter = await getSharedHighlighter({
35+
...highlighterOptions,
36+
preferredHighlighter: engine,
37+
});
38+
for (const theme of ["pierre-dark", "pierre-light"] as const) {
39+
const highlight = createIncrementalHighlightedDocument(highlighter, language, theme);
40+
for (let end = 0; end <= code.length; end++) {
41+
const text = code.slice(0, end);
42+
expect(toHtml(highlight(text)), `${theme}, prefix ${end}`).toBe(
43+
highlighter.codeToHtml(text, { lang: language, theme }),
44+
);
45+
}
46+
highlight.dispose();
3947
}
40-
}
41-
},
42-
);
43-
44-
it("resets after edits and truncation, including edits to a completed line", async () => {
45-
const highlighter = await highlighterPromise;
46-
const highlight = createIncrementalHighlightedDocument(
47-
highlighter,
48-
"typescript",
49-
"pierre-dark",
48+
},
5049
);
51-
const inputs = [
52-
"/* open\ncomment\n",
53-
"/* open\ncomment\n*/\nconst x = 1;",
54-
"const edited = 2;\nconst x = 1;",
55-
"const edited = 2;\nconst x = 10;",
56-
"const edited = 2;\n",
57-
"",
58-
"\n\n\nconst fresh = true;\n",
59-
];
60-
for (const text of inputs) {
61-
expect(toHtml(highlight(text))).toBe(
62-
highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }),
50+
51+
it("resets after edits and truncation, including edits to a completed line", async () => {
52+
const highlighter = await getSharedHighlighter({
53+
...highlighterOptions,
54+
preferredHighlighter: engine,
55+
});
56+
const highlight = createIncrementalHighlightedDocument(
57+
highlighter,
58+
"typescript",
59+
"pierre-dark",
6360
);
64-
}
65-
});
61+
const inputs = [
62+
"/* open\ncomment\n",
63+
"/* open\ncomment\n*/\nconst x = 1;",
64+
"const edited = 2;\nconst x = 1;",
65+
"const edited = 2;\nconst x = 10;",
66+
"const edited = 2;\n",
67+
"",
68+
"\n\n\nconst fresh = true;\n",
69+
];
70+
for (const text of inputs) {
71+
expect(toHtml(highlight(text))).toBe(
72+
highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }),
73+
);
74+
}
75+
highlight.dispose();
76+
});
6677

67-
it("preserves partial CRLF and CR line endings", async () => {
68-
const highlighter = await highlighterPromise;
69-
const highlight = createIncrementalHighlightedDocument(
70-
highlighter,
71-
"typescript",
72-
"pierre-dark",
73-
);
74-
const code = "/* multi\r\nline */\r\nconst x = 1;\r\n";
75-
for (let end = 0; end <= code.length; end++) {
76-
const text = code.slice(0, end);
77-
expect(toHtml(highlight(text))).toBe(
78-
highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }),
78+
// MarkdownCodeBlock creates the document outside its plain-text fallback.
79+
it("creates a document for a language the highlighter has not loaded", async () => {
80+
const highlighter = await getSharedHighlighter({
81+
...highlighterOptions,
82+
preferredHighlighter: engine,
83+
});
84+
expect(() =>
85+
createIncrementalHighlightedDocument(highlighter, "cobol", "pierre-dark").dispose(),
86+
).not.toThrow();
87+
});
88+
89+
it("preserves partial CRLF and CR line endings", async () => {
90+
const highlighter = await getSharedHighlighter({
91+
...highlighterOptions,
92+
preferredHighlighter: engine,
93+
});
94+
const highlight = createIncrementalHighlightedDocument(
95+
highlighter,
96+
"typescript",
97+
"pierre-dark",
7998
);
80-
}
81-
});
82-
});
99+
const code = "/* multi\r\nline */\r\nconst x = 1;\r\n";
100+
for (let end = 0; end <= code.length; end++) {
101+
const text = code.slice(0, end);
102+
expect(toHtml(highlight(text))).toBe(
103+
highlighter.codeToHtml(text, { lang: "typescript", theme: "pierre-dark" }),
104+
);
105+
}
106+
});
107+
},
108+
);

0 commit comments

Comments
 (0)