Skip to content

zed-rx: a Zed extension for .rx - #92

Open
oddcelot wants to merge 11 commits into
solidjs:mainfrom
oddcelot:zed-rx
Open

oddcelot wants to merge 11 commits into
solidjs:mainfrom
oddcelot:zed-rx

Conversation

@oddcelot

Copy link
Copy Markdown
Contributor

Hi! This adds a Zed extension for .rx and .d.rx in tooling/zed-rx. It builds on the tree-sitter grammar from #88 and fills the gap the language server README mentions: "Zed needs an extension of its own; there isn't one yet".

The extension starts the same language server VS Code runs, so hover, go to definition, references, rename, completion, type errors and the compiler's diagnostics behave the same. It installs @reactivescript/language-server from npm and updates it when there's a newer release. If it's offline or the update fails, it keeps the copy it already has. The compiler comes inside that package as WebAssembly, so there's nothing to build per platform.

On the tree-sitter side there are queries for:

  • highlighting, where sources, equations and writes each get their own capture (table below)
  • the outline: components, fns, actions, sources, equations, contexts, errors and types
  • indentation, brackets, JSX tag auto-close and Vim text objects
  • injections: <style> as CSS, /** */ as JSDoc, regex literals, and css, html, sql, graphql/gql and styled templates. rx fences in Markdown are highlighted too.

Trying it

Run zed: install dev extension and pick tooling/zed-rx. Zed builds it with Rust from rustup.

Note

Like the VS Code extension, the server expects a tsconfig.json with "jsx": "preserve" and "jsxImportSource": "@solidjs/web", and @solidjs/web in node_modules.

Highlighting

Code in examples/todo.rx Capture Reads as
source todos, nextId := nextId + 1 @variable.special state
shown = …, remaining = … @constant derived
:= @keyword.operator a write

Markup inside exported components and control blocks is highlighted too, which is what #86 fixed for the TextMate grammar.

examples/todo.rx in Zed with the outline panel open and a hover showing (parameter) ts: Todo[] Zed's language server menu with rx-language-server running

Settings

All optional, under lsp.rx-language-server in Zed's settings.json. They're the same three settings the VS Code extension has.

Setting What it does Default
binary Runs another server, such as one from a checkout. Zed starts it without the extension, see below The npm package
settings.rxc.path Uses an rxc binary instead of the WebAssembly build The bundled build
settings.typescript.tsdk TypeScript's lib folder The project's node_modules/typescript/lib, else the server's own
Example
{
  "lsp": {
    "rx-language-server": {
      "settings": {
        "rxc": { "path": "/path/to/reactivescript/compiler/target/release/rxc" },
        "typescript": { "tsdk": "/path/to/project/node_modules/typescript/lib" }
      }
    }
  }
}

With binary.path set, Zed runs that command itself and never asks the extension. It doesn't add --stdio, doesn't run a .js file with its Node, and doesn't read settings.rxc.path, so all three have to go in binary:

A server from a checkout
{
  "lsp": {
    "rx-language-server": {
      "binary": {
        "path": "node",
        "arguments": ["/path/to/reactivescript/tooling/rx-language-server/bin/server.js", "--stdio"],
        "env": { "RXC": "/path/to/reactivescript/compiler/target/release/rxc" }
      }
    }
  }
}

Zed has semantic tokens off by default. "languages": { "ReactiveScript": { "semantic_tokens": "combined" } } turns the server's on.

Notes for review

The queries are MIT. I wrote them from the grammar's own queries and from tree-sitter-javascript's and tree-sitter-typescript's. Zed's built-in TSX queries are GPL, so none of their text is in here.

rev in extension.toml pins the grammar at b470614, the squash commit of #88. A grammar change only reaches Zed once that line is bumped.

injections.scm has one pattern per template tag with the language written out, the way Zed's TSX injections do it. Taking the language from the tag's capture with injection.combined would be shorter, but it broke JSX tag auto-close everywhere in a .rx file.

TypeScript is only looked up at the worktree root. A project that hoists node_modules to a parent folder has to set settings.typescript.tsdk.

The server README also loses "Neovim has no .rx grammar yet", which stopped being true with #88.

Testing

  • cargo build --release --target wasm32-wasip2, cargo clippy and cargo fmt --check pass
  • ./check-queries.sh runs all seven query files through tree-sitter query on the examples, on the grammar extension.toml pins
  • The new zed workflow runs the cargo checks and check-queries.sh in CI
  • The published npm package (0.3.1) has bin/server.js where the extension starts it
  • Installed as a dev extension and checked by hand on examples/shop and examples/todos.rx

Not included

  • Snippets, and tasks for rx-tsc
  • Theme rules for the server's source and equation semantic tokens
  • Looking for TypeScript in parent folders
  • Publishing to Zed's extension registry, which needs a PR to zed-industries/extensions. Until then it's a dev extension.

Every comment was handed to the JSDoc grammar, so a // line came out as
JSDoc description text, and its words lost the comment colour up to the
first comma or full stop.
…th as Zed runs it

Zed starts lsp.rx-language-server.binary.path itself and never asks the
extension for a command, so the branch that read it was dead: no --stdio,
no Node for a .js file, no RXC. The README now shows the settings that work.

A failed npm install no longer stops a server that is already on disk, and
an install whose bin/server.js is missing is installed again.
…ns, ' closes

The type_arguments and type_parameters patterns sat before the operator
list, which also has < and >, and the later pattern wins.
With the language taken from the tag's capture and injection.combined,
Zed's JSX tag auto-close did nothing anywhere in a .rx file, even where
no template matched. One pattern per tag, the language named, as in
Zed's own TSX injections, brings it back; css`…` still highlights.
…*/ is not a doc comment

Object and array patterns of a parameter are parameters, on optional
parameters too. The root of a write or guess was found three members deep;
a query cannot recurse, so it is four now and the cap is written down.
/**/ and a /*** banner were doc comments in highlights.scm but not JSDoc in
injections.scm; both use the same test now.
…he pinned grammar

No workflow built tooling/zed-rx or ran its queries. A query that names a
node the grammar does not have stops Zed loading the language, so
check-queries.sh runs every query over examples/*.rx on the grammar
extension.toml pins, fetched from the repository named there when the
checkout does not have it. zed.yml runs it after fmt, clippy and the
wasm32-wasip2 build.

The README says how to run the same checks and where to look in Zed.
@oddcelot

Copy link
Copy Markdown
Contributor Author

What I'd do after this, roughly in order:

  1. Semantic token colours. The server already marks variables with a source or equation modifier, but Zed doesn't style those, so turning semantic_tokens on changes nothing yet. I have semantic_token_rules.json on a branch that maps them to variable.special and constant, the same styles the highlights use. It's one small JSON file and a README note, so it can go into this PR instead if you'd rather.
  2. Publishing to Zed's registry, see the next comment. The submodule would point at this repo, so I think it should come from your side, but I can open the PR to zed-industries/extensions if you want.
  3. Looking for TypeScript above the worktree root, so hoisted workspaces work without tsdk.
  4. Tasks for rx-tsc --noEmit.
  5. Snippets for source, action, component and effect.

One more: a grammar change doesn't reach Zed until the rev in extension.toml is bumped. The zed workflow could fail when tooling/tree-sitter-rx has commits after that rev, so the bump isn't forgotten.

@oddcelot

Copy link
Copy Markdown
Contributor Author

Getting this into Zed's registry, going by their publishing guide:

The registry is the zed-industries/extensions repo. Each extension there is a git submodule plus an entry in extensions.toml. This one lives in a subfolder, so the entry needs a path:

git clone https://github.com/zed-industries/extensions
cd extensions
git submodule add https://github.com/solidjs/reactivescript.git extensions/reactivescript
git -C extensions/reactivescript checkout <commit on main with tooling/zed-rx>
[reactivescript]
submodule = "extensions/reactivescript"
path = "tooling/zed-rx"
version = "0.1.0"

Then pnpm sort-extensions and open the PR.

Their requirements, which I think this meets:

  • The ID can't contain zed or extension; reactivescript is fine.
  • The license has to be in the extension's own folder. tooling/zed-rx/LICENSE is MIT.
  • Every language needs a grammar, and Rust code is only allowed with a language server. Both apply here.
  • The extension may only write to its own directory. The npm install goes through zed::npm_install_package.

They also ask that the exact commit being submitted is tested by hand, and they close PRs whose feedback isn't answered within 3 weeks.

Each update is another PR there that moves the submodule and bumps version to match extension.toml. huacnlee/zed-extension-action can open those from a workflow here, like editor.yml does for VS Code. It needs a token that can push to a fork of zed-industries/extensions.

The submodule is the whole repo, so their CI clones eval/ and ref/ too. If that becomes a problem, a small mirror repo with just tooling/zed-rx would work instead.

@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Addedcargo/​zed_extension_api@​0.7.08210093100100

View full report

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant