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            zed_extension_api::register_extension(|| {
24                Box::new(<$extension_type as zed_extension_api::Extension>::new())
25            });
26        }
27    };
28}
29
30#[doc(hidden)]
31pub fn register_extension(build_extension: fn() -> Box<dyn Extension>) {
32    unsafe { EXTENSION = Some((build_extension)()) }
33}
34
35fn extension() -> &'static mut dyn Extension {
36    unsafe { EXTENSION.as_deref_mut().unwrap() }
37}
38
39static mut EXTENSION: Option<Box<dyn Extension>> = None;
40
41#[cfg(target_arch = "wasm32")]
42#[link_section = "zed:api-version"]
43#[doc(hidden)]
44pub static ZED_API_VERSION: [u8; 6] = *include_bytes!(concat!(env!("OUT_DIR"), "/version_bytes"));
45
46mod wit {
47    wit_bindgen::generate!({
48        exports: { world: super::Component },
49        skip: ["init-extension"]
50    });
51}
52
53struct Component;
54
55impl wit::Guest for Component {
56    fn language_server_command(
57        config: wit::LanguageServerConfig,
58        worktree: &wit::Worktree,
59    ) -> Result<wit::Command> {
60        extension().language_server_command(config, worktree)
61    }
62}