zed-extensions/zed-grainpublic Fork
⑂ main
Commits
⬇ Clone ▾
git clone https://git.rickub.com/zed-extensions/zed-grain.git
git clone ssh://git@rickub.com/zed-extensions/zed-grain.git

Host key fingerprint (ed25519): SHA256:iycHnxEyq0Q7uyVpB7JlznP0G7JrTPXLYRcAU5CSLhc — verify it before your first connect.

Download ZIP
🛟 Updated. 0ca90cf · on main · k33g · 3h ago
README.md · 81 lines · 2.5 KBmarkdown
Blame HistoryOpen raw

Grain for Zed

Zed extension for the Grain language (.gr files).

  • Syntax highlighting, outline, bracket matching, indentation and text objects, using the
    Kara-Zor-El/tree-sitter-grain grammar
    (the highlight queries are adapted to Zed's capture names).
  • Markdown injection in doc comments (/** ... */), with Grain for @example.
  • Language server: grain lsp (diagnostics, hover, go to definition, formatting,
    document symbols, inlay type hints, code lenses).
  • Tasks: grain run, grain compile --release, grain format.

Install (dev extension)

Prerequisite: Rust installed with rustup (Zed compiles the extension itself).

  1. In Zed: zed: install dev extension (command palette) and pick this zed-grain folder.
  2. Open a .gr file.

Grain binary used by the LSP

The extension looks for grain, in this order:

  1. lsp.grain-lsp.binary.path in the Zed settings,
  2. grain on the PATH,
  3. otherwise it downloads the latest release from GitHub
    (grain-{linux,mac,win}-{x64,arm64}.tar.gz).
// settings.json
{
  "lsp": {
    "grain-lsp": {
      "binary": {
        "path": "grain",
        "arguments": [
          "lsp"
        ]
      }
    }
  }
}

Grain inside a Docker sandbox (sbx)

If grain only exists in an sbx sandbox (grain-toolchain kit), Zed on the host
starts the LSP through sbx exec. Put this in the project's .zed/settings.json:

{
  "lsp": {
    "grain-lsp": {
      "binary": {
        "path": "sbx",                       // or the absolute path from `which sbx`
        "arguments": [
          "exec", "-i",                      // -i = stdin open; NO -t (a TTY corrupts JSON-RPC)
          "-w", "/path/to/the/project",
          "<sandbox-name>",                  // sbx ls
          "grain", "lsp"
        ]
      }
    }
  }
}
  • This works because in direct mode the workspace is mounted at the same path in the
    sandbox, so the file:// URIs Zed sends are valid inside it.
  • sbx exec starts the sandbox if it's stopped.
  • Limitation: "go to definition" on the stdlib points to /snapshot/grain/stdlib/...,
    a path that only exists inside the grain binary.

Development

cargo build --release --target wasm32-wasip2   # check that the extension compiles

To update the grammar, change rev in extension.toml and compare
languages/grain/highlights.scm with the grammar's queries/highlights.scm.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# Grain for Zed

[Zed](https://zed.dev) extension for the [Grain](https://grain-lang.org) language (`.gr` files).

- Syntax highlighting, outline, bracket matching, indentation and text objects, using the
  [Kara-Zor-El/tree-sitter-grain](https://github.com/Kara-Zor-El/tree-sitter-grain) grammar
  (the highlight queries are adapted to Zed's capture names).
- Markdown injection in doc comments (`/** ... */`), with Grain for `@example`.
- Language server: `grain lsp` (diagnostics, hover, go to definition, formatting,
  document symbols, inlay type hints, code lenses).
- Tasks: `grain run`, `grain compile --release`, `grain format`.

## Install (dev extension)

Prerequisite: Rust installed with `rustup` (Zed compiles the extension itself).

1. In Zed: `zed: install dev extension` (command palette) and pick this `zed-grain` folder.
2. Open a `.gr` file.

## Grain binary used by the LSP

The extension looks for `grain`, in this order:

1. `lsp.grain-lsp.binary.path` in the Zed settings,
2. `grain` on the `PATH`,
3. otherwise it downloads the latest release from GitHub
   (`grain-{linux,mac,win}-{x64,arm64}.tar.gz`).

```jsonc
// settings.json
{
  "lsp": {
    "grain-lsp": {
      "binary": {
        "path": "grain",
        "arguments": [
          "lsp"
        ]
      }
    }
  }
}
```

### Grain inside a Docker sandbox (sbx)

If `grain` only exists in an sbx sandbox (`grain-toolchain` kit), Zed on the host
starts the LSP through `sbx exec`. Put this in the project's `.zed/settings.json`:

```jsonc
{
  "lsp": {
    "grain-lsp": {
      "binary": {
        "path": "sbx",                       // or the absolute path from `which sbx`
        "arguments": [
          "exec", "-i",                      // -i = stdin open; NO -t (a TTY corrupts JSON-RPC)
          "-w", "/path/to/the/project",
          "<sandbox-name>",                  // sbx ls
          "grain", "lsp"
        ]
      }
    }
  }
}
```

- This works because in direct mode the workspace is mounted **at the same path** in the
  sandbox, so the `file://` URIs Zed sends are valid inside it.
- `sbx exec` starts the sandbox if it's stopped.
- Limitation: "go to definition" on the stdlib points to `/snapshot/grain/stdlib/...`,
  a path that only exists inside the `grain` binary.

## Development

```bash
cargo build --release --target wasm32-wasip2   # check that the extension compiles
```

To update the grammar, change `rev` in `extension.toml` and compare
`languages/grain/highlights.scm` with the grammar's `queries/highlights.scm`.