Skip to content

fix(tui): paint cached highlights on the first frame - #46413

Closed
kitlangton wants to merge 1 commit into
v2from
cached-highlight-paint
Closed

fix(tui): paint cached highlights on the first frame#46413
kitlangton wants to merge 1 commit into
v2from
cached-highlight-paint

Conversation

@kitlangton

Copy link
Copy Markdown
Contributor

Why
Returning to an already-highlighted session still paints code as plaintext before Tree-sitter responds. Cached results need to be available synchronously during the first paint, not just through an awaited promise.

What Changes

State First Paint
Completed cache hit for the same text and language Highlighted using the current theme and conceal settings
Cold miss, eviction, or failed highlighting Existing plaintext fallback and normal asynchronous highlighting
Explicit initial styled text or custom highlighting transforms Existing renderer behavior

A bounded 500-entry Tree-sitter cache supplies completed results to CodeRenderable before layout and paint. Parser/configuration changes and client teardown invalidate it. Cached data is copied so a custom callback cannot modify another view's highlighting.

The change is carried as a pinned OpenTUI 0.5.9 patch, with matching Bun/Node implementations. Keeping the lookup in the renderer avoids stale initialStyledText values when Markdown reuses a node after its content or theme changes. No TUI paint suppression or hidden session trees are added.

Demo

cached-highlight-before-after.mp4

Production TUI: 5a4914c670 (left) and 840ff9306d (right), the same imported nine-line TypeScript fixture at 120x40. Cold code appears as readable plaintext in both. On the warm tab return, the first code frame is already highlighted after the fix. No LLM requests or artificial worker delays; matched 1x/60fps clips with setup and idle gaps trimmed. This demonstrates cached first-paint styling, not throughput or hardware latency. The 500ms footer is fixed fixture history, not switch timing. Settled frames match exactly.

Actual warm-return video frame, before the base's colors arrive:

Cached first paint before and after

Scope
This replaces the approach in #40698. Cold content stays readable immediately; this PR does not attempt to hide first-time highlighting latency or change streaming policy on cache misses. It is independent of #46145.

Verification

# packages/tui
bun run test
bun typecheck

# packages/cli
bun typecheck

# repository
bunx prettier --check packages/tui/test/cached-highlight.test.tsx packages/tui/test/app-lifecycle.test.tsx
git diff --check

1,011 tests passed, 4 skipped. TUI/CLI typechecks and all 33 pre-push typecheck tasks passed. Controlled tests hold the next highlight promise unresolved and assert both immediate colored cache hits and immediate plaintext misses. Coverage includes nested fences, Write previews, Solid property ordering, first-frame concealment geometry, theme/content/language changes, LRU eviction, parser invalidation, mutable callbacks, and teardown.

A Node-side worker/cache smoke also passed. Native Node painting could not be exercised because OpenTUI 0.5.9 reports that native FFI is unavailable for that runtime; paint tests use the production Bun renderer.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant