Skip to content

Commit e705b77

Browse files
perf(web): highlighted code tokens compile without manual memoization
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 70d60e0 commit e705b77

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

‎apps/web/reactCompiledFiles.ts‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,5 @@ export const REACT_COMPILED_FILES = [
9696
"apps/web/src/components/pullRequest/PullRequestDetailPanel.tsx",
9797
"apps/web/src/components/ThreadTerminalDrawer.tsx",
9898
"apps/web/src/components/settings/SettingsPanels.tsx",
99+
"apps/web/src/components/chat/HighlightedTokens.tsx",
99100
] as const;

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

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { use, useMemo, type CSSProperties } from "react";
1+
import { use, type CSSProperties } from "react";
22

33
import { resolveDiffThemeName } from "../../lib/diffRendering";
44
import type { EmbeddedScript } from "../../lib/embeddedScripts";
@@ -29,6 +29,20 @@ function syntaxTokenStyle(token: SyntaxToken): CSSProperties {
2929
* Suspends while the grammar loads; wrap it in Suspense with the plain text as
3030
* the fallback.
3131
*/
32+
function useHighlightedLines(
33+
highlighter: Awaited<ReturnType<typeof getSyntaxHighlighterPromise>>,
34+
code: string,
35+
language: string,
36+
embedded: ReadonlyArray<EmbeddedScript>,
37+
theme: "light" | "dark",
38+
) {
39+
"use memo";
40+
const themeName = resolveDiffThemeName(theme);
41+
const tokenize = (text: string, lang: string) =>
42+
highlighter.codeToTokens(text, { lang, theme: themeName }).tokens;
43+
return keyedLines(code, withEmbeddedScripts(code, tokenize(code, language), embedded, tokenize));
44+
}
45+
3246
export function HighlightedTokens({
3347
code,
3448
language,
@@ -44,15 +58,7 @@ export function HighlightedTokens({
4458
}) {
4559
const highlighter = use(getSyntaxHighlighterPromise(language));
4660
for (const script of embedded) use(getSyntaxHighlighterPromise(script.language));
47-
const lines = useMemo(() => {
48-
const themeName = resolveDiffThemeName(theme);
49-
const tokenize = (text: string, lang: string) =>
50-
highlighter.codeToTokens(text, { lang, theme: themeName }).tokens;
51-
return keyedLines(
52-
code,
53-
withEmbeddedScripts(code, tokenize(code, language), embedded, tokenize),
54-
);
55-
}, [code, embedded, highlighter, language, theme]);
61+
const lines = useHighlightedLines(highlighter, code, language, embedded, theme);
5662

5763
return lines.map(({ key, tokens, ending }) => (
5864
<span key={key}>

0 commit comments

Comments
 (0)