since_v0_0_1.rs

  1use super::latest;
  2use crate::wasm_host::WasmState;
  3use crate::wasm_host::wit::since_v0_0_4;
  4use anyhow::Result;
  5use extension::{ExtensionLanguageServerProxy, WorktreeDelegate};
  6use gpui::BackgroundExecutor;
  7use language::BinaryStatus;
  8use semantic_version::SemanticVersion;
  9use std::sync::{Arc, OnceLock};
 10use wasmtime::component::{Linker, Resource};
 11
 12pub const MIN_VERSION: SemanticVersion = SemanticVersion::new(0, 0, 1);
 13
 14wasmtime::component::bindgen!({
 15    async: true,
 16    trappable_imports: true,
 17    path: "../extension_api/wit/since_v0.0.1",
 18    with: {
 19         "worktree": ExtensionWorktree,
 20         "zed:extension/github": latest::zed::extension::github,
 21         "zed:extension/platform": latest::zed::extension::platform,
 22    },
 23});
 24
 25pub type ExtensionWorktree = Arc<dyn WorktreeDelegate>;
 26
 27pub fn linker(executor: &BackgroundExecutor) -> &'static Linker<WasmState> {
 28    static LINKER: OnceLock<Linker<WasmState>> = OnceLock::new();
 29    LINKER.get_or_init(|| super::new_linker(executor, Extension::add_to_linker))
 30}
 31
 32impl From<DownloadedFileType> for latest::DownloadedFileType {
 33    fn from(value: DownloadedFileType) -> Self {
 34        match value {
 35            DownloadedFileType::Gzip => latest::DownloadedFileType::Gzip,
 36            DownloadedFileType::GzipTar => latest::DownloadedFileType::GzipTar,
 37            DownloadedFileType::Zip => latest::DownloadedFileType::Zip,
 38            DownloadedFileType::Uncompressed => latest::DownloadedFileType::Uncompressed,
 39        }
 40    }
 41}
 42
 43impl From<since_v0_0_4::LanguageServerConfig> for LanguageServerConfig {
 44    fn from(value: since_v0_0_4::LanguageServerConfig) -> Self {
 45        Self {
 46            name: value.name,
 47            language_name: value.language_name,
 48        }
 49    }
 50}
 51
 52impl From<Command> for latest::Command {
 53    fn from(value: Command) -> Self {
 54        Self {
 55            command: value.command,
 56            args: value.args,
 57            env: value.env,
 58        }
 59    }
 60}
 61
 62impl HostWorktree for WasmState {
 63    async fn read_text_file(
 64        &mut self,
 65        delegate: Resource<Arc<dyn WorktreeDelegate>>,
 66        path: String,
 67    ) -> wasmtime::Result<Result<String, String>> {
 68        latest::HostWorktree::read_text_file(self, delegate, path).await
 69    }
 70
 71    async fn shell_env(
 72        &mut self,
 73        delegate: Resource<Arc<dyn WorktreeDelegate>>,
 74    ) -> wasmtime::Result<EnvVars> {
 75        latest::HostWorktree::shell_env(self, delegate).await
 76    }
 77
 78    async fn which(
 79        &mut self,
 80        delegate: Resource<Arc<dyn WorktreeDelegate>>,
 81        binary_name: String,
 82    ) -> wasmtime::Result<Option<String>> {
 83        latest::HostWorktree::which(self, delegate, binary_name).await
 84    }
 85
 86    async fn drop(&mut self, _worktree: Resource<Worktree>) -> Result<()> {
 87        Ok(())
 88    }
 89}
 90
 91impl ExtensionImports for WasmState {
 92    async fn node_binary_path(&mut self) -> wasmtime::Result<Result<String, String>> {
 93        latest::nodejs::Host::node_binary_path(self).await
 94    }
 95
 96    async fn npm_package_latest_version(
 97        &mut self,
 98        package_name: String,
 99    ) -> wasmtime::Result<Result<String, String>> {
100        latest::nodejs::Host::npm_package_latest_version(self, package_name).await
101    }
102
103    async fn npm_package_installed_version(
104        &mut self,
105        package_name: String,
106    ) -> wasmtime::Result<Result<Option<String>, String>> {
107        latest::nodejs::Host::npm_package_installed_version(self, package_name).await
108    }
109
110    async fn npm_install_package(
111        &mut self,
112        package_name: String,
113        version: String,
114    ) -> wasmtime::Result<Result<(), String>> {
115        latest::nodejs::Host::npm_install_package(self, package_name, version).await
116    }
117
118    async fn latest_github_release(
119        &mut self,
120        repo: String,
121        options: GithubReleaseOptions,
122    ) -> wasmtime::Result<Result<GithubRelease, String>> {
123        latest::zed::extension::github::Host::latest_github_release(self, repo, options).await
124    }
125
126    async fn current_platform(&mut self) -> Result<(Os, Architecture)> {
127        latest::zed::extension::platform::Host::current_platform(self).await
128    }
129
130    async fn set_language_server_installation_status(
131        &mut self,
132        server_name: String,
133        status: LanguageServerInstallationStatus,
134    ) -> wasmtime::Result<()> {
135        let status = match status {
136            LanguageServerInstallationStatus::CheckingForUpdate => BinaryStatus::CheckingForUpdate,
137            LanguageServerInstallationStatus::Downloading => BinaryStatus::Downloading,
138            LanguageServerInstallationStatus::Cached
139            | LanguageServerInstallationStatus::Downloaded => BinaryStatus::None,
140            LanguageServerInstallationStatus::Failed(error) => BinaryStatus::Failed { error },
141        };
142
143        self.host
144            .proxy
145            .update_language_server_status(lsp::LanguageServerName(server_name.into()), status);
146
147        Ok(())
148    }
149
150    async fn download_file(
151        &mut self,
152        url: String,
153        path: String,
154        file_type: DownloadedFileType,
155    ) -> wasmtime::Result<Result<(), String>> {
156        latest::ExtensionImports::download_file(self, url, path, file_type.into()).await
157    }
158}