Skip to content

In development. Expect rough edges and changes. The source code is not public yet.

Zed 10 min read Deep dive mediumAdept

Enabling low-latency, syntax-aware editing using Tree-sitter

Zed builds its syntax-aware features on Tree-sitter, a GLR-based incremental parsing framework with error recovery that produces concrete syntax trees. Syntax-aware selection uses the CST directly. Highlighting, symbol outlines, auto-indentation and language injections are each driven by declarative Tree-sitter queries, which compile to state machines matched in a single depth-first walk, so adding a language needs only a grammar and query files. Injected trees, such as JavaScript inside HTML or Rust macro arguments, are kept in a copy-on-write B-tree called a sum tree, which lets edits be re-parsed incrementally on a background thread while the main thread keeps reading the previous snapshot.

Assumes: syntax trees and parsing basicscontext-free grammars

Related posts

  • Zed Has Rainbow Brackets

    Zed mediumAdept

    Zed implements rainbow brackets without language servers or a persistent syntax tree by running each language's tree-sitter brackets.scm queries over independent 50-row chunks and coloring pairs by nesting depth across 7 cycling theme accent colors. Chunks may occasionally be off by one color at boundaries, a tradeoff accepted to avoid maintaining global bracket state. Results are cached on the buffer rather than the ephemeral editor, versioned with clock::Global, invalidated eagerly on edits and re-queried lazily only for visible ranges. Upstream tree-sitter changes that limit query search ranges keep performance acceptable, and a visual test notation annotates each bracket with its color index.

  • Extensible Language Support in Zed - Part 1

    Zed mediumAdept

    Zed loads third-party Tree-sitter grammars as WebAssembly modules using a hybrid design. Native code consumes the parser's static parse tables after they are copied out of wasm linear memory, while only the lexing functions, including hand-written external scanners, run inside Wasmtime. This avoids depending on the user's C compiler and keeps buggy scanners from crashing the editor. It also avoids copying large syntax trees out of wasm memory on every keystroke. A new `WasmStore` API lets wasm-loaded languages behave identically to native ones, and a bundled mini-libc with a bump allocator that is reset each parse makes scanner memory leaks impossible.

  • Syntax-Aware Task Spawning With Tree-Sitter

    Zed mediumAdept

    Zed Tasks run shell commands from the editor using variables such as $ZED_SYMBOL (filled from Tree-sitter with the enclosing symbol), $ZED_FILE and $ZED_SELECTED_TEXT. Tasks can be defined in project or global tasks.json templates, bound to keys, and rerun with reevaluate_context so variables pick up the current selection or file. Language extensions can ship runnables.scm Tree-sitter queries that tag syntax nodes such as test functions. Zed matches those tags against the tags on task definitions and places clickable play buttons in the gutter to run them.

  • Rope & SumTree

    Zed mediumAdept

    Zed represents buffer text with a rope rather than a string, because contiguous strings make mid-file inserts and deletes, line navigation and very large files expensive. Its rope is not a classic binary-tree rope but a SumTree of fixed-capacity inline Chunks of at most 128 characters in release builds. The leaves are reference-counted via Arc, so edits mostly rewire pointers and snapshots are cheap. Concurrent access was the deciding requirement: every edit can hand a snapshot to a background thread for Tree-sitter re-parsing without copying the text, which made the rope win over gap buffers and piece tables.

  • Zed Weekly: #18

    Zed easyNovice

    Zed's semantic index now batches embedding requests by token count rather than span count, cutting OpenAI requests and improving throughput and consistency. A new embeddings cache skips re-embedding unchanged tree-sitter spans and reduces incremental indexing tokens by about 75%. The first inline AI assistant has merged. Other updates: Tailwind completions using multiple language servers per file, automatic Python virtualenv activation in v0.102, reconnection handling for collaborative channel notes, rust-analyzer inlay hint fixes, and progress on replace-in-buffer search.

The quest for software engineering knowledge.

Look:
Layout: