developing-extensions.md

  1# Developing Extensions
  2
  3## Extension Capabilities
  4
  5Extensions can add the following capabilities to Zed:
  6
  7- [Languages](./languages.md)
  8- [Themes](./themes.md)
  9- [Icon Themes](./icon-themes.md)
 10- [Slash Commands](./slash-commands.md)
 11- [Context Servers](./context-servers.md)
 12
 13## Developing an Extension Locally
 14
 15Before starting to develop an extension for Zed, be sure to [install Rust via rustup](https://www.rust-lang.org/tools/install).
 16
 17> Rust must be installed via rustup. If you have Rust installed via homebrew or otherwise, installing dev extensions will not work.
 18
 19When developing an extension, you can use it in Zed without needing to publish it by installing it as a _dev extension_.
 20
 21From the extensions page, click the `Install Dev Extension` button and select the directory containing your extension.
 22
 23If you already have a published extension with the same name installed, your dev extension will override it.
 24
 25## Directory Structure of a Zed Extension
 26
 27A Zed extension is a Git repository that contains an `extension.toml`. This file must contain some
 28basic information about the extension:
 29
 30```toml
 31id = "my-extension"
 32name = "My extension"
 33version = "0.0.1"
 34schema_version = 1
 35authors = ["Your Name <you@example.com>"]
 36description = "My cool extension"
 37repository = "https://github.com/your-name/my-zed-extension"
 38```
 39
 40In addition to this, there are several other optional files and directories that can be used to add functionality to a Zed extension. An example directory structure of an extension that provides all capabilities is as follows:
 41
 42```
 43my-extension/
 44  extension.toml
 45  Cargo.toml
 46  src/
 47    lib.rs
 48  languages/
 49    my-language/
 50      config.toml
 51      highlights.scm
 52  themes/
 53    my-theme.json
 54```
 55
 56## WebAssembly
 57
 58Procedural parts of extensions are written in Rust and compiled to WebAssembly. To develop an extension that includes custom code, include a `Cargo.toml` like this:
 59
 60```toml
 61[package]
 62name = "my-extension"
 63version = "0.0.1"
 64edition = "2021"
 65
 66[lib]
 67crate-type = ["cdylib"]
 68
 69[dependencies]
 70zed_extension_api = "0.1.0"
 71```
 72
 73Use the latest version of the [`zed_extension_api`](https://crates.io/crates/zed_extension_api) available on crates.io. Make sure it's still [compatible with Zed versions](https://github.com/zed-industries/zed/blob/main/crates/extension_api#compatible-zed-versions) you want to support.
 74
 75In the `src/lib.rs` file in your Rust crate you will need to define a struct for your extension and implement the `Extension` trait, as well as use the `register_extension!` macro to register your extension:
 76
 77```rs
 78use zed_extension_api as zed;
 79
 80struct MyExtension {
 81    // ... state
 82}
 83
 84impl zed::Extension for MyExtension {
 85    // ...
 86}
 87
 88zed::register_extension!(MyExtension);
 89```
 90
 91## Publishing your extension
 92
 93To publish an extension, open a PR to [the `zed-industries/extensions` repo](https://github.com/zed-industries/extensions).
 94
 95> Note: It is very helpful if you fork the `zed-industries/extensions` repo to a personal GitHub account instead of a GitHub organization, as this allows Zed staff to push any needed changes to your PR to expedite the publishing process.
 96
 97In your PR, do the following:
 98
 991. Add your extension as a Git submodule within the `extensions/` directory
100
101```sh
102git submodule add https://github.com/your-username/foobar-zed.git extensions/foobar
103git add extensions/foobar
104```
105
1062. Add a new entry to the top-level `extensions.toml` file containing your extension:
107
108```toml
109[my-extension]
110submodule = "extensions/my-extension"
111version = "0.0.1"
112```
113
114> If your extension is in a subdirectory within the submodule you can use the `path` field to point to where the extension resides.
115
1163. Run `pnpm sort-extensions` to ensure `extensions.toml` and `.gitmodules` are sorted
117
118Once your PR is merged, the extension will be packaged and published to the Zed extension registry.
119
120> Extension IDs and names should not contain `zed` or `Zed`, since they are all Zed extensions.
121
122## Updating an extension
123
124To update an extension, open a PR to [the `zed-industries/extensions` repo](https://github.com/zed-industries/extensions).
125
126In your PR do the following:
127
1281. Update the extension's submodule to the commit of the new version.
1292. Update the `version` field for the extension in `extensions.toml`
130   - Make sure the `version` matches the one set in `extension.toml` at the particular commit.
131
132If you'd like to automate this process, there is a [community GitHub Action](https://github.com/huacnlee/zed-extension-action) you can use.