diff --git a/packages/coding-agent/src/lsp/render.ts b/packages/coding-agent/src/lsp/render.ts index 39fbcfe9d..dbe21945b 100644 --- a/packages/coding-agent/src/lsp/render.ts +++ b/packages/coding-agent/src/lsp/render.ts @@ -218,7 +218,7 @@ function renderHover( const beforeCode = fullText.slice(0, codeStart).trimEnd(); const afterCode = fullText.slice(fullText.indexOf("```", 3) + 3).trim(); - const codeLines = highlightThemeCode(code, lang); + const codeLines = highlightThemeCode(code, lang, theme); const icon = theme.styledSymbol("status.info", "accent"); const langLabel = lang ? theme.fg("mdCodeBlockBorder", ` ${lang}`) : ""; diff --git a/packages/coding-agent/src/modes/theme/theme.ts b/packages/coding-agent/src/modes/theme/theme.ts index c0222bba9..e27558a49 100644 --- a/packages/coding-agent/src/modes/theme/theme.ts +++ b/packages/coding-agent/src/modes/theme/theme.ts @@ -2559,10 +2559,10 @@ const HIGHLIGHT_CACHE_MAX = 256; const highlightCache = new LRUCache({ max: HIGHLIGHT_CACHE_MAX }); let highlightCacheTheme: Theme | undefined; -function highlightCached(code: string, validLang: string | undefined): string | null { - if (highlightCacheTheme !== theme) { +function highlightCached(code: string, validLang: string | undefined, highlightTheme: Theme): string | null { + if (highlightCacheTheme !== highlightTheme) { highlightCache.clear(); - highlightCacheTheme = theme; + highlightCacheTheme = highlightTheme; } const key = `${validLang ?? ""}\x00${code}`; const hit = highlightCache.get(key); @@ -2571,7 +2571,7 @@ function highlightCached(code: string, validLang: string | undefined): string | } let highlighted: string; try { - highlighted = nativeHighlightCode(code, validLang, getHighlightColors(theme)); + highlighted = nativeHighlightCode(code, validLang, getHighlightColors(highlightTheme)); } catch { return null; } @@ -2583,9 +2583,9 @@ function highlightCached(code: string, validLang: string | undefined): string | * Highlight code with syntax coloring based on file extension or language. * Returns array of highlighted lines. */ -export function highlightCode(code: string, lang?: string): string[] { +export function highlightCode(code: string, lang?: string, highlightTheme: Theme = theme): string[] { const validLang = lang && nativeSupportsLanguage(lang) ? lang : undefined; - const highlighted = highlightCached(code, validLang); + const highlighted = highlightCached(code, validLang, highlightTheme); // Always return a fresh array: callers (e.g. renderCodeCell) push extra lines // onto the result, which would corrupt the cached string otherwise. return (highlighted ?? code).split("\n"); @@ -2633,7 +2633,7 @@ export function getMarkdownTheme(): MarkdownTheme { resolveMermaidAscii, highlightCode: (code: string, lang?: string): string[] => { const validLang = lang && nativeSupportsLanguage(lang) ? lang : undefined; - const highlighted = highlightCached(code, validLang); + const highlighted = highlightCached(code, validLang, theme); if (highlighted !== null) return highlighted.split("\n"); return code.split("\n").map(line => theme.fg("mdCodeBlock", line)); }, diff --git a/packages/coding-agent/test/lsp-render.test.ts b/packages/coding-agent/test/lsp-render.test.ts index ca44f0f74..d8d2009f3 100644 --- a/packages/coding-agent/test/lsp-render.test.ts +++ b/packages/coding-agent/test/lsp-render.test.ts @@ -22,7 +22,7 @@ describe("LSP render", () => { const rendered = Bun.stripANSI(component.render(120).join("\n")); expect(highlightSpy).toHaveBeenCalledTimes(1); - expect(highlightSpy).toHaveBeenCalledWith("const value = 1;", "ts"); + expect(highlightSpy).toHaveBeenCalledWith("const value = 1;", "ts", themeModule.theme); expect(rendered).toContain("CACHED_HIGHLIGHT"); }); });