mach-zed
Zed extension for the Mach programming language.
Features
- Syntax highlighting for Mach 7 via mach-tree-sitter
- Outline of functions, records, unions, tags and their cases, type aliases, module-level
valandvar, tests, andfwdre-exports - Auto-indentation for blocks, records, unions, tags, initializer lists, parameter and argument lists, decorators, and
asmbodies - Bracket matching for
{},[],()and#[ ], and auto-closing for{},[],(),"",'' - Comment toggling with
# - LSP integration with mach-lsp, downloaded for you or taken from
$PATH
Installation
From the Zed Extension Registry
Search for Mach in Zed's extension panel (zed: extensions) and click Install.
Development
Clone this repository:
git clone https://github.com/briar-systems/mach-zed.gitIn Zed, run zed: install dev extension from the command palette and select the mach-zed directory. Zed builds the grammar and the WASM component and loads the extension. A dev extension replaces the registry copy while it is installed. Building it needs Rust installed through rustup.
Language Server
The extension starts mls, the mach-lsp server. When you open a .mach file, Zed resolves it in this order:
- The binary path in your Zed settings (see Configuration below).
mlson your$PATH.- A prebuilt
mlsdownloaded from the latest mach-lsp release.
The download needs no setup. The extension picks the newest mach-lsp release whose compiler your project accepts (see Compiler Compatibility). It fetches that release's archive for your platform and keeps it in the extension's work directory as mls-<version>/. A newer mach-lsp release is picked up the next time Zed loads the extension. A copy no project has used for 30 days is removed. When GitHub cannot be reached, the extension chooses among the copies already downloaded. Prebuilt binaries exist for x86_64 and aarch64 Linux, x86_64 and aarch64 macOS, and x86_64 Windows. On any other platform, put mls on your $PATH or set its path in settings.
Building mach-lsp
To use your own build instead, build mach-lsp from source:
git clone https://github.com/briar-systems/mach-lsp.git
cd mach-lsp
mach dep pull .
mach build .The binary will be at out/{target}/{profile}/bin/mls (e.g. out/linux-x86_64/debug/bin/mls for a default debug build on Linux). Put it on your $PATH or point the settings at it:
install -Dm755 out/linux-x86_64/debug/bin/mls ~/.local/bin/mlsBuilding the WASM Extension
If you're developing the extension locally and need to rebuild the WASM component:
cd mach-zed
cargo build --release --target wasm32-wasip2Note: You need the
wasm32-wasip2target installed:rustup target add wasm32-wasip2
The compiled .wasm file will be at target/wasm32-wasip2/release/mach_zed.wasm. Zed handles building and loading the WASM automatically when installing from the extension registry or through zed: install dev extension.
Configuration
Editor Settings
You can customize Mach-specific editor settings in your Zed settings.json:
{
"languages": {
"Mach": {
"tab_size": 4,
"hard_tabs": false,
"format_on_save": "off"
}
}
}Language Server Binary
To use a specific build of mls, set its path in your Zed settings.json:
{
"lsp": {
"mls": {
"binary": {
"path": "/absolute/path/to/mls"
}
}
}
}mls takes no arguments, so leave binary.arguments unset.
Language Server Options
mls reads its options once, when it starts. Set them under lsp.mls.initialization_options. Zed passes them to the server unchanged and restarts it when they change:
{
"lsp": {
"mls": {
"initialization_options": {
"trace": "messages",
"traceFile": "/home/me/mls.log"
}
}
}
}| key | value | environment fallback |
|---|---|---|
trace |
"off", "messages" (what each message is, no contents) or "bodies" (also message contents, which include your source code) |
MLS_TRACE |
traceFile |
the file the trace is appended to, a relative path being under the workspace root. Without it or MLS_TRACE_FILE, the trace goes to the server's stderr, which Zed shows in its language server log |
MLS_TRACE_FILE |
requestDeadlineMs |
how long a request may wait on analysis before the server gives up on it, at least 1000 |
MLS_REQUEST_DEADLINE_MS |
An option takes precedence over its environment variable, which takes precedence over the server's default. Nothing is traced unless trace or MLS_TRACE turns it on. An unknown key or an unusable value is ignored, noted in the trace, and never stops the server from starting.
Leave requestDeadlineMs unset unless you have a reason to change it. Past the deadline the server cancels the waiting requests and restarts its analysis worker. Loading a project does not count against it.
The server does not read lsp.mls.settings, so options placed there have no effect.
Compiler Compatibility
mls contains the Mach compiler, linked from one mach release. mls --version names it, for example mls 3.0.0 (mach 7.0.0).
A project states the compilers it builds with as [project].mach in its mach.toml:
[project]
mach = "^7"When the linked compiler is outside that range, or outside a range one of the project's dependencies states, mls does not load the project. It reports why as an error on mach.toml, naming the dependency chain, and shows it as a notification. A root mach.toml without the key is refused the same way, naming the key. Both are reported on mach.toml itself, so they appear in the project diagnostics panel and when you open that file, and they clear once you save the fix.
The extension reads these ranges before it installs mls. It collects [project].mach from the mach.toml at the root of your Zed project and from every dependency it can reach (dep/<id>/mach.toml for a git dependency, the declared directory for a path dependency). Then it installs the newest mach-lsp release whose compiler satisfies all of them. Each release publishes RELEASES.json, which maps every mach-lsp version to the compiler it links, so no other release needs downloading to decide. When no release satisfies the ranges, or a range does not parse, the extension installs the newest release, and mls reports the problem on mach.toml. The choice is made once per project when its language server starts, so restart the server (editor: restart language server) after changing a range. An mls from your settings or $PATH is used as it is, whatever it links.
Project Structure
mach-zed/
├── .github/workflows/ci.yml # CI (fmt, clippy, wasm build, query checks)
├── extension.toml # Extension manifest (grammars, LSP, metadata)
├── Cargo.toml # Rust WASM extension build configuration
├── src/
│ ├── lib.rs # WASM extension entry point (language_server_command)
│ ├── compat.rs # dependency closure ranges and mls release selection
│ ├── install.rs # mls release asset naming and install layout
│ └── semver.rs # mach version and range grammar
├── languages/
│ └── mach/
│ ├── config.toml # Language configuration (brackets, comments, etc.)
│ ├── brackets.scm # Bracket matching queries
│ ├── highlights.scm # Syntax highlighting queries
│ ├── indents.scm # Auto-indentation queries
│ ├── outline.scm # Document outline queries
│ └── overrides.scm # String and comment scopes for auto-closing
├── CHANGELOG.md
└── README.mdHow It Works
Zed extensions with language server support require a Rust WASM component that implements the Extension trait from zed_extension_api. The key method is language_server_command, which returns the command Zed should execute to start the LSP.
The extension resolves the mls binary in this order:
- User settings:
lsp.mls.binary.pathin Zed'ssettings.json - System PATH:
worktree.which("mls")searches$PATH - Release download: the newest mach-lsp release whose compiler satisfies the project's dependency closure, found through the latest release's
RELEASES.json(src/compat.rs, with ranges parsed bysrc/semver.rsto the grammar in mach'sdoc/language/manifest.md). Its asset for the current platform, named by mach-lsp's release asset contract, is extracted by Zed and installed atomically intomls-<version>/(src/install.rs). A latest release without a validRELEASES.jsonthat lists itself is reported as an error, not worked around
If none of these succeed, Zed shows the reason in the language server status.
Contributing
- Clone this repo alongside mach-tree-sitter.
- Edit queries in
languages/mach/and reload the Zed extension to test. - For grammar changes, update
mach-tree-sitterand bump therevin[grammars.mach]ofextension.toml. - For LSP integration changes, edit
src/lib.rsand rebuild the WASM component.
Highlight queries are coupled to the grammar. A query may only name node types, fields and tokens the pinned mach-tree-sitter revision produces, and one that names anything else makes Zed drop the whole file, taking all highlighting with it. So a query for new syntax lands together with the grammar bump that introduces it, never ahead of one. CI compiles every query against the pinned grammar, and locally each must pass tree-sitter query --grammar-path <mach-tree-sitter checkout> <file>.scm <sample>.mach.
The grammar's own queries/highlights.scm is the reference for how its nodes are meant to be captured. This extension carries its own copy because Zed uses its own capture names (@keyword, @type, @variant, @variable.special, @embedded and so on). A few grammar facts shape the queries:
- Primitive types are
type_identifiernodes like any other type, told apart by spelling (u1, the integer widths throughu512/i512,f16/f32/f64,ptr, and vectors such asf32x4). - A comptime name (
$mach,$size_of) is acomptime_identifier. As the function of a call it is an intrinsic, otherwise a comptime read, so no list of intrinsics is kept here. - An
asmblock is anasm_head(masc's selection) and one rawasm_body, so{name}operands inside it are not separate nodes. - A comment is one
commentnode, with no doc comment kind. - Later patterns take precedence over earlier ones on the same node.