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- [Slash Commands](./slash-commands.md)
 10
 11## Directory Structure of a Zed Extension
 12
 13A Zed extension is a Git repository that contains an `extension.toml`. This file must contain some
 14basic information about the extension:
 15
 16```toml
 17id = "my-extension"
 18name = "My extension"
 19version = "0.0.1"
 20schema_version = 1
 21authors = ["Your Name <you@example.com>"]
 22description = "My cool extension"
 23repository = "https://github.com/your-name/my-zed-extension"
 24```
 25
 26In 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:
 27
 28```
 29my-extension/
 30  extension.toml
 31  Cargo.toml
 32  src/
 33    lib.rs
 34  languages/
 35    my-language/
 36      config.toml
 37      highlights.scm
 38  themes/
 39    my-theme.json
 40```
 41
 42## WebAssembly
 43
 44Procedural 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:
 45
 46```toml
 47[package]
 48name = "my-extension"
 49version = "0.0.1"
 50edition = "2021"
 51
 52[lib]
 53crate-type = ["cdylib"]
 54
 55[dependencies]
 56zed_extension_api = "0.1.0"
 57```
 58
 59Make sure to use the latest version of the [`zed_extension_api`](https://crates.io/crates/zed_extension_api) available on crates.io.
 60
 61In 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:
 62
 63```rs
 64use zed_extension_api as zed;
 65
 66struct MyExtension {
 67    // ... state
 68}
 69
 70impl zed::Extension for MyExtension {
 71    // ...
 72}
 73
 74zed::register_extension!(MyExtension);
 75```
 76
 77## Developing an Extension Locally
 78
 79Before starting to develop an extension for Zed, be sure to [install Rust](https://www.rust-lang.org/tools/install).
 80
 81When developing an extension, you can use it in Zed without needing to publish it by installing it as a _dev extension_.
 82
 83From the extensions page, click the `Install Dev Extension` button and select the directory containing your extension.
 84
 85If you already have a published extension with the same name installed, your dev extension will override it.
 86
 87## Publishing your extension
 88
 89To publish an extension, open a PR to [the `zed-industries/extensions` repo](https://github.com/zed-industries/extensions).
 90
 91> 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.
 92
 93In your PR, do the following:
 94
 951. Add your extension as a Git submodule within the `extensions/` directory
 962. Add a new entry to the top-level `extensions.toml` file containing your extension:
 97
 98```toml
 99[my-extension]
100submodule = "extensions/my-extension"
101version = "0.0.1"
102```
103
1043. Run `pnpm sort-extensions` to ensure `extensions.toml` and `.gitmodules` are sorted
105
106Once your PR is merged, the extension will be packaged and published to the Zed extension registry.
107
108> Extension IDs and names should not contain `zed` or `Zed`, since they are all Zed extensions.
109
110## Updating an extension
111
112To update an extension, open a PR to [the `zed-industries/extensions` repo](https://github.com/zed-industries/extensions).
113
114In your PR do the following:
115
1161. Update the extension's submodule to the commit of the new version.
1172. Update the `version` field for the extension in `extensions.toml`
118   - Make sure the `version` matches the one set in `extension.toml` at the particular commit.
119
120If you'd like to automate this process, there is a [community GitHub Action](https://github.com/huacnlee/zed-extension-action) you can use.