Fork of daniellemaywood.uk/gleam — Wasm codegen work
1# Wasm Zed extensions
2
3Gleam can build [Zed](https://zed.dev) extensions as WebAssembly **components**
4that implement the full `zed:extension` world (API **0.7.0**).
5
6This repository is the **compiler**. Real extension packages (e.g. Glint) live
7with the language project, not here.
8
9**Installable Glint extension:** `/home/nandi/code/glint/editors/zed`
10
11## Author experience
12
13```text
14my_ext/
15 gleam.toml # target = "wasm"
16 extension.toml # languages, grammars, language_servers
17 languages/…
18 src/extension.gleam
19```
20
21```gleam
22pub type Worktree
23pub type Command {
24 Command(command: String, args: List(String), env: List(#(String, String)))
25}
26
27pub fn language_server_command(
28 _id: String,
29 worktree: Worktree,
30) -> Result(Command, String) {
31 // …
32}
33```
34
35```sh
36gleam export zed-extension
37# → extension.wasm (component + zed:api-version)
38```
39
40## What the compiler emits
41
421. **Gleam → MIR → core Wasm** for user modules (heap strings, structs,
43 lists, `@external(wasm, …)` imports).
442. **Guest shell** for every `zed:extension` world export:
45 - Real `language-server-command` (PATH `which` + documented fallback)
46 - Defaults for other callbacks (`Err("not implemented")` / empty results)
47 - `cabi_realloc` bump allocator and post-return no-ops
483. **Component packaging** via `wit-component` with vendored WIT
49 (`compiler-core/wit/zed_extension/0.7.0/`).
504. Custom section **`zed:api-version`**: six bytes, big-endian `u16` major /
51 minor / patch for `0.7.0`.
52
53## Zed Install Dev Extension (today)
54
55Zed always runs `cargo build --target wasm32-wasip2` when
56`[lib] kind = "Rust"`. It does **not** install a prebuilt Gleam
57`extension.wasm` alone. Until that changes, installable extensions still
58need a thin Rust guest (as in `glint/editors/zed`).
59
60Grammar keys must match the tree-sitter C symbol (e.g. `grammars.gleam` →
61`tree_sitter_gleam`).
62
63## Example package (out of tree)
64
65| Location | Role |
66|----------|------|
67| `~/code/glint/editors/zed` | Installable Glint extension (Rust guest + Gleam sketch) |
68| `~/code/gleam` (this repo) | Compiler: MIR, CABI, `gleam export zed-extension` |
69| `examples/hello_wasm` | Core Wasm smoke test (not a Zed extension) |
70
71## Status / roadmap
72
73| Piece | Status |
74|-------|--------|
75| Full world exports + component | Done |
76| `language-server-command` CABI | Done (guest shell) |
77| `@external(wasm)` + structs/lists | Partial |
78| Wire Gleam body into CABI exports | In progress |
79| Zed prebuilt install (no cargo) | Needs Zed change or alternate install path |