# Wasm Zed extensions

Gleam can build [Zed](https://zed.dev) extensions as WebAssembly **components**
that implement the full `zed:extension` world (API **0.7.0**).

This repository is the **compiler**. Real extension packages (e.g. Glint) live
with the language project, not here.

**Installable Glint extension:** `/home/nandi/code/glint/editors/zed`

## Author experience

```text
my_ext/
  gleam.toml          # target = "wasm"
  extension.toml      # languages, grammars, language_servers
  languages/…
  src/extension.gleam
```

```gleam
pub type Worktree
pub type Command {
  Command(command: String, args: List(String), env: List(#(String, String)))
}

pub fn language_server_command(
  _id: String,
  worktree: Worktree,
) -> Result(Command, String) {
  // …
}
```

```sh
gleam export zed-extension
# → extension.wasm  (component + zed:api-version)
```

## What the compiler emits

1. **Gleam → MIR → core Wasm** for user modules (heap strings, structs,
   lists, `@external(wasm, …)` imports).
2. **Guest shell** for every `zed:extension` world export:
   - Real `language-server-command` (PATH `which` + documented fallback)
   - Defaults for other callbacks (`Err("not implemented")` / empty results)
   - `cabi_realloc` bump allocator and post-return no-ops
3. **Component packaging** via `wit-component` with vendored WIT
   (`compiler-core/wit/zed_extension/0.7.0/`).
4. Custom section **`zed:api-version`**: six bytes, big-endian `u16` major /
   minor / patch for `0.7.0`.

## Zed Install Dev Extension (today)

Zed always runs `cargo build --target wasm32-wasip2` when
`[lib] kind = "Rust"`. It does **not** install a prebuilt Gleam
`extension.wasm` alone. Until that changes, installable extensions still
need a thin Rust guest (as in `glint/editors/zed`).

Grammar keys must match the tree-sitter C symbol (e.g. `grammars.gleam` →
`tree_sitter_gleam`).

## Example package (out of tree)

| Location | Role |
|----------|------|
| `~/code/glint/editors/zed` | Installable Glint extension (Rust guest + Gleam sketch) |
| `~/code/gleam` (this repo) | Compiler: MIR, CABI, `gleam export zed-extension` |
| `examples/hello_wasm` | Core Wasm smoke test (not a Zed extension) |

## Status / roadmap

| Piece | Status |
|-------|--------|
| Full world exports + component | Done |
| `language-server-command` CABI | Done (guest shell) |
| `@external(wasm)` + structs/lists | Partial |
| Wire Gleam body into CABI exports | In progress |
| Zed prebuilt install (no cargo) | Needs Zed change or alternate install path |
