Compare commits

...

2 Commits

Author SHA1 Message Date
Kit Langton d6639cb5ad fix(tui): harden highlight cache lifecycle 2026-08-05 14:06:35 -04:00
Kit Langton b300116d0a fix(tui): cache syntax highlights across tabs 2026-08-05 14:00:00 -04:00
3 changed files with 109 additions and 0 deletions
@@ -96,6 +96,7 @@ import { findMessageBoundary, messageNavigationSlack } from "./message-navigatio
import { stringWidth } from "../../util/string-width"
import { useArgs } from "../../context/args"
import { withTimestampedFallback } from "@opencode-ai/util/session-title-fallback"
import { installSyntaxHighlightCache } from "../../util/syntax-highlight-cache"
addDefaultParsers(parsers.parsers)
@@ -128,6 +129,7 @@ function use() {
}
export function Session() {
installSyntaxHighlightCache()
const setEpilogue = useEpilogue()
const clipboard = useClipboard()
const writeExport = async (file: string, content: string) => {
@@ -0,0 +1,38 @@
import { getTreeSitterClient, type TreeSitterClient } from "@opentui/core"
const CACHE_SIZE = 500
const installed = new WeakSet<TreeSitterClient>()
export function installSyntaxHighlightCache() {
const client = getTreeSitterClient()
if (installed.has(client)) return
installed.add(client)
client.highlightOnce = cacheHighlights(client.highlightOnce.bind(client))
}
export function cacheHighlights(highlight: TreeSitterClient["highlightOnce"], capacity = CACHE_SIZE) {
const cache = new Map<string, ReturnType<TreeSitterClient["highlightOnce"]>>()
return (content: string, filetype: string) => {
const key = `${filetype}\0${content}`
const cached = cache.get(key)
if (cached) {
cache.delete(key)
cache.set(key, cached)
return cached
}
const result = highlight(content, filetype)
cache.set(key, result)
if (cache.size > capacity) cache.delete(cache.keys().next().value!)
void result
.then((value) => {
if (value.error && cache.get(key) === result) cache.delete(key)
})
.catch(() => {
if (cache.get(key) === result) cache.delete(key)
})
return result
}
}
@@ -0,0 +1,69 @@
import { describe, expect, test } from "bun:test"
import { cacheHighlights } from "../../src/util/syntax-highlight-cache"
describe("syntax highlight cache", () => {
test("reuses completed and in-flight highlights", async () => {
let calls = 0
const highlight = cacheHighlights(async () => {
calls++
return { highlights: [[0, 5, "keyword"]] }
})
const first = highlight("const", "typescript")
const second = highlight("const", "typescript")
expect(second).toBe(first)
expect(await second).toEqual({ highlights: [[0, 5, "keyword"]] })
expect(await highlight("const", "typescript")).toEqual({ highlights: [[0, 5, "keyword"]] })
expect(calls).toBe(1)
})
test("evicts least recently used highlights", async () => {
let calls = 0
const highlight = cacheHighlights(async () => {
calls++
return { highlights: [] }
}, 2)
await highlight("one", "text")
await highlight("two", "text")
await highlight("one", "text")
await highlight("three", "text")
await highlight("two", "text")
expect(calls).toBe(4)
})
test("retries failed highlights", async () => {
let calls = 0
const highlight = cacheHighlights(async () => {
calls++
if (calls === 1) return { error: "parser unavailable" }
return { highlights: [] }
})
await highlight("const", "typescript")
await highlight("const", "typescript")
expect(calls).toBe(2)
})
test("an evicted failure does not delete its replacement", async () => {
const pending = Promise.withResolvers<{ highlights: [] }>()
let calls = 0
const highlight = cacheHighlights(() => {
calls++
if (calls === 1) return pending.promise
return Promise.resolve({ highlights: [] })
}, 1)
const stale = highlight("one", "text")
await highlight("two", "text")
const current = highlight("one", "text")
pending.reject(new Error("parser unavailable"))
await expect(stale).rejects.toThrow("parser unavailable")
expect(highlight("one", "text")).toBe(current)
expect(calls).toBe(3)
})
})