Add initial support for defining language server adapters in WebAssembly-based extensions (#8645)

This PR adds **internal** ability to run arbitrary language servers via
WebAssembly extensions. The functionality isn't exposed yet - we're just
landing this in this early state because there have been a lot of
changes to the `LspAdapter` trait, and other language server logic.

## Next steps

* Currently, wasm extensions can only define how to *install* and run a
language server, they can't yet implement the other LSP adapter methods,
such as formatting completion labels and workspace symbols.
* We don't have an automatic way to install or develop these types of
extensions
* We don't have a way to package these types of extensions in our
extensions repo, to make them available via our extensions API.
* The Rust extension API crate, `zed-extension-api` has not yet been
published to crates.io, because we still consider the API a work in
progress.

Release Notes:

- N/A

---------

Co-authored-by: Marshall <marshall@zed.dev>
Co-authored-by: Nathan <nathan@zed.dev>
Co-authored-by: Marshall Bowers <elliott.codes@gmail.com>
This commit is contained in:
Max Brunsfeld 2024-03-01 16:00:55 -08:00 committed by GitHub
parent f3f2225a8e
commit 268fa1cbaf
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
84 changed files with 3714 additions and 1973 deletions

View file

@ -0,0 +1,14 @@
[package]
name = "zed_extension_api"
version = "0.1.0"
edition = "2021"
license = "Apache-2.0"
[lib]
path = "src/extension_api.rs"
[dependencies]
wit-bindgen = "0.18"
[package.metadata.component]
target = { path = "wit" }

View file

@ -0,0 +1 @@
../../LICENSE-APACHE

View file

@ -0,0 +1,15 @@
fn main() {
let version = std::env::var("CARGO_PKG_VERSION").unwrap();
let out_dir = std::env::var("OUT_DIR").unwrap();
let mut parts = version.split(|c: char| !c.is_digit(10));
let major = parts.next().unwrap().parse::<u16>().unwrap().to_be_bytes();
let minor = parts.next().unwrap().parse::<u16>().unwrap().to_be_bytes();
let patch = parts.next().unwrap().parse::<u16>().unwrap().to_be_bytes();
std::fs::write(
std::path::Path::new(&out_dir).join("version_bytes"),
[major[0], major[1], minor[0], minor[1], patch[0], patch[1]],
)
.unwrap();
}

View file

@ -0,0 +1,62 @@
pub struct Guest;
pub use wit::*;
pub type Result<T, E = String> = core::result::Result<T, E>;
pub trait Extension: Send + Sync {
fn new() -> Self
where
Self: Sized;
fn language_server_command(
&mut self,
config: wit::LanguageServerConfig,
worktree: &wit::Worktree,
) -> Result<Command>;
}
#[macro_export]
macro_rules! register_extension {
($extension_type:ty) => {
#[export_name = "init-extension"]
pub extern "C" fn __init_extension() {
zed_extension_api::register_extension(|| {
Box::new(<$extension_type as zed_extension_api::Extension>::new())
});
}
};
}
#[doc(hidden)]
pub fn register_extension(build_extension: fn() -> Box<dyn Extension>) {
unsafe { EXTENSION = Some((build_extension)()) }
}
fn extension() -> &'static mut dyn Extension {
unsafe { EXTENSION.as_deref_mut().unwrap() }
}
static mut EXTENSION: Option<Box<dyn Extension>> = None;
#[cfg(target_arch = "wasm32")]
#[link_section = "zed:api-version"]
#[doc(hidden)]
pub static ZED_API_VERSION: [u8; 6] = *include_bytes!(concat!(env!("OUT_DIR"), "/version_bytes"));
mod wit {
wit_bindgen::generate!({
exports: { world: super::Component },
skip: ["init-extension"]
});
}
struct Component;
impl wit::Guest for Component {
fn language_server_command(
config: wit::LanguageServerConfig,
worktree: &wit::Worktree,
) -> Result<wit::Command> {
extension().language_server_command(config, worktree)
}
}

View file

@ -0,0 +1,80 @@
package zed:extension;
world extension {
export init-extension: func();
record github-release {
version: string,
assets: list<github-release-asset>,
}
record github-release-asset {
name: string,
download-url: string,
}
record github-release-options {
require-assets: bool,
pre-release: bool,
}
enum os {
mac,
linux,
windows,
}
enum architecture {
aarch64,
x86,
x8664,
}
enum downloaded-file-type {
gzip,
gzip-tar,
zip,
uncompressed,
}
variant language-server-installation-status {
checking-for-update,
downloaded,
downloading,
cached,
failed(string),
}
/// Gets the current operating system and architecture
import current-platform: func() -> tuple<os, architecture>;
/// Gets the latest version of the given NPM package.
import npm-package-latest-version: func(package-name: string) -> result<string, string>;
/// Gets the latest release for the given GitHub repository.
import latest-github-release: func(repo: string, options: github-release-options) -> result<github-release, string>;
/// Downloads a file from the given url, and saves it to the given filename within the extension's
/// working directory. Extracts the file according to the given file type.
import download-file: func(url: string, output-filename: string, file-type: downloaded-file-type) -> result<_, string>;
/// Updates the installation status for the given language server.
import set-language-server-installation-status: func(language-server-name: string, status: language-server-installation-status);
record command {
command: string,
args: list<string>,
env: list<tuple<string, string>>,
}
resource worktree {
read-text-file: func(path: string) -> result<string, string>;
}
record language-server-config {
name: string,
language-name: string,
}
export language-server-command: func(config: language-server-config, worktree: borrow<worktree>) -> result<command, string>;
}