0004: Tree-sitter for syntax, LSP for semantics
- Status: Proposed (implemented in the MVP)
- Date: 2026-12-05
Context
Highlighting must be instant and safe in any folder; semantic features need real compilers and analysers, which are large, slow to start and may execute project code. Language support must not fork the application.
Decision
- Syntax comes from Tree-sitter (
tree-sitter0.27 with maintained grammar crates for Rust, TypeScript/TSX and JavaScript), compiled into the binary, parsed incrementally per document and queried only for visible ranges. - Semantics come from language servers over LSP. Use
lsp-typesfor messages andlsp-serverfor framing; write a small client with explicit reader, writer and stderr threads and channel-based event delivery. No async runtime. - One capability contract (
Capability,ProviderKind,DocumentCapabilities) describes what any language provides; the UI shows syntax and semantic providers separately. - Advertise UTF-16 positions; prefer incremental sync; drop results for stale versions.
- CI uses a deterministic fake server; a real rust-analyzer test is opt-in.
Alternatives considered
- Async LSP frameworks (tower-lsp style, async-lsp): add a runtime and are server-oriented; our needs (one client, a handful of requests) fit threads and channels.
- Compiler APIs per language: deeper integration but a separate implementation per language; reconsider for specific modules later.
- TextMate grammars: widely available but regex-based, less precise and not incremental.
- Dynamic grammar loading: deferred; needs ABI checks and a trust story for native code.
Consequences
- Highlighting works in restricted folders and without any server installed.
- Adding a language with an existing grammar and server is configuration, not new client code.
- Tree-sitter memory and reparse cost grow with file size, which led to ADR 0009.
Validation
Lifecycle and protocol tests run against the fake server (including UTF-16 columns with emoji). Manual and opt-in tests confirm rust-analyzer diagnostics and go-to-definition.