Quick Start
Highlight code with the built-in JavaScript/TypeScript support:Tree-Sitter Client
Initialization
The Tree-sitter client manages parsers and performs highlighting:One-Time Highlighting
Highlight code without creating a CodeRenderable:Adding Custom Parsers
Extend language support by adding custom Tree-sitter parsers.Global Default Parsers
Add parsers that all clients will use:Per-Client Parsers
Add parsers to specific client instances:Finding Parsers and Queries
Official Tree-Sitter Parsers
Most popular languages have official parsers on GitHub:Pattern for Finding Parsers
Using Local Files
For better performance and offline support, bundle parsers with your app:Automated Parser Management
Automate parser downloads and configuration:{
"parsers": [
{
"filetype": "python",
"wasm": "https://github.com/tree-sitter/tree-sitter-python/releases/download/v0.23.6/tree-sitter-python.wasm",
"queries": {
"highlights": [
"https://raw.githubusercontent.com/tree-sitter/tree-sitter-python/master/queries/highlights.scm"
]
}
},
{
"filetype": "rust",
"wasm": "https://github.com/tree-sitter/tree-sitter-rust/releases/download/v0.23.2/tree-sitter-rust.wasm",
"queries": {
"highlights": [
"https://raw.githubusercontent.com/tree-sitter/tree-sitter-rust/master/queries/highlights.scm"
]
}
}
]
}
{
"scripts": {
"prebuild": "bun node_modules/@opentui/core/lib/tree-sitter/assets/update.ts --config ./parsers-config.json --assets ./src/parsers --output ./src/parsers.ts",
"build": "bun build ./src/index.ts"
}
}
import { addDefaultParsers, getTreeSitterClient } from "@opentui/core"
import { getParsers } from "./parsers" // Generated file
addDefaultParsers(getParsers())
const client = getTreeSitterClient()
await client.initialize()
const result = await client.highlightOnce(
'def hello():\n print("world")',
"python"
)
Syntax Styles
Customize how syntax elements are colored:CodeRenderable
TheCodeRenderable component provides a full-featured code viewer:
Filetype Detection
Automatically detect filetype from file paths:js,jsx→ javascriptts,tsx→ typescriptpy→ pythonrs→ rustgo→ gorb→ rubyc,h→ ccpp,hpp,cc→ cpphtml→ htmlcss→ css- And many more
Complete Example: Code Viewer
Caching
Parsers and queries are automatically cached to avoid re-downloading:Performance Tips
Initialize Once
Initialize Once
Initialize the Tree-sitter client once at app startup:
Use Local Files in Production
Use Local Files in Production
Bundle parsers to avoid network requests:
Limit Language Support
Limit Language Support
Only include parsers you need:
Troubleshooting
Parser Not Found
Parser Not Found
Make sure the parser is added before using:
No Syntax Highlighting
No Syntax Highlighting
Check that:
- Client is initialized:
await client.initialize() - Filetype is correct:
filetype: "javascript"not"js" - Parser is added for that filetype
Download Failures
Download Failures
If parsers fail to download:
- Check internet connection
- Verify URLs are correct
- Use local files instead of URLs
Next Steps
Building Your First App
Build a complete TUI application
Styling and Colors
Master colors and text styling