feat(mobile): color a diff's code by its language with tree-sitter
Ā
DA
3h ago+2064+2064+0123456789012345678901234567890123456789-12-12-01234567890123456789
Stacked on #10554, above #10551 and #10549. The fourth step of the iOS code viewer (STAG-3904): syntax colors with tree-sitter on the phone, as Dean picked.
What
- Languages: TypeScript/TSX, JavaScript, Swift, Python, Go, Rust, JSON, YAML, Markdown, CSS, HTML, and shell, chosen by the file's extension (
DiffLanguage(path:)). Every other file stays plain, SQL included. - Size: the Mac's link map put tree-sitter at 23.74 MB with SQL. SQL's grammar alone compiled to 12.74 MB, which pushed it past the +8ā15 MB Dean set, so it is left out, and
.sqlfiles are drawn plain. Without it, the twelve languages come to about 11 MB (per grammar,-Os: Swift 3.94, TSX 1.37, TypeScript 1.34, Bash 1.27, Rust 1.06, and the rest under 0.5 each). The next native build measures it again. - Theme: GitHub's default light and dark colors, the themes the desktop's diff draws through Shiki (
tokens/syntax-colors.ts). - Never holds the first frame: the diff draws plain at once. Its two sides (old: context plus removed; new: context plus added) are each parsed whole on a background queue, so a row is colored as the code around it reads. Each grammar's own
highlights.scmruns over them through SwiftTreeSitter's own resolver (resolve(with:)for the query's predicates,highlights()for its ranges, less specific first), so ours only paints those ranges in order onto each row. The visible rows redraw when the colors land. A diff handed again before then drops the stale colors. - Device log:
DiffView colored: N ms, R rows, <language>for the Mac's timing lane.
A plain Swift package, so the core gets a fast compile loop
apps/mobile/modules/diff-view/ios/DiffHighlight/ holds Package.swift, DiffHighlighter.swift and its tests. It has no React Native or UIKit, so swift build && swift test checks it without the app. The pod (VespinDiffView.podspec) compiles the same sources, carries the queries as a resource bundle, and takes the same grammars through React Native's spm_dependency, each pinned exactly as Package.swift and Package.resolved pin it. Only the view wiring in DiffViewModule.swift waits on the app's native build.
Pins worth knowing
- Python, CSS and YAML are pinned to v0.23.6, v0.23.2 and v0.7.0. Their newest manifests look for
src/scanner.crelative to the directory the build runs in, so as a dependency they leave the scanner out and the link fails. I hit this on the firstswift test. - JavaScript is pinned to v0.23.1, the version TypeScript 0.23.2 builds on, so the combined JS+TS queries match the grammar's node names.
- Swift and SQL commit their generated parsers only on a branch of their own (
with-generated-files,gh-pages), so each is pinned to a commit of it. - Queries are vendored unchanged from those same pins, with their MIT notices (
queries/NOTICE.md).
Tests
- Ran:
swift teston a Linux Swift 6.1.2 toolchain in this sandbox, 5 tests passing:- every language's query compiles against its grammar and colors a sample; this catches a grammar bumped past its vendored query
- a removed row takes the old side's colors, and context and added rows the new side's
- a hunk header stays plain
- a language is named by its extension
- a 5,000-row timing print: about 70 ms warm in release for valid TypeScript, 300ā400 ms when the rows aren't valid code and tree-sitter spends its time recovering. Either way it's off the main thread, after a plain first frame.
- Typechecked
@repo/mobile; ran Biome. - Not run: the Swift tests aren't part of Linux CI. The Mac queue runs
swift build && swift teston this package.DiffViewModule.swiftand the podspec'sspm_dependencyneed the app's native build. App size growth from tree-sitter will be measured on that build (du -skof the .app against main's base) and reported before this merges.
š¤ Generated with Claude Code