extension_api.rs

 1pub struct Guest;
 2pub use wit::*;
 3
 4pub type Result<T, E = String> = core::result::Result<T, E>;
 5
 6pub trait Extension: Send + Sync {
 7    fn new() -> Self
 8    where
 9        Self: Sized;
10
11    fn language_server_command(
12        &mut self,
13        config: wit::LanguageServerConfig,
14        worktree: &wit::Worktree,
15    ) -> Result<Command>;
16}
17
18#[macro_export]
19macro_rules! register_extension {
20    ($extension_type:ty) => {
21        #[export_name = "init-extension"]
22        pub extern "C" fn __init_extension() {
23            std::env::set_current_dir(std::env::var("PWD").unwrap()).unwrap();
24            zed_extension_api::register_extension(|| {
25                Box::new(<$extension_type as zed_extension_api::Extension>::new())
26            });
27        }
28    };
29}
30
31#[doc(hidden)]
32pub fn register_extension(build_extension: fn() -> Box<dyn Extension>) {
33    unsafe { EXTENSION = Some((build_extension)()) }
34}
35
36fn extension() -> &'static mut dyn Extension {
37    unsafe { EXTENSION.as_deref_mut().unwrap() }
38}
39
40static mut EXTENSION: Option<Box<dyn Extension>> = None;
41
42#[cfg(target_arch = "wasm32")]
43#[link_section = "zed:api-version"]
44#[doc(hidden)]
45pub static ZED_API_VERSION: [u8; 6] = *include_bytes!(concat!(env!("OUT_DIR"), "/version_bytes"));
46
47mod wit {
48    wit_bindgen::generate!({
49        exports: { world: super::Component },
50        skip: ["init-extension"]
51    });
52}
53
54struct Component;
55
56impl wit::Guest for Component {
57    fn language_server_command(
58        config: wit::LanguageServerConfig,
59        worktree: &wit::Worktree,
60    ) -> Result<wit::Command> {
61        extension().language_server_command(config, worktree)
62    }
63}