ZIm/crates/extension_api/src/extension_api.rs
Max Brunsfeld 6ebe599c98
Fix issues with extension API that come up when moving Svelte into an extension (#9611)
We're doing it. Svelte support is moving into an extension. This PR
fixes some issues that came up along the way.

Notes

* extensions need to be able to retrieve the path the `node` binary
installed by Zed
* previously we were silently swallowing any errors that occurred while
loading a grammar
* npm commands ran by extensions weren't run in the right directory
* Tree-sitter's WASM stdlib didn't support a C function (`strncmp`)
needed by the Svelte parser's external scanner
* the way that LSP installation status was reported was unnecessarily
complex

Release Notes:

- Removed built-in support for the Svelte and Gleam languages, because
full support for those languages is now available via extensions. These
extensions will be suggested for download when you open a `.svelte` or
`.gleam` file.

---------

Co-authored-by: Marshall <marshall@zed.dev>
2024-03-22 17:29:06 -07:00

78 lines
2 KiB
Rust

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: LanguageServerConfig,
worktree: &Worktree,
) -> Result<Command>;
fn language_server_initialization_options(
&mut self,
_config: LanguageServerConfig,
_worktree: &Worktree,
) -> Result<Option<String>> {
Ok(None)
}
}
#[macro_export]
macro_rules! register_extension {
($extension_type:ty) => {
#[export_name = "init-extension"]
pub extern "C" fn __init_extension() {
std::env::set_current_dir(std::env::var("PWD").unwrap()).unwrap();
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!({
skip: ["init-extension"],
path: "./wit/0.0.4",
});
}
wit::export!(Component);
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)
}
fn language_server_initialization_options(
config: LanguageServerConfig,
worktree: &Worktree,
) -> Result<Option<String>, String> {
extension().language_server_initialization_options(config, worktree)
}
}